2022-03-25 15:35:56 +08:00
|
|
|
import { markRaw, nextTick } from 'vue'
|
2021-08-24 13:36:48 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { describe, expect, test } from 'vitest'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { User } from '@element-plus/icons-vue'
|
2021-09-04 19:29:28 +08:00
|
|
|
import {
|
|
|
|
IMAGE_FAIL,
|
2022-03-25 15:35:56 +08:00
|
|
|
IMAGE_SUCCESS,
|
2021-09-04 19:29:28 +08:00
|
|
|
mockImageEvent,
|
2022-04-19 12:46:57 +08:00
|
|
|
} from '@element-plus/test-utils/mock'
|
2020-08-02 18:27:46 +08:00
|
|
|
|
2021-09-13 10:46:12 +08:00
|
|
|
import Avatar from '../src/avatar.vue'
|
2022-02-23 16:38:36 +08:00
|
|
|
import type { VNode } from 'vue'
|
|
|
|
|
|
|
|
const _mount = (render: () => VNode) =>
|
|
|
|
mount({
|
|
|
|
setup: () => render,
|
|
|
|
})
|
2021-08-24 13:36:48 +08:00
|
|
|
|
2020-08-02 18:27:46 +08:00
|
|
|
describe('Avatar.vue', () => {
|
2020-09-07 17:41:18 +08:00
|
|
|
mockImageEvent()
|
2020-09-05 12:17:37 +08:00
|
|
|
|
2020-08-02 18:27:46 +08:00
|
|
|
test('render test', () => {
|
2020-08-02 21:22:39 +08:00
|
|
|
const wrapper = mount(Avatar)
|
|
|
|
expect(wrapper.find('.el-avatar').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('size is number', () => {
|
2022-02-23 16:38:36 +08:00
|
|
|
const wrapper = _mount(() => <Avatar size={50} />)
|
2021-12-12 17:54:21 +08:00
|
|
|
expect(wrapper.attributes('style')).toContain('--el-avatar-size: 50px;')
|
2020-08-02 21:22:39 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('size is string', () => {
|
2022-02-23 16:38:36 +08:00
|
|
|
const wrapper = _mount(() => <Avatar size="small" />)
|
2020-08-02 21:22:39 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-avatar--small')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('shape', () => {
|
2022-02-23 16:38:36 +08:00
|
|
|
const wrapper = _mount(() => <Avatar size="small" shape="square" />)
|
2020-08-02 21:22:39 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-avatar--square')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('icon avatar', () => {
|
2022-03-12 15:58:29 +08:00
|
|
|
const wrapper = _mount(() => <Avatar icon={markRaw(User)} />)
|
2020-08-02 21:22:39 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-avatar--icon')
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(User).exists()).toBe(true)
|
2020-08-02 21:22:39 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('image avatar', () => {
|
2022-02-23 16:38:36 +08:00
|
|
|
const wrapper = _mount(() => <Avatar src={IMAGE_SUCCESS} />)
|
2020-08-02 21:22:39 +08:00
|
|
|
expect(wrapper.find('img').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('image fallback', async () => {
|
|
|
|
const wrapper = mount(Avatar, {
|
2020-08-02 21:29:35 +08:00
|
|
|
props: { src: IMAGE_FAIL },
|
|
|
|
slots: { default: 'fallback' },
|
2020-08-02 21:22:39 +08:00
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.emitted('error')).toBeDefined()
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.text()).toBe('fallback')
|
|
|
|
expect(wrapper.find('img').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('image fit', () => {
|
2022-02-23 16:38:36 +08:00
|
|
|
const fits = ['fill', 'contain', 'cover', 'none', 'scale-down'] as const
|
2020-08-02 21:29:35 +08:00
|
|
|
for (const fit of fits) {
|
2022-02-23 16:38:36 +08:00
|
|
|
const wrapper = _mount(() => <Avatar fit={fit} src={IMAGE_SUCCESS} />)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find('img').attributes('style')).toContain(
|
|
|
|
`object-fit: ${fit};`
|
|
|
|
)
|
2020-08-02 21:22:39 +08:00
|
|
|
}
|
2020-08-02 18:27:46 +08:00
|
|
|
})
|
2021-03-11 20:47:11 +08:00
|
|
|
|
|
|
|
test('src changed', async () => {
|
|
|
|
const wrapper = mount(Avatar, {
|
|
|
|
slots: { default: 'fallback' },
|
|
|
|
})
|
|
|
|
expect(wrapper.vm.hasLoadError).toBe(false)
|
|
|
|
await wrapper.setProps({ src: IMAGE_FAIL })
|
|
|
|
// wait error event trigger
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.vm.hasLoadError).toBe(true)
|
|
|
|
await wrapper.setProps({ src: IMAGE_SUCCESS })
|
|
|
|
expect(wrapper.vm.hasLoadError).toBe(false)
|
|
|
|
expect(wrapper.find('img').exists()).toBe(true)
|
|
|
|
})
|
2020-08-02 18:27:46 +08:00
|
|
|
})
|