import { shallowMount, flushPromises } from "@vue/test-utils"; import { getMountOptions } from "@/helpers/unit-test-helper.js"; import { settleAllPromises } from "@/helpers/layout-helper.js"; import { nextTick } from "vue"; import { storeMutations } from "@/constants/store-mutations"; import { storeActions } from "@/constants/store-actions"; import { fetchCmsContentForPage } from "@/helpers/cms-content-helper"; import vehicleYear from "@/layouts/vehicle-year/vehicle-year.vue"; import yearQuestion from "@/layouts/vehicle-year/year-question/year-question"; import funnelHeader from "@/common-components/funnel-header/funnel-header"; import vehicleBanner from "@/common-components/vehicle-banner/vehicle-banner"; import funnelSubHeader from "@/common-components/funnel-sub-header/funnel-sub-header"; import store from "@/store"; jest.mock("@/store", () => ({ commit: jest.fn(), dispatch: jest.fn(), })); // Mock our module for promises. jest.mock("@/helpers/layout-helper.js", () => ({ settleAllPromises: jest.fn(), })); // Mock fetchCmsContentForPage jest.mock("@/helpers/cms-content-helper", () => ({ fetchCmsContentForPage: jest.fn(), })); describe("vehicle-year.vue", () => { test("Year question component is initized with api data", async (done) => { //Arrange const vehicleYearQuestionCmsContent = { QuestionText: "What year is your vehicle?", }; const yearQuestionInitialData = ["2023", "2022", "2021"]; const { wrapper, apiPromise } = setupMocks({ vehicleYearQuestionCmsContent: vehicleYearQuestionCmsContent, yearQuestionInitialData: yearQuestionInitialData, }); //Act vehicleYear.beforeRouteEnter.call( wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm) ); //Assert apiPromise.finally(() => { expect(yearQuestion.methods.initializeComponent).toHaveBeenCalledWith( vehicleYearQuestionCmsContent, yearQuestionInitialData ); done(); }); }); }); describe("vehicle-year.vue", () => { test("Page logo image is initailized with api data", async (done) => { //Arrange const SiteHeaderWidget = { LogoImage: "https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/safelite-logo.svg?sfvrsn=45e7ed06_3", }; const { wrapper, apiPromise } = setupMocks({ SiteHeaderWidget: SiteHeaderWidget, }); //Act vehicleYear.beforeRouteEnter.call( wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm) ); //Assert apiPromise.finally(() => { expect(funnelHeader.methods.initializeComponent).toHaveBeenCalledWith( SiteHeaderWidget ); done(); }); }); }); describe("vehicle-year.vue", () => { test("Vehicle image is initailized with api data", async (done) => { //Arrange const VehicleBannerWidget = { GenericVehicleImage: "https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/blurred-image.jpg?sfvrsn=a6ce3034_3", }; const { wrapper, apiPromise } = setupMocks({ VehicleBannerWidget: VehicleBannerWidget, }); //Act vehicleYear.beforeRouteEnter.call( wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm) ); //Assert apiPromise.finally(() => { expect(vehicleBanner.methods.initializeComponent).toHaveBeenCalledWith( VehicleBannerWidget ); done(); }); }); }); describe("vehicle-year.vue", () => { test("arePagePrerequisitesValid should be true ", async () => { //Arrange const { wrapper } = setupMocks({}); //Act vehicleYear.beforeRouteEnter.call( wrapper.vm, { query: { fmgPage: "vehicle-make" } }, undefined, (c) => c(wrapper.vm) ); let arePagePrerequisitesValid = wrapper.vm.arePagePrerequisitesValid(); await nextTick(); //Assert expect(arePagePrerequisitesValid).toBe(true); }); }); describe("vehicle-year.vue", () => { test("Year set, call invalidation, make, model, style, carId, category should be null", async () => { //Arrange const { wrapper } = setupMocks({}); //Act vehicleYear.beforeRouteEnter.call( wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm) ); wrapper.vm.resetDependentState(); //Assert expect(store.commit).toBeCalledWith(storeMutations.UPDATE_MAKE, null) expect(store.commit).toBeCalledWith(storeMutations.UPDATE_MODEL, null) expect(store.commit).toBeCalledWith(storeMutations.UPDATE_STYLE, null) expect(store.commit).toBeCalledWith(storeMutations.UPDATE_CAR_ID, null) expect(store.commit).toBeCalledWith(storeMutations.UPDATE_VEHICLE_CATEGORY, null) expect(store.dispatch).toBeCalledWith(storeActions.RESET_DAMAGE_STATE_AND_DEPENDENCIES) expect(store.dispatch).toBeCalledWith(storeActions.RESET_REGISTRATION_STATE_AND_DEPENDENCIES) }); }); function setupMocks({ vehicleYearQuestionCmsContent = {}, yearQuestionInitialData = {}, pageHeaderWidgetHeaderText = {}, mountOptionsMockData = {}, }) { //Mock api responses const apiResponses = { cmsContent: { FunnelSubHeaderWidget: { HeaderText: pageHeaderWidgetHeaderText }, VehicleYearQuestion: vehicleYearQuestionCmsContent, VehicleBannerWidget: { GenericVehicleImage: "https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/blurred-image.jpg?sfvrsn=a6ce3034_3", }, FunnelHeaderWidget: { LogoImage: "https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/safelite-logo.svg?sfvrsn=45e7ed06_3", }, }, yearQuestionInitialData: yearQuestionInitialData, }; const apiPromise = Promise.resolve(apiResponses); settleAllPromises.mockImplementation(() => apiPromise); fetchCmsContentForPage.mockImplementation(() => Promise.resolve()); //Mock year question methods yearQuestion.methods = { loadInitialData: jest.fn(), initializeComponent: jest.fn(), }; funnelHeader.methods = { initializeComponent: jest.fn(), }; vehicleBanner.methods = { initializeComponent: jest.fn(), }; funnelSubHeader.methods = { initializeComponent: jest.fn(), }; const mountOptions = getMountOptions(mountOptionsMockData); const wrapper = shallowMount(vehicleYear, mountOptions); const yearQuestionWrapper = wrapper.findComponent({ name: "yearQuestion" }); yearQuestionWrapper.vm.initializeComponent = yearQuestion.methods.initializeComponent; const funnelHeaderWrapper = wrapper.findComponent({ name: "funnelHeader" }); funnelHeaderWrapper.vm.initializeComponent = funnelHeader.methods.initializeComponent; const vehicleBannerWrapper = wrapper.findComponent({ name: "vehicleBanner" }); vehicleBannerWrapper.vm.initializeComponent = vehicleBanner.methods.initializeComponent; const funnelSubHeaderWrapper = wrapper.findComponent({ name: "funnelSubHeader", }); funnelSubHeaderWrapper.vm.initializeComponent = funnelSubHeader.methods.initializeComponent; return { wrapper, apiPromise }; }