unit tests for navigation

This commit is contained in:
Kroell 2023-01-18 11:28:21 -05:00
parent 934dccd37d
commit 54705cc61a

View file

@ -1,15 +1,19 @@
// Components // Components
import licensePlateLookup from "@/layouts/license-plate-lookup/license-plate-lookup.vue"; import licensePlateLookup from "@/layouts/license-plate-lookup/license-plate-lookup.vue";
// Supporting files // Supporting Files
import { shallowMount } from '@vue/test-utils';
import { settleAllPromises } from "@/helpers/layout-helper.js"; import { settleAllPromises } from "@/helpers/layout-helper.js";
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper"; import { shallowMount } from "@vue/test-utils";
import baseMixin from "@/mixins/base-mixin.js";
import { getMountOptions } from "@/helpers/unit-test-helper.js"; import { getMountOptions } from "@/helpers/unit-test-helper.js";
import { nextTick } from "vue";
import { useMainStore } from "@/store"; import { useMainStore } from "@/store";
import { applicationConfig } from "@/constants/application-config"; import { nextTick } from "vue";
import { navigationScenarios } from "@/router/router-constants/navigation-scenarios";
import e from "express";
jest.mock("@/helpers/damage-helper", () => ({
isGlassAvailableForCarId: jest.fn().mockImplementation(() => true),
getDamageString: jest.fn(),
}));
// Mock our module for promises. // Mock our module for promises.
jest.mock("@/helpers/layout-helper.js", () => ({ jest.mock("@/helpers/layout-helper.js", () => ({
@ -22,27 +26,49 @@ jest.mock("@/helpers/cms-content-helper", () => ({
})); }));
describe("license-plate-lookup.vue", () => { describe("license-plate-lookup.vue", () => {
test("Vehicle make set, arePagePrerequisitesValid should be true", () => { describe ("page level alerts", () => {
//Arrange test("License plate matches a different vehicle, display MatchedDifferentVehicleAlert", async () => {
const { wrapper } = setupMocks({}); // Arrange
useMainStore().order.vehicle.carId = "CR00000395"; const mockRegistrationLicensePlate = {
licensePlate: "UTN990",
};
//Act const { wrapper } = setupMocks({});
licensePlateLookup.beforeRouteEnter.call(
wrapper.vm, useMainStore().order.vehicle.carId = "C00000";
{ query: { issPage: "license-plate-lookup" } },
undefined,
(c) => c(wrapper.vm)
);
let arePagePrerequisitesValid = wrapper.vm.arePagePrerequisitesValid(); await wrapper.setData({
licensePlate: mockRegistrationLicensePlate
});
//Assert // Act
expect(arePagePrerequisitesValid).toBe(true); await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.findComponent({ ref: "alertMatchedDifferentVehicle" }).isVisible()).toBe(
true
);
}); });
}); })
describe ("navigation", () => {
test("Vehicle make set, arePagePrerequisitesValid should be true", () => {
// Arrange
const { wrapper } = setupMocks({});
useMainStore().order.vehicle.carId = "CR00000395";
describe("navigation", () => { // Act
licensePlateLookup.beforeRouteEnter.call(
wrapper.vm,
{ query: { issPage: "license-plate-lookup" } },
undefined,
(c) => c(wrapper.vm)
);
let arePagePrerequisitesValid = wrapper.vm.arePagePrerequisitesValid();
// Assert
expect(arePagePrerequisitesValid).toBe(true);
});
test("BackButtonAction triggers a router.navigate change", async () => { test("BackButtonAction triggers a router.navigate change", async () => {
//Arrange //Arrange
const { wrapper, apiPromise } = setupMocks({ const { wrapper, apiPromise } = setupMocks({
@ -59,86 +85,90 @@ describe("navigation", () => {
undefined, undefined,
(c) => c(wrapper.vm) (c) => c(wrapper.vm)
); );
wrapper.vm.backButtonAction(); await wrapper.vm.backButtonAction();
await nextTick();
//Assert //Assert
return apiPromise.finally(() => { expect(wrapper.vm.$router.navigate).toHaveBeenCalled();
try { })
expect(wrapper.vm.$router.navigate).toHaveBeenCalled();
}
catch (e) {
throw e
}
});
});
// TODO: Create test for navigating forward when user clicks Continue
// and information entered is correct (no alerts)
// TODO: Create test for license-plate-question component emitting value when user clicks Continue
// and information entered is correct (no alerts)
// TODO: Create test for zip-question component emitting value when user clicks Continue
// and information entered is correct (no alerts)
// *The above will be completed when navigation from page is complete (SSR-179)
}); });
});
function setupMocks({ function setupMocks({
pageHeaderWidgetHeaderText = {}, isZipValid = true,
pageFooterWidgetCmsContent = {}, isZipServiceable = true,
mountOptionsMockData = {}, lookupVinByPlateResponse,
//Values to set in the state store partsOrQuestions = [],
carId = null, vehicle = {},
registrationZipCode = null, //"55555", carId = "C0000",
licensePlate = null, //"TESTPLATE", route = null,
}) { })
//Mock api responses {
const apiResponses = { useMainStore().validateZip = jest.fn().mockImplementation(() => {
cmsContent: { return Promise.resolve({
SiteSubHeaderWidget: pageHeaderWidgetHeaderText, data: {
VehicleBannerWidget: { isValid: isZipValid,
GenericVehicleImage: isServiceable: isZipServiceable,
`${applicationConfig.ISS_DEV_CMS_DOMAIN}/images/default-source/default-album/blurred-image.jpg`, },
}, })
SiteHeaderWidget: { });
LogoImage:
`${applicationConfig.ISS_DEV_CMS_DOMAIN}/images/default-source/default-album/logos/insuranceLogo.jpg`, useMainStore().lookupVinByPlate = jest.fn().mockImplementation(() => {
}, return Promise.resolve({
SiteFooterWidget: pageFooterWidgetCmsContent, data: lookupVinByPlateResponse
? lookupVinByPlateResponse
: {
licensePlate: "TEST1234",
licenseState: "ZZ"
},
})
});
useMainStore().getPartsOrQuestions = jest.fn().mockImplementation(() => {
return Promise.resolve({
data: {
partsOrQuestions: partsOrQuestions,
},
})
});
const wrapper = shallowMount(
licensePlateLookup,
getMountOptions({
route: route ? route : undefined,
router: {
navigate: jest.fn(),
},
mainStore: {
order: {
vehicle: {
carId: carId,
registration: {
licensePlate: "TESTPLATE",
zipCode: "12345",
},
},
},
},
})
);
const apiResponses = {
serviceZipValidationResponse: {
isValid: isZipValid,
isServiceable: isZipServiceable,
}, },
}; vinLookupResponse: {
vehicle: vehicle,
mountOptionsMockData = { },
...mountOptionsMockData, };
router: {
navigate: jest.fn(),
},
store: {
getters: {
vehicle: {
registration: {
licensePlate: licensePlate,
zipCode: registrationZipCode,
},
carId: carId,
},
},
},
};
const apiPromise = Promise.resolve(apiResponses); settleAllPromises.mockImplementation(() => apiResponses);
settleAllPromises.mockImplementation(() => apiPromise);
fetchCmsContentForPage.mockImplementation(() => Promise.resolve());
const mountOptions = getMountOptions(mountOptionsMockData);
const wrapper = shallowMount(licensePlateLookup, mountOptions); wrapper.vm.getCmsContent = jest.fn().mockImplementation(() => "");
wrapper.vm.setCmsContent = jest.fn();
wrapper.vm.$refs.siteFooter.updateButtonText = jest.fn();
wrapper.vm.$refs.siteFooter.removeLoader = jest.fn();
wrapper.vm.setCmsContent = baseMixin.methods.setCmsContent; return { wrapper };
return { wrapper, apiPromise };
} }