DigitalConsumer.ISS/src/layouts/duplicate-check/duplicate-check.spec.js
2023-10-16 16:14:25 -04:00

522 lines
20 KiB
JavaScript

// Components
import { shallowMount } from '@vue/test-utils';
import { createTestingPinia } from '@pinia/testing';
import duplicateCheck from '@/layouts/duplicate-check/duplicate-check.vue';
// Supporting Files
import { getMountOptions } from '@/helpers/unit-test-helper.js';
import navigationScenarios from '@/router/router-constants/navigation-scenarios.js';
import { getRandomString } from '@/helpers/data-generation.js';
import { useMainStore } from '@/store';
const duplicateOrderText = 'Finish Existing Claim';
describe('duplicateCheck.vue', () => {
describe('Rendering', () => {
test('Should render site header', () => {
// Arrange
const wrapper = shallowMount(duplicateCheck, getMountOptions());
// Act
const siteHeader = wrapper.findComponent({ ref: 'siteHeader' });
// Assert
expect(siteHeader.exists()).toBeTruthy();
});
test('Should render sub title', () => {
// Arrange
const wrapper = shallowMount(duplicateCheck, getMountOptions());
// Act
const siteSubHeader = wrapper.findComponent({ ref: 'siteSubHeader' });
// Assert
expect(siteSubHeader.exists()).toBeTruthy();
});
test('Should render question subcomponent', () => {
// Arrange
const wrapper = shallowMount(duplicateCheck, getMountOptions());
// Act
const question = wrapper.findComponent({ ref: 'buttonQuestion' });
// Assert
expect(question.exists()).toBeTruthy();
});
test('Should render site footer', () => {
// Arrange
const wrapper = shallowMount(duplicateCheck, getMountOptions());
// Act
const footer = wrapper.findComponent({ ref: 'siteFooter' });
// Assert
expect(footer.exists()).toBeTruthy();
});
});
describe('computed properties', () => {
describe('duplicateOrders computed', () => {
test('duplicateOrders in store undefined => returns empty list', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const mainInitialState = {
applicationUser: {
duplicateOrders: undefined
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(0);
});
test('duplicateOrders in store empty => returns empty list', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const mainInitialState = {
applicationUser: {
duplicateOrders: []
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(0);
});
test('duplicateOrder in store with null vehicle year => returns order with only date in subtext', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const referralNumber = getRandomString(6, 6);
const mainInitialState = {
applicationUser: {
duplicateOrders: [
{
vehicleYear: null,
vehicleMake: getRandomString(5, 5),
vehicleModel: getRandomString(5, 5),
responseDate: '1990-09-23T01:12:34',
referralNumber
}
]
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const expectedSubtext = '9/23/1990';
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(1);
expect(wrapper.vm.duplicateOrders[0]).toStrictEqual({
Text: duplicateOrderText,
Name: referralNumber,
SubText: expectedSubtext
});
});
test('duplicateOrder in store with null vehicle make => returns order with only date subtext', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const referralNumber = getRandomString(6, 6);
const mainInitialState = {
applicationUser: {
duplicateOrders: [
{
vehicleYear: getRandomString(6, 6),
vehicleMake: null,
vehicleModel: getRandomString(5, 5),
responseDate: '2004-11-01T01:12:34',
referralNumber
}
]
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const expectedSubtext = '11/1/2004';
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(1);
expect(wrapper.vm.duplicateOrders[0]).toStrictEqual({
Text: duplicateOrderText,
Name: referralNumber,
SubText: expectedSubtext
});
});
test('duplicateOrder in store with null vehicle model => returns order with only date subtext', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const referralNumber = getRandomString(6, 6);
const mainInitialState = {
applicationUser: {
duplicateOrders: [
{
vehicleYear: getRandomString(6, 6),
vehicleMake: getRandomString(6, 6),
vehicleModel: null,
responseDate: '1999-01-15T01:12:34',
referralNumber
}
]
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const expectedSubtext = '1/15/1999';
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(1);
expect(wrapper.vm.duplicateOrders[0]).toStrictEqual({
Text: duplicateOrderText,
Name: referralNumber,
SubText: expectedSubtext
});
});
test('duplicateOrder in store with all vehicle info => returns order with year, make, model and date in subtext', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const referralNumber = getRandomString(6, 6);
const mainInitialState = {
applicationUser: {
duplicateOrders: [
{
vehicleYear: 'YEAR',
vehicleMake: 'make',
vehicleModel: 'MoDel',
responseDate: '2018-03-01T01:12:34',
referralNumber
}
]
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const expectedVehicle = 'Year Make Model';
const expectedDate = '3/1/2018';
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.duplicateOrders.length).toBe(1);
expect(wrapper.vm.duplicateOrders[0]).toStrictEqual({
Text: duplicateOrderText,
Name: referralNumber,
SubText: `${expectedVehicle}, ${expectedDate}`
});
});
});
describe('getNewOrderSelectionName', () => {
test('answersFromCms null => returns empty string', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const answersFromCmsValue = null;
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers' ? answersFromCmsValue : ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.getNewOrderSelectionName).toBe('');
});
test('answersFromCms empty list => returns empty string', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const answersFromCmsValue = [];
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers' ? answersFromCmsValue : ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.getNewOrderSelectionName).toBe('');
});
test('answersFromCms non-empty list whose first item has no Name property => returns empty string', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const answersFromCmsValue = [{ test: getRandomString(6, 6) }];
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers'
? answersFromCmsValue
: ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.getNewOrderSelectionName).toBe('');
});
test('answersFromCms first item has Name property => returns expected', () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const expectedName = getRandomString(6, 6);
const answersFromCmsValue = [{ Name: expectedName }];
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers'
? answersFromCmsValue
: ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Assert
expect(wrapper.vm.getNewOrderSelectionName).toBe(expectedName);
});
});
});
describe('Navigation', () => {
test('Back button clicked triggers navigation', () => {
// Arrange
const wrapper = shallowMount(duplicateCheck, getMountOptions({
router: {
navigate: jest.fn()
}
}));
// Act
wrapper.vm.backButtonAction();
// Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalled();
expect(wrapper.vm.$router.navigate)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_BACK, undefined);
});
describe('forwardButtonAction', () => {
test('Selected duplicate => load session called', async () => {
// Arrange
const newOrderSelectionName = getRandomString(6, 6);
const selectedAnswer = getRandomString(6, 6);
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers'
? [{ Name: newOrderSelectionName }]
: ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
wrapper.setData({ selectedAnswer });
useMainStore().loadSession = jest.fn().mockImplementation(() => Promise.resolve({}));
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.mainStore.loadSession).toHaveBeenCalledTimes(1);
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
});
test('Selected new order => load session not called', async () => {
// Arrange
const newOrderSelectionName = getRandomString(6, 6);
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers'
? [{ Name: newOrderSelectionName }]
: ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
wrapper.setData({ selectedAnswer: newOrderSelectionName });
useMainStore().loadSession = jest.fn().mockImplementation(() => Promise.resolve({}));
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.mainStore.loadSession).toHaveBeenCalledTimes(0);
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
});
test('Load session throws error => still navigate forward', async () => {
// Arrange
const newOrderSelectionName = getRandomString(6, 6);
const selectedAnswer = getRandomString(6, 6);
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
mountOptions.mixins = [{
methods: {
getCmsContent: jest.fn().mockImplementation((_, label) => (label === 'Answers'
? [{ Name: newOrderSelectionName }]
: ''))
}
}];
const wrapper = shallowMount(duplicateCheck, mountOptions);
wrapper.setData({ selectedAnswer });
const error = 'load session error';
useMainStore().loadSession = jest.fn().mockImplementation(() => Promise.reject(error));
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.mainStore.loadSession).toHaveBeenCalledTimes(1);
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
});
test('policyLookupSuccessful true and policy vehicles returned => CLICKED_FORWARD_POLICY_VERIFIED_WITH_VEHICLES', async () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const mainInitialState = {
order: {
policy: {
policyLookupSuccessful: true,
vehicles: [{ test: 'a' }]
}
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
expect(wrapper.vm.$router.navigate)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_POLICY_VERIFIED_WITH_VEHICLES, undefined);
});
test('policyLookupSuccessful true and no policy vehicles returned => CLICKED_FORWARD_POLICY_VERIFIED_NO_VEHICLES', async () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const mainInitialState = {
order: {
policy: {
policyLookupSuccessful: true,
vehicles: []
}
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
expect(wrapper.vm.$router.navigate)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_POLICY_VERIFIED_NO_VEHICLES, undefined);
});
test('policyLookupSuccessful false => CLICKED_FORWARD_POLICY_UNVERIFIED', async () => {
// Arrange
const mountOptions = getMountOptions({
router: { navigate: jest.fn() }
});
const mainInitialState = {
order: {
policy: {
policyLookupSuccessful: false
}
}
};
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(duplicateCheck, mountOptions);
// Act
await wrapper.vm.forwardButtonAction();
// Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalledTimes(1);
expect(wrapper.vm.$router.navigate)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_POLICY_UNVERIFIED, undefined);
});
});
});
});