Вы должны извлечь логику вашего update
хука в отдельный метод:
updated() {
// set showPackages = true after all the child component render
this.handleUpdate();
},
methods: {
handleUpdate() {
this.$nextTick(() => {
this.show = this.element.show ? true : false;
this.done = true;
});
}
}
модульный тест :
import { createLocalVue, shallowMount } from '@vue/test-utils';
import YourComponent from '@/components/YourComponent';
const localVue = createLocalVue();
describe('YourComponent.vue', () => {
test('async update lifecycle hook behavior', () => {
const wrapper = shallowMount(YourComponent, {
localVue,
propsData: {
element: {
show: false
},
done: false,
show: true
}
});
expect(wrapper.exists()).toBe(true);
expect(wrapper.vm.done).toBe(false);
wrapper.vm.handleUpdate();
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.done).toBe(true);
expect(wrapper.vm.show).toBe(false);
});
});
});
См. Также:https://vue -test-utils.vuejs.org / guides / testing-async-components.html
Дополнительное предложение:
Вы можете еще больше улучшить свой код, заменив:
this.show = this.element.show ? true : false;
от
this.show = !!this.element.show;
(см. Также: https://eslint.org/docs/rules/no-unneeded-ternary)