import { markRaw, nextTick } from 'vue' import { mount } from '@vue/test-utils' import { User } from '@element-plus/icons-vue' import { IMAGE_FAIL, IMAGE_SUCCESS, mockImageEvent, } from '@element-plus/test-utils' import Avatar from '../src/avatar.vue' import type { VNode } from 'vue' const _mount = (render: () => VNode) => mount({ setup: () => render, }) describe('Avatar.vue', () => { mockImageEvent() test('render test', () => { const wrapper = mount(Avatar) expect(wrapper.find('.el-avatar').exists()).toBe(true) }) test('size is number', () => { const wrapper = _mount(() => ) expect(wrapper.attributes('style')).toContain('--el-avatar-size: 50px;') }) test('size is string', () => { const wrapper = _mount(() => ) expect(wrapper.classes()).toContain('el-avatar--small') }) test('shape', () => { const wrapper = _mount(() => ) expect(wrapper.classes()).toContain('el-avatar--square') }) test('icon avatar', () => { const wrapper = _mount(() => ) expect(wrapper.classes()).toContain('el-avatar--icon') expect(wrapper.findComponent(User).exists()).toBe(true) }) test('image avatar', () => { const wrapper = _mount(() => ) 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'] as const for (const fit of fits) { const wrapper = _mount(() => ) expect(wrapper.find('img').attributes('style')).toContain( `object-fit: ${fit};` ) } }) 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) }) })