Skip to content

Commit

Permalink
fix: fix custom carriers not showing in fake delivery (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored Jun 28, 2024
1 parent 1c71948 commit 907fc17
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,58 +21,86 @@ import {
} from '../__tests__';
import {useResolvedDeliveryOptions} from './useResolvedDeliveryOptions';

const mockSetup = async (): Promise<void> => {
const CARRIER_IDENTIFIER_WITH_CONTRACT = `${CarrierName.PostNl}:1234`;

const doTestSetup = async (config: RecursivePartial<InputDeliveryOptionsConfiguration> = {}): Promise<void> => {
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.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();
};

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},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DELIVERY_TYPE_DEFAULT,
type AnyTranslatable,
createUntranslatable,
resolveCarrierName,
} from '@myparcel-do/shared';
import {type Replace} from '@myparcel/ts-utils';
import {type Timestamp, type DeliveryOption, type DeliveryPossibility, type DeliveryTimeFrame} from '@myparcel/sdk';
Expand Down Expand Up @@ -104,7 +105,8 @@ export const useResolvedDeliveryOptions = useMemoize(() => {

const final = computed(() => {
return asyncComputed.value.filter((option) => {
const carrier = carriers.value.find(({carrier}) => carrier.value.name === option.carrier);
const carrierName = resolveCarrierName(option.carrier);
const carrier = carriers.value.find(({carrier}) => carrier.value.name === carrierName);

return carrier?.deliveryTypes.value.has(option.deliveryType);
});
Expand Down

0 comments on commit 907fc17

Please sign in to comment.