2020-08-27 21:22:32 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import Image from '../src/index.vue'
|
|
|
|
import { nextTick } from 'vue'
|
|
|
|
|
2020-09-05 12:17:37 +08:00
|
|
|
import { IMAGE_SUCCESS, IMAGE_FAIL, mockImageEvent } from '../../test-utils/mock'
|
2020-08-27 21:22:32 +08:00
|
|
|
|
2020-09-05 12:17:37 +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(Image, {
|
|
|
|
props: {
|
|
|
|
src: IMAGE_SUCCESS,
|
|
|
|
alt,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
|
2020-09-05 12:17:37 +08:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
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('imageStyle fit test', async () => {
|
|
|
|
const fits = ['fill', 'contain', 'cover', 'none', 'scale-down']
|
|
|
|
for (const fit of fits) {
|
|
|
|
const wrapper = mount(Image, {
|
|
|
|
props: { fit, src: IMAGE_SUCCESS },
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
await doubleWait()
|
2020-08-27 21:22:32 +08:00
|
|
|
expect(wrapper.find('img').attributes('style')).toContain(`object-fit: ${fit};`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
test('preview classname test', async () => {
|
|
|
|
const wrapper = mount(Image, {
|
|
|
|
props: {
|
|
|
|
fit: 'cover',
|
|
|
|
src: IMAGE_SUCCESS,
|
|
|
|
previewSrcList: new Array(3).fill(IMAGE_SUCCESS),
|
|
|
|
},
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
await doubleWait()
|
2020-08-27 21:22:32 +08:00
|
|
|
expect(wrapper.find('img').classes()).toContain('el-image__preview')
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test('$attrs', async () => {
|
|
|
|
const alt = 'this ia alt'
|
|
|
|
const wrapper = mount(Image, {
|
|
|
|
props: {
|
|
|
|
src: IMAGE_SUCCESS,
|
|
|
|
alt,
|
|
|
|
referrerpolicy: 'origin',
|
|
|
|
},
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
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() => {
|
|
|
|
let result = false
|
|
|
|
const wrapper = mount(Image, {
|
|
|
|
props: {
|
|
|
|
src: IMAGE_SUCCESS,
|
|
|
|
onClick: () => result = true,
|
|
|
|
},
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
await doubleWait()
|
|
|
|
await wrapper.find('.el-image__inner').trigger('click')
|
2020-08-27 21:22:32 +08:00
|
|
|
expect(result).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
//@todo lazy image test
|
|
|
|
|
|
|
|
test('big image preview', async() => {
|
|
|
|
const wrapper = mount(Image, {
|
|
|
|
props: {
|
|
|
|
src: IMAGE_SUCCESS,
|
|
|
|
previewSrcList: [IMAGE_SUCCESS],
|
|
|
|
},
|
|
|
|
})
|
2020-09-05 12:17:37 +08:00
|
|
|
await doubleWait()
|
2020-08-27 21:22:32 +08:00
|
|
|
expect(wrapper.find('.el-image__inner').exists()).toBe(true)
|
|
|
|
await wrapper.find('.el-image__inner').trigger('click')
|
|
|
|
const viewer = wrapper.find('.el-image-viewer__wrapper')
|
|
|
|
expect(viewer.exists()).toBe(true)
|
|
|
|
await wrapper.find('.el-image-viewer__close').trigger('click')
|
|
|
|
expect(wrapper.find('.el-image-viewer__wrapper').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
})
|