0

Below is a mounted I have in a vue component which I want to perform a unit testing on.

mounted () {
        const pageRef = this.$refs.tabRef.offsetParent.offsetParent;
        this.pageSize = {
            width: pageRef.offsetWidth,
            height: pageRef.offsetHeight
        };
},

and below is my unit test for the component


const propsData = {
    configs: {
        tabColor: "#f2f2f2ff",
        activeTabColor: "#4c4c4cff",
        background: "#ffffffff",
        tabData: [
            { id: "", name: "tab1", title: "New Tab", widgets: [], disabled: false },
            { id: "", name: "tab2", title: "Tab 2", widgets: [], disabled: false }
        ],
        eventConfig: []

    },
    id: "",
    mode: "preview",
    pageType: "pixels",
    pageSize: { width: 1920, height: 1080 },
    zoomFactor: 1
};
describe("XSafeTabWidget", () => {
    const localVue = createLocalVue();
    useQuasarComponent(localVue);

    let vm: any = null;
    let wrapper: any = null;
    let store: any = null;
    let newPropsData = clonedeep(propsData) as any;

    localVue.use(Vuex as any);

    wrapper = mount(XSafeTabWidget, {
      localVue,
      propsData,
      mocks: {
          $store: {
            getters: jest.fn(),
            dispatch: jest.fn()
          },
      }
    });

    store = new Vuex.Store({
        getters: {
            isWidgetSelected: (_store: any) => (_wgtId: string) => {
                return true;
            }
        },
        actions: {
          requestAction: (_store: any, action: any) => {
              switch (action.name) {
                  case "updateControlWidgetValue": {
                      newPropsData.value = action.payload;
                      wrapper.setProps(clonedeep(newPropsData));
                      break;
                  }
              }
          }
      }
    });

    wrapper.setProps({ $store: store });

    vm = wrapper.vm as any;

    it("default data loaded correctly", () => {
        expect(vm.tabPosition).toBe("top");
        expect(vm.tabHeight).toBe(3);
        expect(vm.tabWidth).toBe(9.1);
        expect(vm.border).toBe(true);
    });
});

My issue now is that I am getting an error saying that "TypeError: Cannot read property 'offsetParent' of null" which I assume it is because I have to mock my $refs in the unit testing component. How can I do so? I have tried many ways and I am still unable to get it to work. Thank you in advance.

I tried to do as below and it does not work

wrapper = mount(XSafeTabWidget, {
      localVue,
      propsData,
      mocks: {
          $store: {
            getters: jest.fn(),
            dispatch: jest.fn()
          },
          $refs: {
            tabRef: {
              offsetParent: {
                offsetParent: {
                  offsetWidth: 10,
                  offsetHeight: 10
                }
              }
            }
          }
      }
});
3
  • What is tabRef? You need to mock a component that this ref refers to, not just a ref. Commented Dec 12, 2023 at 8:54
  • @EstusFlask The tabRef is a Vue ref which in my code is as below: <template> <div class="fit" :style="cssVars" ref="tabRef"> // Other codes... </div> </template> I believe i have already mock the tabRef which is in $refs. Not sure what Im doing wrong... Commented Dec 13, 2023 at 2:18
  • I don't think you can mock $refs the way you did it because it's not random component property like $store. The question doesn't contain this code. Since it's div ref, the problem is in how jest fake dom works, it cannot handle offsetParent correctly, among many other things Commented Dec 13, 2023 at 8:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.