Skip to content

Commit

Permalink
fix: fix non-default package types not working (#216)
Browse files Browse the repository at this point in the history
INT-447
  • Loading branch information
EdieLemoine authored Apr 15, 2024
1 parent d717532 commit 1fb7b7c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {type ComputedRef} from 'vue';
import {describe, it, expect, beforeEach} from 'vitest';
import {setActivePinia, createPinia} from 'pinia';
import {flushPromises} from '@vue/test-utils';
import {
KEY_CONFIG,
ConfigSetting,
CarrierSetting,
KEY_CARRIER_SETTINGS,
DELIVERY_TYPE_DEFAULT,
PACKAGE_TYPE_DEFAULT,
type SupportedPackageTypeName,
type SelectOption,
} from '@myparcel-do/shared';
import {CarrierName, PackageTypeName} from '@myparcel/constants';
import {parseJson} from '../utils';
import {type SelectedDeliveryMoment} from '../types';
import {mockSelectedDeliveryOptions, mockDeliveryOptionsConfig} from '../__tests__';
import {useDeliveryMomentOptions} from './useDeliveryMomentOptions';

const setup = async (packageType?: SupportedPackageTypeName): Promise<ComputedRef<SelectOption<string>[]>> => {
mockDeliveryOptionsConfig({
[KEY_CONFIG]: {
[ConfigSetting.ShowDeliveryDate]: true,
[CarrierSetting.AllowDeliveryOptions]: true,
[CarrierSetting.AllowStandardDelivery]: true,
[CarrierSetting.AllowEveningDelivery]: true,
[CarrierSetting.AllowMorningDelivery]: true,
[CarrierSetting.AllowSignature]: true,
[CarrierSetting.AllowOnlyRecipient]: true,
[KEY_CARRIER_SETTINGS]: {
[CarrierName.PostNl]: {},
[CarrierName.DhlForYou]: {},
},
// TODO: allow optional key to be passed with undefined as value
...(packageType ? {[CarrierSetting.PackageType]: packageType} : {}),
},
});
mockSelectedDeliveryOptions();

const options = useDeliveryMomentOptions();
await flushPromises();

return options;
};

describe('useDeliveryMomentOptions', () => {
beforeEach(() => {
setActivePinia(createPinia());
});

it('returns delivery moment options', async () => {
expect.assertions(8);
const options = await setup();

expect(options.value).toHaveLength(1);

options.value.forEach((option) => {
expect(Object.keys(option)).toEqual(['carrier', 'label', 'price', 'value']);
expect(option.value).toBeTypeOf('string');

const parsedValue = parseJson<SelectedDeliveryMoment>(option.value);

expect(parsedValue.carrier).toBe(CarrierName.PostNl);
expect(parsedValue.time).not.toBeNull();
expect(parsedValue.deliveryType).toBe(DELIVERY_TYPE_DEFAULT);
expect(parsedValue.packageType).toBe(PACKAGE_TYPE_DEFAULT);
expect(parsedValue.shipmentOptions).toEqual([]);
});
});

it.each([PackageTypeName.Mailbox, PackageTypeName.DigitalStamp, PackageTypeName.PackageSmall])(
'returns delivery moment options for different package types',
async (packageType) => {
expect.assertions(8);

const options = await setup(packageType);

expect(options.value).toHaveLength(1);

options.value.forEach((option) => {
expect(Object.keys(option)).toEqual(['carrier', 'label', 'price', 'value']);
expect(option.value).toBeTypeOf('string');

const parsedValue = parseJson<SelectedDeliveryMoment>(option.value);

expect(parsedValue.carrier).toBe(CarrierName.PostNl);
expect(parsedValue.time).toBeNull();
expect(parsedValue.deliveryType).toBe(DELIVERY_TYPE_DEFAULT);
expect(parsedValue.packageType).toBe(packageType);
expect(parsedValue.shipmentOptions).toEqual([]);
});
},
);
});
21 changes: 8 additions & 13 deletions apps/delivery-options/src/composables/useDeliveryMomentOptions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {type ComputedRef, computed, toValue} from 'vue';
import {
type SelectOption,
PACKAGE_TYPE_DEFAULT,
DELIVERY_TYPE_DEFAULT,
type SupportedShipmentOptionName,
} from '@myparcel-do/shared';
import {type SelectOption, PACKAGE_TYPE_DEFAULT, DELIVERY_TYPE_DEFAULT} from '@myparcel-do/shared';
import {getDeliveryTypePrice, createPackageTypeTranslatable} from '../utils';
import {useConfigStore} from '../stores';
import {SHOWN_SHIPMENT_OPTIONS} from '../data';
import {useResolvedDeliveryMoments} from './useResolvedDeliveryMoments';
import {useActiveCarriers} from './useActiveCarriers';

export const useDeliveryMomentOptions = (): ComputedRef<SelectOption[]> => {
export const useDeliveryMomentOptions = (): ComputedRef<SelectOption<string>[]> => {
const config = useConfigStore();
const deliveryMoments = useResolvedDeliveryMoments();
const activeCarriers = useActiveCarriers();
Expand All @@ -21,12 +16,14 @@ export const useDeliveryMomentOptions = (): ComputedRef<SelectOption[]> => {
return activeCarriers.value
.filter((carrier) => toValue(carrier.hasAnyDelivery) && toValue(carrier.packageTypes).has(config.packageType))
.map((carrier) => {
const resolvedCarrier = toValue(carrier.carrier);

return {
carrier: carrier.name,
carrier: resolvedCarrier.name,
label: createPackageTypeTranslatable(config.packageType),
price: getDeliveryTypePrice(DELIVERY_TYPE_DEFAULT, carrier.name),
price: getDeliveryTypePrice(DELIVERY_TYPE_DEFAULT, resolvedCarrier.name),
value: JSON.stringify({
carrier: carrier.name,
carrier: resolvedCarrier.name,
date: null,
deliveryType: DELIVERY_TYPE_DEFAULT,
packageType: config.packageType,
Expand All @@ -48,9 +45,7 @@ export const useDeliveryMomentOptions = (): ComputedRef<SelectOption[]> => {
date: option.date,
deliveryType: option.deliveryType,
packageType: option.packageType,
shipmentOptions: option.shipmentOptions.filter((option) =>
SHOWN_SHIPMENT_OPTIONS.includes(option.name as SupportedShipmentOptionName),
),
shipmentOptions: option.shipmentOptions.filter((option) => SHOWN_SHIPMENT_OPTIONS.includes(option.name)),
}),
};
});
Expand Down
5 changes: 4 additions & 1 deletion apps/delivery-options/src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const FIELD_SHIPMENT_OPTIONS = 'shipmentOptions';
*/
export const FIELD_PICKUP_LOCATION = 'pickupLocation';

export const SHOWN_SHIPMENT_OPTIONS = [ShipmentOptionName.Signature, ShipmentOptionName.OnlyRecipient] as const;
export const SHOWN_SHIPMENT_OPTIONS = Object.freeze<ShipmentOptionName[]>([
ShipmentOptionName.Signature,
ShipmentOptionName.OnlyRecipient,
]);

export const MAP_MARKER_CLASS_ACTIVE = 'active';

Expand Down
7 changes: 4 additions & 3 deletions libs/shared/src/types/options.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {type AnyTranslatable} from './language.types';
import {type CarrierIdentifier} from './config.types';
import {type Translation} from './common.types';

export type OptionsProps<T extends SelectOptionValue> = {
options: SelectOption<T>[];
Expand All @@ -20,7 +19,9 @@ interface BaseSelectOption<Value extends SelectOptionValue = SelectOptionValue>

export type SelectOptionValue = string | object | boolean;

export interface SelectOption<Value extends SelectOptionValue = SelectOptionValue, T extends Translation = Translation>
extends BaseSelectOption<Value> {
export interface SelectOption<
Value extends SelectOptionValue = SelectOptionValue,
T extends AnyTranslatable = AnyTranslatable,
> extends BaseSelectOption<Value> {
label: T;
}

0 comments on commit 1fb7b7c

Please sign in to comment.