DigitalConsumer.ISS/src/layouts/policy-vehicles/policy-vehicles.spec.js
2023-06-28 14:55:03 -04:00

426 lines
No EOL
13 KiB
JavaScript

import policyVehicles from '@/layouts/policy-vehicles/policy-vehicles.vue';
import { settleAllPromises } from '@/helpers/layout-helper';
import { shallowMount } from '@vue/test-utils';
import { getMountOptions } from '@/helpers/unit-test-helper';
import { useMainStore } from '@/store';
import { fetchCmsContentForPage } from '@/helpers/cms-content-helper';
import { navigationScenarios } from '@/router/router-constants/navigation-scenarios';
import baseMixin from '@/mixins/base-mixin';
import { getRandomString, getRandomInt } from '@/helpers/data-generation';
import { endorsementOptions } from '@/constants/endorsement-options';
import { createTestingPinia } from '@pinia/testing';
import { issPageValues } from "@/router/router-constants/issPage-values";
import { vehicleSelectionOptions } from "@/constants/vehicle-selection-options";
// Mock fetchCmsContentForPage
jest.mock('@/helpers/cms-content-helper', () => ({
fetchCmsContentForPage: jest.fn(),
doesCopyContainRouterLink: jest.fn(),
splitCopyOnCMSPlaceHolder: jest.fn().mockImplementation(() => 'test'),
getRouterLinkRouteFromCopy: jest.fn(),
getRouterLinkDisplayTextFromCopy: jest.fn(),
splitCMSCopyOnParagraphTag: jest.fn(),
}));
// Mock our module for promises.
jest.mock('@/helpers/layout-helper.js', () => ({
settleAllPromises: jest.fn(),
}));
describe('policy-vehicles.vue', () => {
test('Should navigate to CLICKED_BACK if backButtonAction is run', async () => {
// Arrange
const { wrapper } = setupMocks({});
// Act
await wrapper.vm.backButtonAction();
//Assert
expect(wrapper.vm.$router.navigate).toBeCalled();
});
describe('forwardButtonAction', () => {
test('Selected VIN matches vehicle listed in system => update vehicle and navigate forward with CLICKED_FORWARD_LISTED_VEHICLE scenario.', async () => {
//Arrange
const { wrapper } = setupMocks({});
const vin = getRandomString(17,17);
await wrapper.setData({
selectedVehicleVin: vin,
policyVehicles: [
{ vin: vin },
],
bailout: false
});
const year = getRandomInt(1998, 2023);
const lookupVehicleResponse = {
data: {
year: year,
}
};
const store = useMainStore();
store.lookupVehicleByVin.mockReturnValue(Promise.resolve(lookupVehicleResponse));
const expectedInput = {
year: year,
vin: vin,
noCompensation: true,
deductible: 0,
repairWaived: false
}
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.bailout).toBeFalsy();
expect(store.updateVehicle).toHaveBeenCalledWith(expectedInput);
expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
navigationScenarios.CLICKED_FORWARD_LISTED_VEHICLE,
undefined,
{},
{}
);
});
test('Error in lookupVehicleByVin call => bailout true and navigate forward with CLICKED_FORWARD_WITH_BAILOUT scenario.', async () => {
//Arrange
const { wrapper } = setupMocks({});
const vin = getRandomString(17,17);
await wrapper.setData({
selectedVehicleVin: vin,
bailout: false
});
const store = useMainStore();
store.lookupVehicleByVin.mockReturnValue(Promise.reject());
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.bailout).toBeTruthy();
expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
navigationScenarios.CLICKED_FORWARD_WITH_BAILOUT,
undefined,
{},
{}
);
});
test('vehicle not listed => navigate forward with CLICKED_FORWARD_NON_LISTED_VEHICLE scenario.', async () => {
// Arrange
const { wrapper } = setupMocks({});
await wrapper.setData({
selectedVehicleVin: vehicleSelectionOptions.VEHICLE_NOT_LISTED,
});
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
navigationScenarios.CLICKED_FORWARD_NON_LISTED_VEHICLE,
undefined,
{},
{}
);
});
})
describe('noCompensationForSelectedVehicle computed property', () => {
it('No vehicle match => returns true', async () => {
// Arrange
const selectedVin = getRandomString(17,17);
const otherVin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: selectedVin,
policyVehicles: [
{
vin: otherVin,
},
],
}
// Act
const result = policyVehicles.computed.noCompensationForSelectedVehicle.call(testValues);
// Assert
expect(result).toBeTruthy();
});
it('Coverages list empty => true', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
coverages: []
},
],
}
// Act
const result = policyVehicles.computed.noCompensationForSelectedVehicle.call(testValues);
// Assert
expect(result).toBeTruthy();
});
it('Coverages list non-empty => false', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
coverages: [
{
deductible: 0
}
]
},
],
}
// Act
const result = policyVehicles.computed.noCompensationForSelectedVehicle.call(testValues);
// Assert
expect(result).toBeFalsy();
});
})
describe('deductibleForSelectedVehicle computed property', () => {
it('No vehicle match => undefined returned', () => {
// Arrange
const selectedVin = getRandomString(17,17);
const otherVin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: selectedVin,
policyVehicles: [
{
vin: otherVin,
},
],
}
// Act
const result = policyVehicles.computed.deductibleForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(undefined);
});
it('Vehicle match with empty coverages list => 0 returned', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
coverages: []
},
],
}
// Act
const result = policyVehicles.computed.deductibleForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(0);
});
it('Coverages list non empty => deductible from first coverage returned', () => {
// Arrange
const vin = getRandomString(17,17);
const firstDeductible = getRandomInt(1,1000);
const secondDeductible = getRandomInt(1,1000);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
coverages: [
{
deductible: firstDeductible
},
{
deductible: secondDeductible
}
]
},
],
}
// Act
const result = policyVehicles.computed.deductibleForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(firstDeductible);
});
});
describe('repairWaivedForSelectedVehicle computed property', () => {
it('No vehicle match => false returned', () => {
// Arrange
const selectedVin = getRandomString(17,17);
const otherVin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: selectedVin,
policyVehicles: [
{
vin: otherVin,
},
],
}
// Act
const result = policyVehicles.computed.repairWaivedForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(false);
});
it('Endorsements list empty => false returned', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
endorsements: []
},
],
}
// Act
const result = policyVehicles.computed.repairWaivedForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(false);
});
it('Endorsements list non-empty, not containing repair waived => false returned', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
endorsements: [ endorsementOptions.EDUCATOR, endorsementOptions.PARKING_GUARD ]
},
],
}
// Act
const result = policyVehicles.computed.repairWaivedForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(false);
});
it('Endorsements list contains repair waived => true returned', () => {
// Arrange
const vin = getRandomString(17,17);
const testValues = {
selectedVehicleVin: vin,
policyVehicles: [
{
vin: vin,
endorsements: [
endorsementOptions.EDUCATOR,
endorsementOptions.REPAIR_WAIVED,
endorsementOptions.PARKING_GUARD
]
},
],
}
// Act
const result = policyVehicles.computed.repairWaivedForSelectedVehicle.call(testValues);
// Assert
expect(result).toBe(true);
});
});
test('first vehicle is auto-selected if only one vehicle on policy', async () => {
// Arrange
const vin = getRandomString(17,17);
useMainStore().applicationUser = {
pageData: {
[issPageValues.POLICY_VEHICLES]: [ { vin: vin } ],
}
};
const { wrapper } = setupMocks();
// Act
await wrapper.vm.$nextTick();
// Assert
expect(wrapper.vm.selectedVehicleVin).toBe(vin);
});
});
const mockMixin = {
methods: {
getCmsContent: jest.fn(),
},
computed: {
dynamicStrings() {
return { ROUTER_LINK: 'routerLink:' };
},
},
};
function setupMocks()
{
const mountOptions = getMountOptions({
router: {
navigate: jest.fn(),
},
mixins: [mockMixin],
global: {
mocks: {
$route: {
params: {
id: 1
}
},
$router: {
navigate: jest.fn()
}
},
plugins: [createTestingPinia()]
}
});
const apiResponses = {
cmsContent: {},
};
settleAllPromises.mockImplementation(() => apiResponses);
fetchCmsContentForPage.mockImplementation(() => Promise.resolve());
const wrapper = shallowMount(policyVehicles, mountOptions);
wrapper.vm.getCmsContent = jest.fn().mockImplementation(() => '');
wrapper.vm.setCmsContent = jest.fn();
wrapper.vm.setCmsContent = baseMixin.methods.setCmsContent;
wrapper.vm.$refs.siteFooter.updateButtonText = jest.fn();
wrapper.vm.$refs.siteFooter.removeLoader = jest.fn();
return { wrapper };
}