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
}
}
}
}
}
});