element-plus/packages/components/image/__tests__/image.test.tsx

157 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-08-27 21:22:32 +08:00
import { nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, expect, test, vi } from 'vitest'
import {
IMAGE_FAIL,
IMAGE_SUCCESS,
mockImageEvent,
} from '@element-plus/test-utils/mock'
import Image from '../src/image.vue'
import type { AnchorHTMLAttributes, ImgHTMLAttributes } from 'vue'
import type { ImageProps } from '../src/image'
type ElImageProps = ImgHTMLAttributes &
AnchorHTMLAttributes &
Partial<ImageProps>
2020-08-27 21:22:32 +08:00
// firstly wait for image event
// secondly wait for vue render
async function doubleWait() {
await nextTick()
await nextTick()
}
2020-08-27 21:22:32 +08:00
describe('Image.vue', () => {
2020-09-07 17:41:18 +08:00
mockImageEvent()
2020-08-27 21:22:32 +08:00
test('render test', () => {
const wrapper = mount(Image)
expect(wrapper.find('.el-image').exists()).toBe(true)
})
test('image load success test', async () => {
const alt = 'this ia alt'
const wrapper = mount({
setup() {
const props: ElImageProps = {
alt,
src: IMAGE_SUCCESS,
}
return () => <Image {...props} />
2020-08-27 21:22:32 +08:00
},
})
expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
await doubleWait()
2020-08-27 21:22:32 +08:00
expect(wrapper.find('.el-image__inner').exists()).toBe(true)
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('.el-image__placeholder').exists()).toBe(false)
expect(wrapper.find('.el-image__error').exists()).toBe(false)
})
test('image load error test', async () => {
const wrapper = mount(Image, {
props: {
src: IMAGE_FAIL,
},
})
await doubleWait()
2020-08-27 21:22:32 +08:00
expect(wrapper.emitted('error')).toBeDefined()
expect(wrapper.find('.el-image__inner').exists()).toBe(false)
expect(wrapper.find('img').exists()).toBe(false)
expect(wrapper.find('.el-image__error').exists()).toBe(true)
})
test('image load sequence success test', async () => {
const wrapper = mount(Image, {
props: {
src: IMAGE_FAIL,
},
})
wrapper.setProps({
src: IMAGE_SUCCESS,
})
expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
await doubleWait()
expect(wrapper.emitted('error')).toBeUndefined()
expect(wrapper.find('.el-image__inner').exists()).toBe(true)
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('.el-image__placeholder').exists()).toBe(false)
expect(wrapper.find('.el-image__error').exists()).toBe(false)
})
2020-08-27 21:22:32 +08:00
test('imageStyle fit test', async () => {
const fits = ['fill', 'contain', 'cover', 'none', 'scale-down'] as const
2020-08-27 21:22:32 +08:00
for (const fit of fits) {
const wrapper = mount(() => <Image src={IMAGE_SUCCESS} fit={fit} />)
await doubleWait()
expect(wrapper.find('img').attributes('style')).toContain(
`object-fit: ${fit};`
)
2020-08-27 21:22:32 +08:00
}
})
test('preview classname test', async () => {
const props: ElImageProps = {
fit: 'cover',
src: IMAGE_SUCCESS,
previewSrcList: Array.from<string>({ length: 3 }).fill(IMAGE_SUCCESS),
}
const wrapper = mount(() => <Image {...props} />)
await doubleWait()
2020-08-27 21:22:32 +08:00
expect(wrapper.find('img').classes()).toContain('el-image__preview')
})
test('preview initial index test', async () => {
const props: ElImageProps = {
src: IMAGE_SUCCESS,
previewSrcList: Array.from<string>({ length: 3 }).fill(IMAGE_FAIL),
initialIndex: 1,
}
const wrapper = mount(() => <Image {...props} />)
await doubleWait()
await wrapper.find('.el-image__inner').trigger('click')
expect(
wrapper.findAll('.el-image-viewer__img')[1].attributes('style')
).not.toContain('display: none')
})
2020-08-27 21:22:32 +08:00
test('$attrs', async () => {
const alt = 'this ia alt'
const props: ElImageProps = {
alt,
src: IMAGE_SUCCESS,
referrerpolicy: 'origin',
}
const wrapper = mount(() => <Image {...props} />)
await doubleWait()
2020-08-27 21:22:32 +08:00
expect(wrapper.find('img').attributes('alt')).toBe(alt)
expect(wrapper.find('img').attributes('referrerpolicy')).toBe('origin')
})
test('pass event listeners', async () => {
2020-08-27 21:22:32 +08:00
let result = false
const props: ElImageProps = {
src: IMAGE_SUCCESS,
onClick: () => (result = true),
}
const wrapper = mount(() => <Image {...props} />)
await doubleWait()
await wrapper.find('.el-image__inner').trigger('click')
2020-08-27 21:22:32 +08:00
expect(result).toBeTruthy()
})
test('emit load event', async () => {
const handleLoad = vi.fn()
const props: ElImageProps = {
src: IMAGE_SUCCESS,
onLoad: handleLoad,
}
const wrapper = mount(() => <Image {...props} />)
await doubleWait()
expect(wrapper.find('.el-image__inner').exists()).toBe(true)
expect(handleLoad).toBeCalled()
})
2020-08-27 21:22:32 +08:00
//@todo lazy image test
})