/** * The Checkout rates service picker lists one entry per country quoted, plus Rest of the world. */ import { zonesToBrowse } from '../features/settings/utils/checkoutRatesZones'; import type { DefaultServiceZone } from '../types/default-service'; function zone(id: number, name: string, countryCodes: string[], methods = 1): DefaultServiceZone { return { id, name, countries: countryCodes, countryCodes, countryCount: countryCodes.length, methods: Array.from({ length: methods }, (_, i) => ({ id: `flat_rate_${i}`, title: 'Flat rate', price: '£1', conditions: [], services: [] })), } as DefaultServiceZone; } describe('zonesToBrowse', () => { it('lists each zone by name, quoted to its first country', () => { expect( zonesToBrowse([zone(1, 'United Kingdom', ['GB']), zone(2, 'EU zones', ['FR', 'DE', 'ES'])]) ).toEqual([ { value: 'country:GB', label: 'United Kingdom', country: 'GB' }, { value: 'country:FR', label: 'EU zones', country: 'FR' }, ]); }); it('shows zones quoted to the same country once (they give the same services)', () => { // The test store: four "United Kingdom (UK)" zones, all GB. const uk = [1, 2, 3, 4].map((id) => zone(id, 'United Kingdom (UK)', ['GB'])); expect(zonesToBrowse([...uk, zone(5, 'Europe', ['FR'])]).map((o) => o.label)).toEqual([ 'United Kingdom (UK)', 'Europe', ]); }); it('names every zone sharing a country', () => { expect( zonesToBrowse([zone(1, 'UK Mainland', ['GB']), zone(2, 'UK Highlands', ['GB'])]) ).toEqual([{ value: 'country:GB', label: 'UK Mainland / UK Highlands', country: 'GB' }]); }); it('tells apart same-name zones for different countries', () => { expect( zonesToBrowse([zone(1, 'Europe', ['FR', 'BE']), zone(2, 'europe ', ['DE']), zone(3, 'United Kingdom', ['GB'])]).map( (o) => o.label ) ).toEqual(['Europe (FR)', 'europe (DE)', 'United Kingdom']); }); it('offers Rest of the world last, with a country to pick, when it has methods', () => { const options = zonesToBrowse([zone(0, 'Locations not covered by your other zones', []), zone(1, 'UK', ['GB'])]); expect(options[options.length - 1]).toEqual({ value: 'rest', label: 'Rest of the world', country: '', needsCountry: true }); }); it('leaves out Rest of the world when it has no methods', () => { expect(zonesToBrowse([zone(0, 'Rest of the world', [], 0)])).toEqual([]); }); it('gives nothing for a store without zones', () => { expect(zonesToBrowse([])).toEqual([]); }); });