如何在shallowMount上使用vue-test-utils设置属性?

我想测试我的Vue组件中的方法,但是我需要为此模拟一些属性数据,稍后以this.$attrs.pattern的形式访问,等等...我当前的代码是:

let wrapper;

beforeEach(() => {
   wrapper = shallowMount(Input);
});

afterEach(() => {
   wrapper.destroy();
});

it('should pass pattern check',() => {
   // I want to setup pattern attribute here
   expect(wrapper.vm.passpatternCheck).toBeTruthy();
});

我原以为会有类似wrapper.setProps()的东西,但是还找不到。

xuan2901 回答:如何在shallowMount上使用vue-test-utils设置属性?

似乎尚无支持将以后的attrs添加到wrapper的支持。因此,我必须删除beforeEach,而对完成的每个测试都使用特定的shallowMount来进行attrs。看起来像这样:

let wrapper;

afterEach(() => {
   wrapper.destroy();
});

it('should pass pattern check',() => {
   wrapper = shallowMount(Input,{
      attrs: {
          pattern: regex,}});
   expect(wrapper.vm.passPatternCheck).toBeTruthy();
});

如果能像以后attrs一样支持以后再添加props: wrapper.setProps({}),那就太好了

本文链接:https://www.f2er.com/3021190.html

大家都在问