mirror of
https://github.com/element-plus/element-plus.git
synced 2025-01-12 10:45:10 +08:00
fix(avatar): watch invalid when src is missing from props (#1615)
* fix(avatar): watch invalid when src is missing from props * test(avatar): add test case
This commit is contained in:
parent
7b83c1569e
commit
4d6b26ae7a
@ -68,5 +68,19 @@ describe('Avatar.vue', () => {
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, computed, ref, PropType, watch, toRefs } from 'vue'
|
||||
import { defineComponent, computed, ref, PropType, watch, toRef } from 'vue'
|
||||
|
||||
const ERROR_EVENT = 'error'
|
||||
export default defineComponent({
|
||||
@ -50,7 +50,7 @@ export default defineComponent({
|
||||
setup(props, { emit }) {
|
||||
const hasLoadError = ref(false)
|
||||
|
||||
const { src } = toRefs(props)
|
||||
const src = toRef(props, 'src')
|
||||
// need reset hasLoadError to false if src changed
|
||||
watch(src,()=>{
|
||||
hasLoadError.value = false
|
||||
|
Loading…
Reference in New Issue
Block a user