如何在Vue-test-utils上测试vue-property-decorator的@InjectReactive()?

发布于 2025-01-21 18:41:48 字数 1547 浏览 0 评论 0原文

我不确定在安装组件时如何提供注射反应性数据。我不需要确切的解决方案,只是一个提示将不胜感激。

这是我的父组件:

@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。我已经看到了他们的源代码,似乎我必须以某种方式提供此密钥__ reactiveInject __

I am not sure how to provide the inject-reactive data when I mount the component. I don't need the exact solution, just a hint would be much appreciated.

Here is my parent component:

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

Here is my child component:

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

Here is my test file:

// 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...

Currently, I am getting the below console error when I run the test:

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

    found in

    ---> <CarrierFilters>
           <Root>

By the way, the above code is working with the @Inject but not with the @InjectReactive. I have seen their source code, seems like I have to provide this key __reactiveInject__ somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2025-01-28 18:41:48

将所有反应性属性放入__ reactiveInject __中。例如:

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

Put all reactive properties inside __reactiveInject__. For example:

const wrapper = mount(Component, {
  localVue,
  provide: {
    __reactiveInject__: {
      foo: "bar"
    },
  },
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文