Search code examples
vue.jsvuejs2vue-test-utilsvue-property-decorator

How to test @InjectReactive() of Vue-Property-Decorator on vue-test-utils?


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.


Solution

  • Put all reactive properties inside __reactiveInject__. For example:

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