From 4b075afa21126f8d19b8b6d8ec1f2190048d50fb Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Thu, 27 Jun 2024 16:39:32 +0200 Subject: [PATCH] test: cover this in tests --- .../useResolvedDeliveryOptions.spec.ts | 107 ++++++++++++------ 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/apps/delivery-options/src/composables/useResolvedDeliveryOptions.spec.ts b/apps/delivery-options/src/composables/useResolvedDeliveryOptions.spec.ts index 3b8fd646..5f10a101 100644 --- a/apps/delivery-options/src/composables/useResolvedDeliveryOptions.spec.ts +++ b/apps/delivery-options/src/composables/useResolvedDeliveryOptions.spec.ts @@ -1,8 +1,17 @@ import {describe, it, expect, beforeEach} from 'vitest'; +import {assign} from 'radash'; import {setActivePinia, createPinia} from 'pinia'; import {normalizeDate} from '@vueuse/core'; import {mockGetDeliveryOptions} from '@myparcel-do/shared/testing'; -import {KEY_CONFIG, CarrierSetting, KEY_CARRIER_SETTINGS, createTimestamp} from '@myparcel-do/shared'; +import { + KEY_CONFIG, + CarrierSetting, + KEY_CARRIER_SETTINGS, + createTimestamp, + type InputDeliveryOptionsConfiguration, + KEY_ADDRESS, +} from '@myparcel-do/shared'; +import {type RecursivePartial} from '@myparcel/ts-utils'; import {DeliveryTypeName, CarrierName} from '@myparcel/constants'; import { waitForDeliveryOptions, @@ -12,22 +21,49 @@ import { } from '../__tests__'; import {useResolvedDeliveryOptions} from './useResolvedDeliveryOptions'; -const mockSetup = async (): Promise => { +const CARRIER_IDENTIFIER_WITH_CONTRACT = `${CarrierName.PostNl}:1234`; + +const doTestSetup = async (config: RecursivePartial = {}): Promise => { + const morning = normalizeDate('2022-01-01T09:00:00'); + const standard = normalizeDate('2022-01-01T15:00:00'); + const evening = normalizeDate('2022-01-01T20:00:00'); + + mockGetDeliveryOptions.mockReturnValueOnce( + Promise.resolve([ + { + date: createTimestamp(standard), + possibilities: [ + createDeliveryPossibility(evening, {type: DeliveryTypeName.Evening}), + createDeliveryPossibility(standard), + createDeliveryPossibility(morning, {type: DeliveryTypeName.Morning}), + ], + }, + ]), + ); + + const carrierSettings = { + [CarrierSetting.AllowDeliveryOptions]: true, + [CarrierSetting.AllowMondayDelivery]: true, + [CarrierSetting.AllowSameDayDelivery]: true, + [CarrierSetting.AllowEveningDelivery]: true, + [CarrierSetting.AllowMorningDelivery]: true, + [CarrierSetting.AllowStandardDelivery]: true, + }; + mockDeliveryOptionsConfig( - getMockDeliveryOptionsConfiguration({ - [KEY_CONFIG]: { - [KEY_CARRIER_SETTINGS]: { - [CarrierName.PostNl]: { - [CarrierSetting.AllowDeliveryOptions]: true, - [CarrierSetting.AllowMondayDelivery]: true, - [CarrierSetting.AllowSameDayDelivery]: true, - [CarrierSetting.AllowEveningDelivery]: true, - [CarrierSetting.AllowMorningDelivery]: true, - [CarrierSetting.AllowStandardDelivery]: true, + getMockDeliveryOptionsConfiguration( + assign( + { + [KEY_CONFIG]: { + [KEY_CARRIER_SETTINGS]: { + [CarrierName.PostNl]: carrierSettings, + [CARRIER_IDENTIFIER_WITH_CONTRACT]: carrierSettings, + }, }, }, - }, - }), + config, + ), + ), ); await waitForDeliveryOptions(); @@ -35,35 +71,38 @@ const mockSetup = async (): Promise => { describe('useResolvedDeliveryOptions', () => { beforeEach(() => { + useResolvedDeliveryOptions.clear(); setActivePinia(createPinia()); }); it('sorts options by time', async () => { - const morning = normalizeDate('2022-01-01T09:00:00'); - const standard = normalizeDate('2022-01-01T15:00:00'); - const evening = normalizeDate('2022-01-01T20:00:00'); + await doTestSetup(); - mockGetDeliveryOptions.mockReturnValueOnce( - Promise.resolve([ - { - date: createTimestamp(standard), - possibilities: [ - createDeliveryPossibility(evening, {type: DeliveryTypeName.Evening}), - createDeliveryPossibility(standard), - createDeliveryPossibility(morning, {type: DeliveryTypeName.Morning}), - ], - }, - ]), - ); + const options = useResolvedDeliveryOptions(); + + const resolvedOptions = options.value.map(({carrier, deliveryType}) => ({carrier, deliveryType})); - await mockSetup(); + expect(resolvedOptions).toEqual([ + {carrier: CarrierName.PostNl, deliveryType: DeliveryTypeName.Morning}, + {carrier: CarrierName.PostNl, deliveryType: DeliveryTypeName.Standard}, + {carrier: CarrierName.PostNl, deliveryType: DeliveryTypeName.Evening}, + {carrier: CARRIER_IDENTIFIER_WITH_CONTRACT, deliveryType: DeliveryTypeName.Morning}, + {carrier: CARRIER_IDENTIFIER_WITH_CONTRACT, deliveryType: DeliveryTypeName.Standard}, + {carrier: CARRIER_IDENTIFIER_WITH_CONTRACT, deliveryType: DeliveryTypeName.Evening}, + ]); + }); + + it('handles fake delivery', async () => { + // DE is not a delivery country for PostNL. + await doTestSetup({[KEY_ADDRESS]: {cc: 'DE'}}); const options = useResolvedDeliveryOptions(); - expect(options.value.map((option) => option.deliveryType)).toEqual([ - DeliveryTypeName.Morning, - DeliveryTypeName.Standard, - DeliveryTypeName.Evening, + const resolvedOptions = options.value.map(({carrier, deliveryType}) => ({carrier, deliveryType})); + + expect(resolvedOptions).toEqual([ + {carrier: CarrierName.PostNl, deliveryType: DeliveryTypeName.Standard}, + {carrier: CARRIER_IDENTIFIER_WITH_CONTRACT, deliveryType: DeliveryTypeName.Standard}, ]); }); });