Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix contract carrier behavior when using non-default package type #230

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`useDeliveryMomentOptions > returns delivery moment options with package type digital_stamp 1`] = `
[
{
"carrier": "postnl",
"label": {
"key": "packageTypeDigitalStamp",
},
"price": 0,
"value": {
"carrier": "postnl",
"date": null,
"deliveryType": "standard",
"packageType": "digital_stamp",
"shipmentOptions": [],
"time": null,
},
},
{
"carrier": "postnl:123",
"label": {
"key": "packageTypeDigitalStamp",
},
"price": 4,
"value": {
"carrier": "postnl:123",
"date": null,
"deliveryType": "standard",
"packageType": "digital_stamp",
"shipmentOptions": [],
"time": null,
},
},
]
`;

exports[`useDeliveryMomentOptions > returns delivery moment options with package type mailbox 1`] = `
[
{
"carrier": "postnl",
"label": {
"key": "packageTypeMailbox",
},
"price": 0,
"value": {
"carrier": "postnl",
"date": null,
"deliveryType": "standard",
"packageType": "mailbox",
"shipmentOptions": [],
"time": null,
},
},
{
"carrier": "postnl:123",
"label": {
"key": "packageTypeMailbox",
},
"price": 5,
"value": {
"carrier": "postnl:123",
"date": null,
"deliveryType": "standard",
"packageType": "mailbox",
"shipmentOptions": [],
"time": null,
},
},
]
`;

exports[`useDeliveryMomentOptions > returns delivery moment options with package type package 1`] = `
[
{
"carrier": "postnl",
"label": {
"key": "deliveryStandardTitle",
},
"price": 3,
"value": {
"carrier": "postnl",
"deliveryType": "standard",
"packageType": "package",
"shipmentOptions": [],
"time": {
"key": "deliveryStandardTitle",
},
},
},
{
"carrier": "postnl:123",
"label": {
"key": "deliveryStandardTitle",
},
"price": 456,
"value": {
"carrier": "postnl:123",
"deliveryType": "standard",
"packageType": "package",
"shipmentOptions": [],
"time": {
"key": "deliveryStandardTitle",
},
},
},
]
`;

exports[`useDeliveryMomentOptions > returns delivery moment options with package type package_small 1`] = `
[
{
"carrier": "postnl",
"label": {
"key": "packageTypePackageSmall",
},
"price": 6,
"value": {
"carrier": "postnl",
"date": null,
"deliveryType": "standard",
"packageType": "package_small",
"shipmentOptions": [],
"time": null,
},
},
{
"carrier": "postnl:123",
"label": {
"key": "packageTypePackageSmall",
},
"price": 0,
"value": {
"carrier": "postnl:123",
"date": null,
"deliveryType": "standard",
"packageType": "package_small",
"shipmentOptions": [],
"time": null,
},
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import {describe, it, expect, beforeEach} from 'vitest';
import {setActivePinia, createPinia} from 'pinia';
import {flushPromises} from '@vue/test-utils';
import {
type SupportedPackageTypeName,
type SelectOption,
KEY_CONFIG,
ConfigSetting,
CarrierSetting,
ConfigSetting,
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';
Expand All @@ -28,8 +26,16 @@ const setup = async (packageType?: SupportedPackageTypeName): Promise<ComputedRe
[CarrierSetting.AllowMorningDelivery]: true,
[CarrierSetting.AllowSignature]: true,
[CarrierSetting.AllowOnlyRecipient]: true,
[CarrierSetting.PriceStandardDelivery]: 3,
[KEY_CARRIER_SETTINGS]: {
[CarrierName.PostNl]: {},
[CarrierName.PostNl]: {
[CarrierSetting.PricePackageTypePackageSmall]: 6,
},
[`${CarrierName.PostNl}:123`]: {
[CarrierSetting.PriceStandardDelivery]: 456,
[CarrierSetting.PricePackageTypeMailbox]: 5,
[CarrierSetting.PricePackageTypeDigitalStamp]: 4,
},
[CarrierName.DhlForYou]: {},
},
// TODO: allow optional key to be passed with undefined as value
Expand All @@ -49,47 +55,23 @@ describe('useDeliveryMomentOptions', () => {
setActivePinia(createPinia());
});

it('returns delivery moment options', async () => {
expect.assertions(8);
const options = await setup();
it.each([
PackageTypeName.Package,
PackageTypeName.Mailbox,
PackageTypeName.DigitalStamp,
PackageTypeName.PackageSmall,
])('returns delivery moment options with package type %s', async (packageType) => {
expect.assertions(2);

expect(options.value).toHaveLength(1);
const options = await setup(packageType);

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

const parsedValue = parseJson<SelectedDeliveryMoment>(option.value);
const resolved = options.value.map((option) => ({
...option,
value: 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([]);
});
expect(resolved).toMatchSnapshot();
});

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([]);
});
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export const useDeliveryMomentOptions = (): ComputedRef<SelectOption<string>[]>
return activeCarriers.value
.filter((carrier) => toValue(carrier.hasAnyDelivery) && toValue(carrier.packageTypes).has(config.packageType))
.map((carrier) => {
const resolvedCarrier = toValue(carrier.carrier);
const carrierIdentifier = toValue(carrier.carrier).identifier;

return {
carrier: resolvedCarrier.name,
carrier: carrierIdentifier,
label: createPackageTypeTranslatable(config.packageType),
price: getDeliveryTypePrice(DELIVERY_TYPE_DEFAULT, resolvedCarrier.name),
price: getDeliveryTypePrice(DELIVERY_TYPE_DEFAULT, carrierIdentifier),
value: JSON.stringify({
carrier: resolvedCarrier.name,
carrier: carrierIdentifier,
date: null,
deliveryType: DELIVERY_TYPE_DEFAULT,
packageType: config.packageType,
Expand Down
Loading