如何通过在 beforeEach 钩子中浅层安装 vue 组件来干燥代码?

2023-12-22

我第一次使用 vue-test-utils 在 Vue 应用程序中实现一堆单元测试。测试确实有效,但是,我想干燥代码。

目前我正在将以下代码添加到每个测试中:

const wrapper = shallowMount(ConceptForm, {
    localVue,
    router,
    propsData: { conceptProp: editingConcept }
});

使测试看起来像这样

it('shows the ctas if the state is equal to editing', () => {
    const wrapper = shallowMount(ConceptForm, {
        localVue,
        router,
        propsData: { conceptProp: editingConcept }
    });

    expect(wrapper.find('#ctas').exists()).toBe(true)
});

我这样做是因为我需要将这个特定的 propsData 传递给包装器。有没有什么方法可以让我在 beforeEach 中浅层安装组件,然后传递 prop?

Thanks!


我认为你可以使用setProps https://vue-test-utils.vuejs.org/api/wrapper/#setprops包装对象的功能。考虑以下示例:

let wrapper;
beforeEach(() => {
    wrapper = shallowMount(ConceptForm, {
        localVue,
        router,
        propsData: { conceptProp: editingConcept }
    });
});

it('shows the ctas if the state is equal to editing', () => {
    wrapper.setProps({ conceptProp: editingConcept });
    expect(wrapper.find('#ctas').exists()).toBe(true)
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过在 beforeEach 钩子中浅层安装 vue 组件来干燥代码? 的相关文章

随机推荐