vue.js - 如何在 vue-test-utils 上测试 Vue-Property-Decorator 的 @InjectReactive()?

标签 vue.js vuejs2 vue-test-utils vue-property-decorator

我不确定在挂载组件时如何提供注入(inject) react 数据。我不需要确切的解决方案,只需提示即可。

这是我的父组件:

@Component
export default class Preferences extends Vue {
  @ProvideReactive() serviceLevels: CarrierFilterType[] = [];
  // more code here...

这是我的子组件:

@Component
export default class CarrierFilters {
  @InjectReactive() readonly serviceLevels!: CarrierFilterType[];
   // more code here...

这是我的测试文件:

// other imports are here...

import { supportedServiceLevels } from '../../constant';

// initial setup code here...

describe('Settings > Preferences > Content > CarrierFilters.vue', () => {
  beforeEach(() => {
    options = {
      localVue,
      i18n,
      store,
      router,
      vuetify: new Vuetify(),
      provide: { // I am not sure how to provide the inject-reactive data here...?
        serviceLevels: [...supportedServiceLevels],
      },
    };

    initWrapper = shallowMount(CarrierFilters, options);
  });
  // more code here...

目前,我在运行测试时收到以下控制台错误:

console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
    [Vue warn]: Injection "__reactiveInject__" not found

    found in

    ---> <CarrierFilters>
           <Root>

顺便说一句,上面的代码使用了@Inject,但没有使用@InjectReactive。我看过他们的源代码,看来我必须以某种方式提供这个 key __reactiveInject__

最佳答案

将所有响应式属性放入 __reactiveInject__ 中。例如:

const wrapper = mount(Component, {
  localVue,
  provide: {
    __reactiveInject__: {
      foo: "bar"
    },
  },
});

关于vue.js - 如何在 vue-test-utils 上测试 Vue-Property-Decorator 的 @InjectReactive()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71871221/

相关文章:

vue.js - 在 monorepo : Version mismatch error from vue-template-compiler 中构建 vue2 和 vue3

javascript - Vue js 搜索过滤器 - 数据和绑定(bind)加载问题

css - Vue 组件中的响应式 Prop

vue.js test-utils 如何测试挂载生命周期钩子(Hook)中的函数

javascript - 使用 VueJS 将选择选项值传递到输入字段

javascript - 为什么我的 Vue for 循环与 JSON 不起作用?

vue.js - 如何禁用vue test utils中的 "Global error handler detected"警告

javascript - 为什么我的数组在方法调用 live 时更新但在单元测试中不更新?

vue.js - 无法循环遍历 Vue 3 Reactive Array Prop (代理)

vue.js - 什么是 Vue 选项 API?