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

81 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-08-02 18:27:46 +08:00
import { mount } from '@vue/test-utils'
2020-08-02 21:22:39 +08:00
import { nextTick } from 'vue'
2020-08-02 18:27:46 +08:00
import Avatar from '../src/index.vue'
2020-08-02 21:22:39 +08:00
import {IMAGE_SUCCESS, IMAGE_FAIL} from '../../__tests__/mock'
2020-08-02 18:27:46 +08:00
2020-08-02 21:22:39 +08:00
beforeAll(() => {
Object.defineProperty(global.Image.prototype, 'src', {
set(src) {
const event = new Event(
src === IMAGE_FAIL ? 'error' : 'load'
)
nextTick(() => this.dispatchEvent(event))
},
})
})
2020-08-02 18:27:46 +08:00
describe('Avatar.vue', () => {
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, {
props: { size: 50},
})
expect(wrapper.attributes('style')).toContain('height: 50px')
})
test('size is string', () => {
const wrapper = mount(Avatar, {
props: { size: 'small'},
})
expect(wrapper.classes()).toContain('el-avatar--small')
})
test('shape', () => {
const wrapper = mount(Avatar, {
props: { size: 'small', shape: 'square'},
})
expect(wrapper.classes()).toContain('el-avatar--square')
})
test('icon avatar', () => {
const wrapper = mount(Avatar, {
props: { icon: 'el-icon-user-solid'},
})
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:22:39 +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, {
props: { src: IMAGE_FAIL},
slots: {default: 'fallback'}
})
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']
for (let fit of fits) {
const wrapper = mount(Avatar, {
props: { fit, src: IMAGE_SUCCESS },
})
expect(wrapper.find('img').attributes('style')).toContain(`object-fit: ${fit};`)
}
2020-08-02 18:27:46 +08:00
})
})