import { useMainStore } from '@/store/index.js'; import { setActivePinia, createPinia } from 'pinia'; import { getRandomString, getRandomGuid, getRandomInt, getRandomBoolean } from '@/helpers/data-generation.js'; import { coverageStatuses } from '@/constants/coverage-statuses.js'; import globalMethods from '@/global-methods.js'; describe('Store', () => { let store; beforeEach(() => { const pinia = createPinia(); setActivePinia(pinia); store = useMainStore(); store.applicationUser.eventBus = []; jest.resetAllMocks(); }); it('Should Store Vehicle Year', () => { const testYear = '2001'; store.updateVehicleYear(testYear); expect(store.order.vehicle.year).toEqual(testYear); }); it('Should add events to the bus', () => { // Arrange const category = getRandomString(1, 25); const subCategory = getRandomString(5, 20); const isDismissible = getRandomBoolean(); const copy = getRandomString(5, 25); const headline = getRandomString(5, 25); const type = getRandomString(5, 15); const event = { category, subCategory, eventValue: { isDismissible, messageCopy: copy, messageHeadline: headline, type } }; // Act store.addEventToBus(event); // Assert expect(store.applicationUser.eventBus[0]).toEqual(event); }); it('Should remove events from the bus', () => { // Arrange const category = getRandomString(1, 25); const subCategory = getRandomString(5, 20); const isDismissible = getRandomBoolean(); const copy = getRandomString(5, 25); const headline = getRandomString(5, 25); const type = getRandomString(5, 15); const event = { category, subCategory, eventValue: { isDismissible, messageCopy: copy, messageHeadline: headline, type } }; store.addEventToBus(event); expect(store.applicationUser.eventBus.length).toBe(1); // Act store.removeEventFromBus({ category: event.category, subCategory: event.subCategory }) // Assert expect(store.applicationUser.eventBus.length).toBe(0); }); it('Should return correct event using the getter function eventBusItem', () => { // Arrange const category = getRandomString(1, 25); const subCategory = getRandomString(5, 20); const isDismissible = getRandomBoolean(); const copy = getRandomString(5, 25); const headline = getRandomString(5, 25); const type = getRandomString(5, 15); const event = { category, subCategory, eventValue: { isDismissible, messageCopy: copy, messageHeadline: headline, type } }; store.addEventToBus(event); // Act const actual = store.eventBusItem(event.category, event.subCategory); // Assert expect(actual).toEqual(event.eventValue); }); it('UpdateVehicle should merge vehicle with response object', () => { // Arrange const carId = getRandomString(10, 14); const category = getRandomString(3, 7); const year = getRandomInt(1960, 2023); const make = getRandomString(4, 10); const model = getRandomString(4, 10); const style = getRandomString(4, 15); const imageUrl = getRandomString(50, 100); const imageVifNumber = getRandomInt(10000, 99999).toString(); const imageColor = getRandomString(4, 10); const providedVehicle = { carId, category, year, make, model, style, imageUrl, imageVifNumber, imageVifColor: imageColor }; const expectedVehicle = { carId, category, year, make, model, style, imageUrl, imageVifNumber, imageColor }; // Act store.updateVehicle(providedVehicle); // Assert expect(store.order.vehicle).toMatchObject(expectedVehicle); }); it('UpdateVehicle should set policy values appropriately with repair waived', () => { // Arrange const noCoverage = getRandomBoolean(); const deductible = getRandomInt(1, 500); const vehicle = { noCoverage, deductible, repairWaived: true }; const expectedPolicy = { noCoverage, deductible: { replace: deductible, repair: 0 } }; // Act store.updateVehicle(vehicle); // Assert expect(store.order.policy).toMatchObject(expectedPolicy); }); it('UpdateVehicle should set policy values appropriately with repair not waived', () => { // Arrange const noCoverage = getRandomBoolean(); const deductible = getRandomInt(1, 500); const vehicle = { noCoverage, deductible, repairWaived: false }; const expectedPolicy = { noCoverage, deductible: { replace: deductible, repair: deductible } }; // Act store.updateVehicle(vehicle); // Assert expect(store.order.policy).toMatchObject(expectedPolicy); }); // TODO update test to work also checking store values it('setVehicle should call globalMethods.callHttpClient', () => { // Arrange const carId = getRandomString(10, 14); const category = getRandomString(3, 7); const year = getRandomInt(1960, 2023); const make = getRandomString(4, 10); const model = getRandomString(4, 10); const style = getRandomString(4, 15); const imageUrl = getRandomString(50, 100); const imageVifNumber = getRandomInt(10000, 99999).toString(); const imageColor = getRandomString(4, 10); const response = { data: { carId, category, year, make, model, style, imageUrl, imageVifNumber, imageVifColor: imageColor } }; globalMethods.callHttpClient = jest.fn().mockReturnValue(Promise.resolve(response)); // Act const returned = store.setVehicle(); // Assert expect(globalMethods.callHttpClient).toHaveBeenCalled(); expect(returned).resolves.toMatchObject(response); }); it('saveVehicleDamage with windshield repair should update damage with number of chips not null', () => { // Arrange const glassName = getRandomString(4, 10); const glassLocation = getRandomString(5, 15); const isWindshieldRepair = true; const selectedGlassToReplace = [{ glassName, glassLocation }]; const chipCount = getRandomInt(0, 3); // Act store.saveVehicleDamage(isWindshieldRepair, selectedGlassToReplace, chipCount); // Assert expect(store.order.damage.glassToReplace).toEqual(selectedGlassToReplace); expect(store.order.damage.isRepair).toEqual(isWindshieldRepair); expect(store.order.damage.numberOfChips).toEqual(chipCount); }); it('saveVehicleDamage without windshield repair should update damage with number of chips null', () => { // Arrange const glassName = getRandomString(4, 10); const glassLocation = getRandomString(5, 15); const isWindshieldRepair = false; const selectedGlassToReplace = [{ glassName, glassLocation }]; const chipCount = getRandomInt(0, 3); const expectedChipCount = null; // Act store.saveVehicleDamage(isWindshieldRepair, selectedGlassToReplace, chipCount); // Assert expect(store.order.damage.glassToReplace).toEqual(selectedGlassToReplace); expect(store.order.damage.isRepair).toEqual(isWindshieldRepair); expect(store.order.damage.numberOfChips).toEqual(expectedChipCount); }); it('should return registration data if available', () => { // Arrange const streetAddress = getRandomString(5, 15); const city = getRandomString(5, 15); const state = getRandomString(5, 10); const zipCode = getRandomInt(10000, 99999).toString(); const firstName = getRandomString(5, 20); const lastName = getRandomString(5, 20); const expected = { addressQuestions: { streetAddress, city, state, zipCode }, firstName, lastName }; store.order.vehicle.registration = { licensePlate: null, address: streetAddress, city, state, zipCode, firstName, lastName }; // Act const actual = store.customerData; // Assert expect(actual).toEqual(expected); }); it('should return customer data if registration data unavailable', () => { // Arrange const address = getRandomString(1, 25); const city = getRandomString(5, 20); const state = getRandomString(4, 20); const zipCode = getRandomInt(10000, 99999).toString(); const firstName = getRandomString(5, 25); const lastName = getRandomString(5, 25); const expected = { addressQuestions: { streetAddress: address, city, state, zipCode }, firstName, lastName }; store.order.vehicle.registration.address = null; store.order.customer = { address: { streetAddress: address, city, state, zipCode }, firstName, lastName }; // Act const actual = store.customerData; // Assert expect(actual).toMatchObject(expected); }); describe('registerClaim method', () => { it('successful response with no coverage => isVerified true and coverage status no comp', async () => { // Arrange const response = { data: { claimantId: null, claimNumber: getRandomString(9, 9), correlationId: getRandomGuid(), isSuccess: true, isError: false, successMessage: getRandomString(9, 9), deductible: 0 } }; store.policy.noCoverage = true; globalMethods.callHttpClient = jest.fn().mockReturnValue(Promise.resolve(response)); // Act await store.registerClaim(); // Asserts expect(globalMethods.callHttpClient).toHaveBeenCalled(); expect(store.payment.insuranceCoverage.isVerified).toBe(true); expect(store.payment.insuranceCoverage.coverageStatus).toBe(coverageStatuses.NO_COMP); }); it('successful response with coverage => isVerified true and coverage status verified', async () => { // Arrange const response = { data: { claimantId: null, claimNumber: getRandomString(9, 9), correlationId: getRandomGuid(), isSuccess: true, isError: false, successMessage: getRandomString(9, 9), deductible: 0 } }; store.policy.noCoverage = false; globalMethods.callHttpClient = jest.fn().mockReturnValue(Promise.resolve(response)); // Act await store.registerClaim(); // Asserts expect(globalMethods.callHttpClient).toHaveBeenCalled(); expect(store.payment.insuranceCoverage.isVerified).toBe(true); expect(store.payment.insuranceCoverage.coverageStatus).toBe(coverageStatuses.VERIFIED); }); it('Call to client returns exception, resulting in object with error property being returned', async () => { // Arrange expect.assertions(4); const error = 'this is the error'; globalMethods.callHttpClient = jest.fn().mockReturnValue(Promise.reject(error)); // Act await store.registerClaim().catch((e) => { expect(e).toEqual(error); }); // Asserts expect(globalMethods.callHttpClient).toHaveBeenCalled(); expect(store.payment.insuranceCoverage.isVerified).toBe(false); expect(store.payment.insuranceCoverage.coverageStatus).toBe(coverageStatuses.PENDING); }); }); describe('updateContactInfo method', () => { it('UpdateContactInfo updates contact info in store', () => { // Arrange const firstName = getRandomString(4, 10); const lastName = getRandomString(5, 15); const emailAddress = false; const phoneNumber = getRandomInt(1000000000, 9999999999); const requestTextUpdates = getRandomBoolean(); const notesForTechnician = getRandomString(50, 150); // Act store.updateContactInfo({ firstName, lastName, emailAddress, phoneNumber, requestTextUpdates, notesForTechnician }); // Assert expect(store.contactInfo.firstName).toEqual(firstName); expect(store.contactInfo.lastName).toEqual(lastName); expect(store.contactInfo.emailAddress).toEqual(emailAddress); expect(store.contactInfo.phoneNumber).toEqual(phoneNumber); expect(store.contactInfo.requestTextUpdates).toEqual(requestTextUpdates); expect(store.contactInfo.notesForTechnician).toEqual(notesForTechnician); }); it('All null values => contact info set in store to all nulls', () => { // Act store.updateContactInfo({}); // Assert expect(store.contactInfo.firstName).toEqual(''); expect(store.contactInfo.lastName).toEqual(''); expect(store.contactInfo.emailAddress).toEqual(''); expect(store.contactInfo.phoneNumber).toEqual(''); expect(store.contactInfo.requestTextUpdates).toEqual(false); expect(store.contactInfo.notesForTechnician).toEqual(''); }); }); // TODO add tests describe('saveSession method', () => {}); });