193 lines
5.4 KiB
TypeScript
193 lines
5.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Neo region store invariants: the selection always belongs to the active
|
|
* region, a region change clears it, and the two flow flags are exclusive.
|
|
*/
|
|
import type { RegionSummary } from '@packages/shared'
|
|
import { useNeoRegionStore } from '@/src/store/neoRegionStore'
|
|
import type { NeoAddress } from '@/src/lib/neo-address'
|
|
|
|
const R1: RegionSummary = {
|
|
id: 1,
|
|
name: 'Region A',
|
|
description: null,
|
|
effectiveBoundId: 7,
|
|
center: { lat: 16.7, lng: 77.9 },
|
|
}
|
|
const R2: RegionSummary = {
|
|
id: 2,
|
|
name: 'Region B',
|
|
description: null,
|
|
effectiveBoundId: 8,
|
|
center: { lat: 18.0, lng: 79.0 },
|
|
}
|
|
|
|
function address(overrides: Partial<NeoAddress> = {}): NeoAddress {
|
|
return {
|
|
id: 12,
|
|
regionId: 2,
|
|
boundId: 8,
|
|
name: 'Work',
|
|
phone: '9876543211',
|
|
addressLine1: '45 Office Street',
|
|
addressLine2: null,
|
|
city: 'Hyderabad',
|
|
state: 'Telangana',
|
|
isDefault: false,
|
|
latitude: 18.1,
|
|
longitude: 79.1,
|
|
googleMapsUrl: null,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
const A_IN_B = address()
|
|
|
|
function reset() {
|
|
useNeoRegionStore.setState({
|
|
regionId: null,
|
|
boundId: null,
|
|
regionName: null,
|
|
coords: null,
|
|
origin: null,
|
|
regions: [],
|
|
addresses: [],
|
|
selectedAddressId: null,
|
|
needsAddressFormFlow: false,
|
|
needsAddressConfirmation: false,
|
|
})
|
|
}
|
|
|
|
describe('neoRegionStore', () => {
|
|
beforeEach(reset)
|
|
|
|
it('applyRegion keeps a selection that belongs to the region', () => {
|
|
useNeoRegionStore.setState({ addresses: [A_IN_B], regions: [R2] })
|
|
|
|
useNeoRegionStore.getState().applyRegion({
|
|
regionId: 2,
|
|
boundId: 8,
|
|
regionName: 'Region B',
|
|
coords: { latitude: 18, longitude: 79 },
|
|
origin: 'device',
|
|
selectedAddressId: 12,
|
|
})
|
|
|
|
const state = useNeoRegionStore.getState()
|
|
expect(state.selectedAddressId).toBe(12)
|
|
expect(state.origin).toBe('address')
|
|
expect(state.boundId).toBe(8)
|
|
expect(state.coords).toEqual({ latitude: 18.1, longitude: 79.1 })
|
|
})
|
|
|
|
it('applyRegion drops a selection that belongs to another region', () => {
|
|
useNeoRegionStore.setState({ addresses: [A_IN_B], regions: [R1] })
|
|
|
|
useNeoRegionStore.getState().applyRegion({
|
|
regionId: 1,
|
|
boundId: 7,
|
|
regionName: 'Region A',
|
|
coords: { latitude: 16.7, longitude: 77.9 },
|
|
origin: 'device',
|
|
selectedAddressId: 12,
|
|
})
|
|
|
|
const state = useNeoRegionStore.getState()
|
|
expect(state.selectedAddressId).toBeNull()
|
|
expect(state.origin).toBe('device')
|
|
})
|
|
|
|
it('changeRegion switches without an address and clears the selection', async () => {
|
|
useNeoRegionStore.setState({
|
|
regionId: 1,
|
|
boundId: 7,
|
|
regionName: 'Region A',
|
|
coords: { latitude: 16.7, longitude: 77.9 },
|
|
regions: [R1, R2],
|
|
addresses: [A_IN_B],
|
|
selectedAddressId: 12,
|
|
})
|
|
|
|
await useNeoRegionStore.getState().changeRegion(2)
|
|
|
|
const state = useNeoRegionStore.getState()
|
|
expect(state.regionId).toBe(2)
|
|
expect(state.boundId).toBe(8)
|
|
expect(state.regionName).toBe('Region B')
|
|
expect(state.selectedAddressId).toBeNull()
|
|
expect(state.origin).toBe('manual')
|
|
})
|
|
|
|
it('selectAddress adopts the address region, bound and coords', async () => {
|
|
useNeoRegionStore.setState({
|
|
regionId: 1,
|
|
boundId: 7,
|
|
regionName: 'Region A',
|
|
coords: { latitude: 16.7, longitude: 77.9 },
|
|
regions: [R1, R2],
|
|
addresses: [A_IN_B],
|
|
})
|
|
|
|
await useNeoRegionStore.getState().selectAddress(12)
|
|
|
|
const state = useNeoRegionStore.getState()
|
|
expect(state.regionId).toBe(2)
|
|
expect(state.boundId).toBe(8)
|
|
expect(state.selectedAddressId).toBe(12)
|
|
expect(state.origin).toBe('address')
|
|
expect(state.coords).toEqual({ latitude: 18.1, longitude: 79.1 })
|
|
})
|
|
|
|
it('setAddresses clears a selection that vanished from the list', () => {
|
|
useNeoRegionStore.setState({
|
|
regionId: 2,
|
|
boundId: 8,
|
|
addresses: [A_IN_B],
|
|
selectedAddressId: 12,
|
|
})
|
|
|
|
useNeoRegionStore.getState().setAddresses([])
|
|
|
|
expect(useNeoRegionStore.getState().selectedAddressId).toBeNull()
|
|
})
|
|
|
|
it('setAddresses filters addresses without a region', () => {
|
|
useNeoRegionStore.getState().setAddresses([
|
|
A_IN_B,
|
|
address({ id: 13, regionId: null, boundId: null }),
|
|
])
|
|
|
|
expect(useNeoRegionStore.getState().addresses.map((item) => item.id)).toEqual([12])
|
|
})
|
|
|
|
it('logout clears addresses and selection but keeps the region', () => {
|
|
useNeoRegionStore.setState({
|
|
regionId: 2,
|
|
boundId: 8,
|
|
regionName: 'Region B',
|
|
coords: { latitude: 18, longitude: 79 },
|
|
addresses: [A_IN_B],
|
|
selectedAddressId: 12,
|
|
})
|
|
|
|
useNeoRegionStore.getState().resetForLogout()
|
|
|
|
const state = useNeoRegionStore.getState()
|
|
expect(state.regionId).toBe(2)
|
|
expect(state.addresses).toEqual([])
|
|
expect(state.selectedAddressId).toBeNull()
|
|
})
|
|
|
|
it('the two flow flags are mutually exclusive', () => {
|
|
useNeoRegionStore.getState().openAddressFlow()
|
|
expect(useNeoRegionStore.getState().needsAddressFormFlow).toBe(true)
|
|
expect(useNeoRegionStore.getState().needsAddressConfirmation).toBe(false)
|
|
|
|
useNeoRegionStore.getState().setNeedsConfirmation(true)
|
|
expect(useNeoRegionStore.getState().needsAddressConfirmation).toBe(true)
|
|
expect(useNeoRegionStore.getState().needsAddressFormFlow).toBe(false)
|
|
|
|
useNeoRegionStore.getState().setNeedsConfirmation(false)
|
|
expect(useNeoRegionStore.getState().needsAddressConfirmation).toBe(false)
|
|
})
|
|
})
|