import { dropCountOf, dropSummary, isMultiDrop, plural } from '@/lib/bookingDrops'; describe('dropCountOf', () => { // The console can ship before or after the backend that adds these fields. // An absent count must read as 0 so every multi-drop behaviour stays off and // the page renders exactly as it did. it('treats an absent destinationcount as zero', () => { expect(dropCountOf({})).toBe(0); expect(dropCountOf({ destinationcount: undefined })).toBe(0); expect(dropCountOf(undefined)).toBe(0); expect(dropCountOf(null)).toBe(0); }); it('reads the count the backend sent', () => { expect(dropCountOf({ destinationcount: 3 })).toBe(3); expect(dropCountOf({ destinationcount: 1 })).toBe(1); }); // A console-created booking has no bookingdestinations rows at all. it('keeps a console-created booking at zero', () => { expect(dropCountOf({ destinationcount: 0 })).toBe(0); }); }); describe('isMultiDrop', () => { // THE IMPORTANT ONE. This gate is what stops the Orders row rendering the // booking's own consignmentstatus — which is the FIRST leg's status wearing // the whole booking's name — as the status of a three-drop pickup. it('is true only above one destination', () => { expect(isMultiDrop({ destinationcount: 2 })).toBe(true); expect(isMultiDrop({ destinationcount: 3 })).toBe(true); }); it('is false for a single drop, so the row keeps todays behaviour', () => { expect(isMultiDrop({ destinationcount: 1 })).toBe(false); }); it('is false when the backend has not shipped the field yet', () => { expect(isMultiDrop({})).toBe(false); expect(isMultiDrop({ bookingid: 7, status: 'converted_to_consignment' })).toBe(false); }); it('is false for a console-created booking reporting zero', () => { expect(isMultiDrop({ destinationcount: 0 })).toBe(false); }); }); describe('dropSummary', () => { it('reads as the handoff specifies', () => { expect(dropSummary({ destinationcount: 3, totalpackagecount: 4 })).toBe( '3 destinations · 4 packages' ); }); // Every destination carries at least one package, so a zero package total is // a stale or partial response rather than a real booking. Falling back to the // destination count keeps the line truthful instead of printing "0 packages". it('falls back to the destination count when the package sum is missing', () => { expect(dropSummary({ destinationcount: 3 })).toBe('3 destinations · 3 packages'); expect(dropSummary({ destinationcount: 2, totalpackagecount: 0 })).toBe( '2 destinations · 2 packages' ); }); it('pluralises both nouns independently', () => { expect(dropSummary({ destinationcount: 2, totalpackagecount: 1 })).toBe( '2 destinations · 1 package' ); expect(dropSummary({ destinationcount: 1, totalpackagecount: 2 })).toBe( '1 destination · 2 packages' ); }); }); describe('plural', () => { it('keeps the singular at exactly one', () => { expect(plural(1, 'package')).toBe('1 package'); expect(plural(0, 'package')).toBe('0 packages'); expect(plural(2, 'destination')).toBe('2 destinations'); }); });