element-plus/packages/components/avatar/__tests__/avatar.spec.ts

93 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-08-02 21:22:39 +08:00
import { nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import {
IMAGE_SUCCESS,
IMAGE_FAIL,
mockImageEvent,
} from '@element-plus/test-utils'
2020-08-02 18:27:46 +08:00
import Avatar from '../src/index.vue'
2020-08-02 18:27:46 +08:00
describe('Avatar.vue', () => {
2020-09-07 17:41:18 +08:00
mockImageEvent()
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', () => {
const wrapper = mount(Avatar, {
2020-08-02 21:29:35 +08:00
props: { size: 50 },
2020-08-02 21:22:39 +08:00
})
expect(wrapper.attributes('style')).toContain('height: 50px')
})
test('size is string', () => {
const wrapper = mount(Avatar, {
2020-08-02 21:29:35 +08:00
props: { size: 'small' },
2020-08-02 21:22:39 +08:00
})
expect(wrapper.classes()).toContain('el-avatar--small')
})
test('shape', () => {
const wrapper = mount(Avatar, {
2020-08-02 21:29:35 +08:00
props: { size: 'small', shape: 'square' },
2020-08-02 21:22:39 +08:00
})
expect(wrapper.classes()).toContain('el-avatar--square')
})
test('icon avatar', () => {
const wrapper = mount(Avatar, {
2020-08-02 21:29:35 +08:00
props: { icon: 'el-icon-user-solid' },
2020-08-02 21:22:39 +08:00
})
expect(wrapper.classes()).toContain('el-avatar--icon')
expect(wrapper.find('i').classes()).toContain('el-icon-user-solid')
})
test('image avatar', () => {
2020-08-02 18:27:46 +08:00
const wrapper = mount(Avatar, {
2020-08-02 21:29:35 +08:00
props: { src: IMAGE_SUCCESS },
2020-08-02 18:27:46 +08:00
})
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', () => {
const fits = ['fill', 'contain', 'cover', 'none', 'scale-down']
2020-08-02 21:29:35 +08:00
for (const fit of fits) {
2020-08-02 21:22:39 +08:00
const wrapper = mount(Avatar, {
props: { fit, src: IMAGE_SUCCESS },
})
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
})
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
})