【发布时间】:2021-06-16 19:26:10
【问题描述】:
我使用vue-test-utils@1.1.3 和jest@26 来测试我的组件。
组件从父级获取provide 数据,并监视它的变化。
父组件注入一个计算值。我想检查观察者(及其调用的功能)是否按预期工作。
我想知道如何更改我在测试中提供的模拟数据?
// ParentComponent.vue, using the @vue/composition-api plugin + syntax
setup() {
let chosenItems = reactive({ items: [] })
function UpdateItems(items) {
chosenItems.items = [...items]
}
provide(
"userItems",
computed(() => chosenItems.items)
)
return { UpdateItems }
}
// The component I want to test, using options-api syntax
export default {
inject: ["userItems"],
watch: {
"userItems.value": function (items) {
// Do Things...
},
},
}
// The test code for the component
const wrapper = mount(ComponentToTest, {
localVue,
router,
provide: {
userItems() {
return ["mockedItems"]
},
},
})
// And now, after mounting, I want to change the data was provided to test the watcher
【问题讨论】:
标签: vuejs2 jestjs vue-test-utils vue-composition-api