/** * Pickup point delivery on the ship page: which place a booking sends, and when booking waits. */ import { orderToCheckoutValidatePayload } from '../features/order-ship/utils/orderToCheckoutValidatePayload'; import ordersToCheckoutValidatePayload from '../features/orders/utils/ordersToCheckoutValidatePayload'; import { wooMetadata } from '../features/order-ship/utils/wooMetadata'; import { bookingPickupPoint, hasPickupLookup, isPickupQuote, pickupKind, pickupPointText, withPickupPoint, } from '../features/order-ship/utils/pickupPoint'; import { otherCarrierChoice, pickupPointStatus, replacedChoice, shortPlace, } from '../features/order-ship/booking/pickup-point-section'; import type { Order, OrderPickupPoint, PluginSettings, Quote } from '../types'; jest.mock('@wordpress/api-fetch', () => jest.fn()); const makeQuote = (tags: string[], provider = 'INPOST', slug = 'inpost_lockertolocker_2815') => ({ service: { slug, dropOffProviderGroup: provider }, tags, availableExtras: [], dates: [], }) as unknown as Quote; const locker = makeQuote(['shopdelivery', 'locker', 'dropoff']); const evriShop = makeQuote(['shopdelivery', 'dropoff'], 'MYHRMS', 'myhermes-parcelshop-to-parcelshop'); const home = makeQuote(['dropoff'], 'INPOST', 'inpost_shopify'); const point = (id: number, provider = 'INPOST'): OrderPickupPoint => ({ id, provider, name: `InPost Locker ${id}`, address1: 'Next to the tills', address2: 'Leeds', postcode: 'LS1 5BQ', isLocker: true, }); const settings = { shipping: { country: 'GB', contactName: 'Shop', email: 's@example.com', town: 'London', property: '1', postcode: 'SW1A 1AA', street: 'Street' }, tax: {}, packages: [], defaultPackageId: '', overrides: { defaultWeightKg: 2 }, } as unknown as PluginSettings; const makeOrder = (pickupPoint: OrderPickupPoint | null = null) => ({ id: 34, shipping: { name: 'Test Customer', address_1: '1 Test Street', city: 'Leeds', postcode: 'LS2 7HY', country: 'GB', phone: '' }, customer: { name: 'Test Customer', email: 'test@example.com' }, items: [], units: { weight: 'kg', dimension: 'cm' }, parcel: { length: 30, width: 20, height: 10, weight: 1 }, shipping_method: { id: 'parcel2go_live', title: 'InPost Locker Pick-up', checkout: pickupPoint ? { serviceSlug: 'inpost_lockertolocker_2815', courierSlug: 'inpost', label: '', quotedNet: 199, quotedGross: 239, quotedAt: null, customerPaid: 263, customerTax: 0, pickupPoint } : null, }, }) as unknown as Order; const shopId = (order: Order, quote: Quote) => orderToCheckoutValidatePayload(order, settings, quote, [])?.items[0].destination.useAddress.shopId; describe('booking a pickup point service', () => { // Positive — the customer's locker goes to Parcel2Go it('sends the customer\'s locker as destination.useAddress.shopId', () => { expect(shopId(makeOrder(point(905283)), locker)).toBe(905283); }); // Positive — the merchant's pick replaces it for this booking only it('sends the merchant\'s pick', () => { const order = makeOrder(point(905283)); const picked = withPickupPoint(order, point(453800)); expect(shopId(picked, locker)).toBe(453800); expect(shopId(order, locker)).toBe(905283); }); // Positive — an order without a checkout choice (e.g. Flat rate) can take a pick it('adds a pick to an order without a checkout choice', () => { const picked = withPickupPoint(makeOrder(), point(7, 'MYHRMS')); expect(shopId(picked, evriShop)).toBe(7); }); // Negative — home delivery and the other carrier's service send no shopId it('sends no shopId for home delivery or another provider', () => { expect(shopId(makeOrder(point(905283)), home)).toBeUndefined(); expect(shopId(makeOrder(point(905283)), evriShop)).toBeUndefined(); expect(shopId(makeOrder(), locker)).toBeUndefined(); }); it('knows pickup services by their tag', () => { expect(isPickupQuote(locker)).toBe(true); expect(isPickupQuote(home)).toBe(false); expect(isPickupQuote(null)).toBe(false); expect(bookingPickupPoint(makeOrder(point(1, 'inpost')), locker)?.id).toBe(1); }); it('writes the place as one line', () => { expect(pickupPointText(point(1))).toBe('InPost Locker 1, Next to the tills, Leeds, LS1 5BQ'); expect(pickupPointText({ name: 'Shop', address1: '', address2: ' ', postcode: 'LS1 1AA' })).toBe('Shop, LS1 1AA'); }); }); describe('bulk booking a pickup point service', () => { const bulkShopIds = (orders: Order[]) => ordersToCheckoutValidatePayload({ orders, settings })?.items.map( (item) => item.destination.useAddress.shopId ); // Positive — each order goes to its own customer's locker it('sends each order\'s locker', () => { const a = { ...makeOrder(point(905283)), id: 34, cheapestQuote: locker } as Order; const b = { ...makeOrder(point(453800)), id: 36, cheapestQuote: locker } as Order; expect(bulkShopIds([a, b])).toEqual([905283, 453800]); }); // Negative — home delivery orders send none it('sends no shopId for home delivery', () => { const order = { ...makeOrder(point(905283)), cheapestQuote: home } as Order; expect(bulkShopIds([order])).toEqual([undefined]); }); }); describe('wooMetadata', () => { beforeEach(() => { (window as unknown as { parcel2goShipping: Record }).parcel2goShipping = { siteUrl: 'https://shop.example' }; }); afterEach(() => { delete (window as unknown as { parcel2goShipping?: unknown }).parcel2goShipping; }); // Positive — the shop, the booking ref and the service the customer chose at checkout it('adds the checkout service when the order has one', () => { expect(wooMetadata('34', makeOrder(point(905283)))).toEqual({ wooShop: 'https://shop.example', wooOrderId: '34', wooCheckoutService: 'inpost_lockertolocker_2815', }); }); // Negative — orders without a checkout rate (flat rate, free shipping), or no order, leave it out it('leaves the checkout service out otherwise', () => { const expected = { wooShop: 'https://shop.example', wooOrderId: 'bulk' }; expect(wooMetadata('bulk', makeOrder())).toEqual(expected); expect(wooMetadata('bulk', undefined)).toEqual(expected); expect(wooMetadata('bulk', { ...makeOrder(), shipping_method: undefined } as unknown as Order)).toEqual(expected); }); // Positive — bulk items carry their order's id as ref, which finds each item's order it('bulk items are referenced by their order id', () => { const a = { ...makeOrder(point(905283)), id: 34, cheapestQuote: locker } as Order; const b = { ...makeOrder(), id: 36, cheapestQuote: home } as Order; expect(ordersToCheckoutValidatePayload({ orders: [a, b], settings })?.items.map((item) => item.ref)).toEqual(['34', '36']); }); }); describe('replacedChoice', () => { // Positive — the merchant changed the customer's locker to another of the same carrier it('returns the customer\'s place when the booking goes elsewhere', () => { expect(replacedChoice(point(905283), point(453800))?.id).toBe(905283); expect(replacedChoice(point(905283, 'inpost'), point(453800, 'INPOST'))?.id).toBe(905283); }); // Negative — same place, no checkout choice, or another carrier (that case has its own wording) it('returns null otherwise', () => { expect(replacedChoice(point(905283), point(905283))).toBeNull(); expect(replacedChoice(null, point(453800))).toBeNull(); expect(replacedChoice(point(905283), null)).toBeNull(); expect(replacedChoice(point(905283), point(7, 'MYHRMS'))).toBeNull(); }); // Positive — after a change, booking sends the merchant's place it('books the changed place', () => { expect(shopId(withPickupPoint(makeOrder(point(905283)), point(453800)), locker)).toBe(453800); }); }); describe('shortPlace', () => { // Positive — name and postcode for the reminders it('writes the name and postcode', () => { expect(shortPlace(point(1))).toBe('InPost Locker 1 (LS1 5BQ)'); }); // Negative — no postcode: the name alone it('leaves out an empty postcode', () => { expect(shortPlace({ name: ' Post Office ', postcode: ' ' })).toBe('Post Office'); }); }); describe('otherCarrierChoice', () => { // Positive — the customer chose an InPost locker and the merchant books Evri ParcelShop to ParcelShop it('returns the customer\'s place when booking the other carrier', () => { expect(otherCarrierChoice(point(453800), 'MYHRMS')?.id).toBe(453800); }); // Negative — same carrier (any case), no checkout choice, or no provider it('returns null otherwise', () => { expect(otherCarrierChoice(point(453800), 'INPOST')).toBeNull(); expect(otherCarrierChoice(point(453800, 'inpost'), 'INPOST')).toBeNull(); expect(otherCarrierChoice(null, 'MYHRMS')).toBeNull(); expect(otherCarrierChoice(point(453800), '')).toBeNull(); }); }); describe('pickupPointStatus', () => { const places = [{ id: 905283, name: 'A', address1: '', address2: '', postcode: '', distanceMiles: 0.3, isLocker: true }]; // Positive it('lets booking go ahead for home delivery or a place still offered', () => { expect(pickupPointStatus(makeOrder(point(905283)), home, null)).toBe('ok'); expect(pickupPointStatus(makeOrder(point(905283)), locker, places)).toBe('ok'); }); // Negative — waits while checking, and when the place has gone or there is none it('waits for a place', () => { expect(pickupPointStatus(makeOrder(point(905283)), locker, null)).toBe('checking'); expect(pickupPointStatus(makeOrder(point(1)), locker, places)).toBe('needs-pick'); expect(pickupPointStatus(makeOrder(), locker, places)).toBe('needs-pick'); expect(pickupPointStatus(makeOrder(point(905283)), evriShop, places)).toBe('needs-pick'); }); // Positive — any carrier's pickup service can be booked it('looks up places for any carrier', () => { const dpdPickup = makeQuote(['shopdelivery'], 'DPD', 'dpd-pickup'); expect(hasPickupLookup(dpdPickup)).toBe(true); expect(hasPickupLookup(locker)).toBe(true); expect(hasPickupLookup(evriShop)).toBe(true); expect(pickupPointStatus(makeOrder(), dpdPickup, null)).toBe('needs-pick'); }); // Negative — no provider, or an invalid one: can't be booked here it('says when a pickup service cannot be booked here', () => { const noProvider = makeQuote(['shopdelivery'], '', 'mystery-pickup'); const badProvider = makeQuote(['shopdelivery'], '../X', 'odd-pickup'); expect(pickupPointStatus(makeOrder(), noProvider, null)).toBe('unsupported'); expect(pickupPointStatus(makeOrder(), badProvider, null)).toBe('unsupported'); expect(hasPickupLookup(home)).toBe(false); }); it('words lockers and shops from the locker tag', () => { expect(pickupKind(locker)).toBe('locker'); expect(pickupKind(evriShop)).toBe('shop'); expect(pickupKind(null)).toBe('shop'); }); });