import { SubmissionCoreFormType } from '@sky-distribution/policy-service-interfaces/lib/core/SubmissionForm.type'; import { SubmissionQuotePricingArgs, SubmissionSelectCoveragesArgs, } from '@sky-distribution/policy-service-interfaces/lib/rpc'; import { SubmissionSelectQuoteArgs } from '@sky-distribution/policy-service-interfaces/lib/rpc/SubmissionSelectQuote'; import { Container } from 'typedi'; import { buildDatePartial } from '../../../../models/Core/partials/date'; import { AbstractCoreService } from '../../../../services/Core/AbstractCore.service'; import { Messenger } from '../../../../services/Messenger/Messenger.service'; import { setupContainer } from '../../../testUtils'; import { updateSubmissionResponseMock } from '../../Models/Core/mock/UpdateSubmissionModel.mock'; import { createArgsMocked } from '../../rpc/mock/rpc-requests/submission.create.mock'; import { updateArgsMocked } from '../../rpc/mock/rpc-requests/submission.update.mock'; import * as submissionFormQuoted from './mock/responses/setSelectedQuote.json'; describe('AbstractCore.service', () => { let tmpInstance: AbstractCoreService; beforeAll(() => { setupContainer(); }); beforeEach(() => { jest.clearAllMocks(); tmpInstance = new (class Tmp extends AbstractCoreService { cmdPrefix = 'test'; })(); }); describe('should send create() message with payload', () => { it('should send correct create command', async () => { // given const expectedCmd = 'test.create'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.create(createArgsMocked); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); it('should send correct payload', async () => { // given const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.create(createArgsMocked); // then const payloadSent = sendMocked.mock.calls[0][1]; expect(payloadSent) .toEqual({ submissionForm: { baseData: { accountHolder: { accountHolder: true, dateOfBirth: { day: 14, month: 4, year: 1954, }, displayName: 'GENOWEFA PIGWA', emailAddress1: undefined, firstName: 'Genowefa', homeNumber: undefined, lastName: 'Pigwa', maritalStatus: undefined, pesel_SKY: '72032602354', primaryAddress: { addressLine1: 'SKYStreet', addressLine2: '', addressLine3: '', addressType: 'registered_sky', city: 'SKYCity', country: 'PL', displayName: 'SKYStreet, 00-001, SKYCity', postalCode: '00-001', state: 'PL_04_SKY', }, subtype: 'Person', }, accountOrgType_SKY: 'person_SKY', additionalInsureds_SKY: [], inputDataType_SKY: 'vehicle_normal', jobType: 'Submission', periodStartDate: buildDatePartial(new Date()), policyAddress: { addressLine1: 'SKYStreet', addressLine2: '', addressLine3: '', addressType: 'registered_sky', city: 'SKYCity', country: 'PL', displayName: 'SKYStreet, 00-001, SKYCity', postalCode: '00-001', state: 'PL_04_SKY', }, policyStartDateFromCEP_SKY: undefined, productCode: 'SkyPersonalAuto_SKY', productName: 'SPALine_SKY', salesProcessType_SKY: 'short_path', termType: 'Annual', trackingCode: undefined, }, lobData: { skyPersonalAuto: { coverables: { personalVehicles: [ { fuelType_SKY: 'diesel', LPG_SKY: false, autoalarm: false, costNew: { amount: 5000, }, dnaMapping: false, immobilizer: false, keysNumber_SKY: 2, license: 'BO6V7E0', licensePlate: 'BO6V7E0', licenseState: 'PL_04_SKY', make: 'SKYMake', model: 'SKYModel', primaryUse: 'Ordinary', securityLocationDevice: false, steeringWheelLock: false, transmissionLock: false, vehicleType_SKY: 'passenger_car', vehicleValues: [ { amount: { amount: 78200, currency: 'pln', }, externalSystemID: '12345616', selected: true, source: 'eurotax', }, { amount: { amount: 10, currency: 'pln', }, externalSystemID: 'infoexpert-12345', selected: false, source: 'info_expert', }, ], vin: 'Vin_Sky', year: 1970, }, ], }, }, }, }, }); }); }); describe('should send update() message with payload', () => { it('should send correct update command', async () => { // given const expectedCmd = 'test.update'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.update(updateArgsMocked); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); it('should send correct payload', async () => { // given const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.update(updateArgsMocked); // then const payloadSent = sendMocked.mock.calls[0][1]; expect(payloadSent) .toEqual({ submissionForm: updateSubmissionResponseMock }); }); }); describe('should send selectQuote() message with payload', () => { it('should send correct selectQuote command', async () => { // given const expectedCmd = 'test.selectQuote'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.selectQuote({} as SubmissionSelectQuoteArgs); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); }); describe('should send preQuote() message with payload', () => { it('should send correct preQuote command', async () => { // given const expectedCmd = 'test.quote'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.preQuote({} as SubmissionCoreFormType); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); }); describe('quoteCoverages', () => { it('should send correct quoteCoverages amount', async () => { // given const expectedCmd = 'test.quotePricing'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.quoteCoverages({} as SubmissionQuotePricingArgs); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); }); describe('should send updateCoverages() message with payload', () => { const UPDATED_COVERAGES_MOCK = { skyPersonalAuto: { vehicleCoverages: [ { vehicleName: '2018 OPEL Astra (SPS69006)', fixedId: 1, publicID: 'azuat1:640720', coverages: [ { name: 'OC', coverageCategoryCode: 'PersonalVehicleStndGrp', coverageCategoryDisplayName: 'Personal Vehicle Standard Coverages', hasTerms: false, isValid: true, riskType_SKY: 'primary', paymentPlansAmounts_SKY: { everyyear: { currency: 'pln', amount: 577.67, }, everysixmonths: { currency: 'pln', amount: 635.44, }, }, coverageCode_SKY: 'SPAMTPLCov', coverageRatingParams_SKY: { technicalPrice: 577.67, chance: 0.0064, marketPriceA: 537.3, marketPriceB: 4000, }, updated: false, description: '', required: true, selected: true, publicID: 'zqdh68eba0euaem8o2hd4tnrr6b', amount: { currency: 'pln', amount: 577.67, }, terms: [], }], }], }, valueAddedServices: { lineCoverages: [ { name: 'Ubezpieczenie sprzętu sportowego', coverageCategoryCode: 'PersonalVehicleStndGrp', coverageCategoryDisplayName: 'Personal Vehicle Standard Coverages', hasTerms: true, isValid: true, riskType_SKY: 'secondary', coverageCode_SKY: 'SPASportsEquipmentCov', updated: false, description: '', required: false, selected: false, publicID: 'zn0jq6k062vqgbh1uggat386p3b', terms: [], }], }, }; it('should update submission with updateCoverages response data', async () => { // given const expectedCmd = 'test.updateCoverages'; // eslint-disable-next-line max-len submissionFormQuoted.lobData.skyPersonalAuto.offerings[0].coverages.vehicleCoverages = UPDATED_COVERAGES_MOCK.skyPersonalAuto.vehicleCoverages; // eslint-disable-next-line max-len submissionFormQuoted.lobData.valueAddedServices.offerings[0].coverages.lineCoverages = UPDATED_COVERAGES_MOCK.valueAddedServices.lineCoverages; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockResolvedValue(UPDATED_COVERAGES_MOCK); // when const res = await tmpInstance.updateCoverages(( { submissionForm: submissionFormQuoted, coverages: { addons: [], vases: [], }, } as unknown ) as SubmissionSelectCoveragesArgs); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); expect(res) .toEqual(submissionFormQuoted); }); it('should return submission coverages if selected coverages are not matched by selected quote', async () => { jest.spyOn(Container.get(Messenger), 'send') .mockResolvedValue(undefined); // eslint-disable-next-line max-len const testSubmission = { ...submissionFormQuoted, bindData: { ...submissionFormQuoted.bindData, chosenQuote: '' }, lobData: undefined, }; // when const res = await tmpInstance.updateCoverages(( { submissionForm: testSubmission, coverages: { addons: [], vases: [], }, } as unknown ) as SubmissionSelectCoveragesArgs); // then expect(res) .toEqual(testSubmission); }); }); describe('getByUUID()', () => { it('should send requests to guidewire service', async () => { // given const expectedCmd = 'guidewirePolicy.quote.retrieve.uuid'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.getByUUID({ uuid: 'some-test-uuid' }); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); }); describe('get()', () => { it('should send requests to guidewire service', async () => { // given const expectedCmd = 'guidewirePolicy.quote.retrieve'; const sendMocked = jest .spyOn(Container.get(Messenger), 'send') .mockImplementation(jest.fn()); // when await tmpInstance.get({ quoteID: '00011111' }); // then const cmdSent = sendMocked.mock.calls[0][0]; expect(cmdSent) .toEqual(expectedCmd); }); }); });