image.test.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test, vi } from 'vitest'
  4. import {
  5. IMAGE_FAIL,
  6. IMAGE_SUCCESS,
  7. mockImageEvent,
  8. } from '@element-plus/test-utils/mock'
  9. import Image from '../src/image.vue'
  10. import type { AnchorHTMLAttributes, ImgHTMLAttributes } from 'vue'
  11. import type { ImageProps } from '../src/image'
  12. type ElImageProps = ImgHTMLAttributes &
  13. AnchorHTMLAttributes &
  14. Partial<ImageProps>
  15. // firstly wait for image event
  16. // secondly wait for vue render
  17. async function doubleWait() {
  18. await nextTick()
  19. await nextTick()
  20. }
  21. describe('Image.vue', () => {
  22. mockImageEvent()
  23. test('render test', () => {
  24. const wrapper = mount(Image)
  25. expect(wrapper.find('.el-image').exists()).toBe(true)
  26. })
  27. test('image load success test', async () => {
  28. const alt = 'this ia alt'
  29. const wrapper = mount({
  30. setup() {
  31. const props: ElImageProps = {
  32. alt,
  33. src: IMAGE_SUCCESS,
  34. }
  35. return () => <Image {...props} />
  36. },
  37. })
  38. expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
  39. await doubleWait()
  40. expect(wrapper.find('.el-image__inner').exists()).toBe(true)
  41. expect(wrapper.find('img').exists()).toBe(true)
  42. expect(wrapper.find('.el-image__placeholder').exists()).toBe(false)
  43. expect(wrapper.find('.el-image__error').exists()).toBe(false)
  44. })
  45. test('image load error test', async () => {
  46. const wrapper = mount(Image, {
  47. props: {
  48. src: IMAGE_FAIL,
  49. },
  50. })
  51. await doubleWait()
  52. expect(wrapper.emitted('error')).toBeDefined()
  53. expect(wrapper.find('.el-image__inner').exists()).toBe(false)
  54. expect(wrapper.find('img').exists()).toBe(false)
  55. expect(wrapper.find('.el-image__error').exists()).toBe(true)
  56. })
  57. test('image load sequence success test', async () => {
  58. const wrapper = mount(Image, {
  59. props: {
  60. src: IMAGE_FAIL,
  61. },
  62. })
  63. wrapper.setProps({
  64. src: IMAGE_SUCCESS,
  65. })
  66. expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
  67. await doubleWait()
  68. expect(wrapper.emitted('error')).toBeUndefined()
  69. expect(wrapper.find('.el-image__inner').exists()).toBe(true)
  70. expect(wrapper.find('img').exists()).toBe(true)
  71. expect(wrapper.find('.el-image__placeholder').exists()).toBe(false)
  72. expect(wrapper.find('.el-image__error').exists()).toBe(false)
  73. })
  74. test('imageStyle fit test', async () => {
  75. const fits = ['fill', 'contain', 'cover', 'none', 'scale-down'] as const
  76. for (const fit of fits) {
  77. const wrapper = mount(() => <Image src={IMAGE_SUCCESS} fit={fit} />)
  78. await doubleWait()
  79. expect(wrapper.find('img').attributes('style')).toContain(
  80. `object-fit: ${fit};`
  81. )
  82. }
  83. })
  84. test('preview classname test', async () => {
  85. const props: ElImageProps = {
  86. fit: 'cover',
  87. src: IMAGE_SUCCESS,
  88. previewSrcList: Array.from<string>({ length: 3 }).fill(IMAGE_SUCCESS),
  89. }
  90. const wrapper = mount(() => <Image {...props} />)
  91. await doubleWait()
  92. expect(wrapper.find('img').classes()).toContain('el-image__preview')
  93. })
  94. test('preview initial index test', async () => {
  95. const props: ElImageProps = {
  96. src: IMAGE_SUCCESS,
  97. previewSrcList: Array.from<string>({ length: 3 }).fill(IMAGE_FAIL),
  98. initialIndex: 1,
  99. }
  100. const wrapper = mount(() => <Image {...props} />)
  101. await doubleWait()
  102. await wrapper.find('.el-image__inner').trigger('click')
  103. expect(
  104. wrapper.findAll('.el-image-viewer__img')[1].attributes('style')
  105. ).not.toContain('display: none')
  106. })
  107. test('native loading attributes', async () => {
  108. const wrapper = mount(Image, {
  109. props: {
  110. src: IMAGE_SUCCESS,
  111. loading: 'eager',
  112. } as ElImageProps,
  113. })
  114. await doubleWait()
  115. expect(wrapper.find('img').exists()).toBe(true)
  116. expect(wrapper.find('img').attributes('loading')).toBe('eager')
  117. await wrapper.setProps({ loading: undefined })
  118. expect(wrapper.find('img').attributes('loading')).toBe(undefined)
  119. })
  120. test('$attrs', async () => {
  121. const alt = 'this ia alt'
  122. const props: ElImageProps = {
  123. alt,
  124. src: IMAGE_SUCCESS,
  125. referrerpolicy: 'origin',
  126. }
  127. const wrapper = mount(() => <Image {...props} />)
  128. await doubleWait()
  129. expect(wrapper.find('img').attributes('alt')).toBe(alt)
  130. expect(wrapper.find('img').attributes('referrerpolicy')).toBe('origin')
  131. })
  132. test('pass event listeners', async () => {
  133. let result = false
  134. const props: ElImageProps = {
  135. src: IMAGE_SUCCESS,
  136. onClick: () => (result = true),
  137. }
  138. const wrapper = mount(() => <Image {...props} />)
  139. await doubleWait()
  140. await wrapper.find('.el-image__inner').trigger('click')
  141. expect(result).toBeTruthy()
  142. })
  143. test('emit load event', async () => {
  144. const handleLoad = vi.fn()
  145. const props: ElImageProps = {
  146. src: IMAGE_SUCCESS,
  147. onLoad: handleLoad,
  148. }
  149. const wrapper = mount(() => <Image {...props} />)
  150. await doubleWait()
  151. expect(wrapper.find('.el-image__inner').exists()).toBe(true)
  152. expect(handleLoad).toBeCalled()
  153. })
  154. //@todo lazy image test
  155. })