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:
inottn 2021-03-11 20:47:11 +08:00 committed by GitHub
parent 7b83c1569e
commit 4d6b26ae7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -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)
})
})

View File

@ -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