test(components): [avatar, image] improve image load test stability (#21762)

* test(components): [avatar] improve fallback slot test stability

* test: improve image test
This commit is contained in:
知晓同丶
2025-08-15 14:26:30 +08:00
committed by GitHub
parent 110d4e1d7e
commit 69d717028e
2 changed files with 21 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
import { markRaw, nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { flushPromises, mount } from '@vue/test-utils'
import { describe, expect, test } from 'vitest'
import { User } from '@element-plus/icons-vue'
import {
@@ -54,7 +54,12 @@ describe('Avatar.vue', () => {
)
await nextTick()
wrapper.emitted('error') && expect(wrapper.emitted('error')).toBeDefined()
const img = wrapper.find('img')
if (img.exists()) {
await img.trigger('error')
}
await flushPromises()
expect(wrapper.emitted('error')).toBeDefined()
await nextTick()
expect(wrapper.text()).toBe('fallback')
expect(wrapper.find('img').exists()).toBe(false)

View File

@@ -1,5 +1,5 @@
import { nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { flushPromises, mount } from '@vue/test-utils'
import { describe, expect, test, vi } from 'vitest'
import {
IMAGE_FAIL,
@@ -271,12 +271,19 @@ describe('Image.vue', () => {
src: IMAGE_FAIL,
},
})
wrapper.setProps({
src: IMAGE_SUCCESS,
})
expect(wrapper.find('.el-image__placeholder').exists()).toBe(true)
await doubleWait()
expect(wrapper.emitted('error')).toBeUndefined()
await flushPromises()
const errorCountBefore = wrapper.emitted('error')?.length || 0
await wrapper.setProps({ src: IMAGE_SUCCESS })
await nextTick()
const img = wrapper.find('img')
if (img.exists()) {
await img.trigger('load')
}
await flushPromises()
// expect no new error event to be emitted
expect(wrapper.emitted('error')?.length).toBe(errorCountBefore)
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)