844 lines
34 KiB
TypeScript
844 lines
34 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 25 — "Region and location" (`tests/lib/use-region.test.tsx`).
|
|
*
|
|
* REGION-01..15: the `useRegion()` startup region-resolution state machine.
|
|
*
|
|
* `useRegion` keeps the launch decision in module-level singletons
|
|
* (`startupCheckDone` / `startupLaunchRegionId`), so every case runs against a
|
|
* freshly loaded module registry (`jest.isolateModulesAsync`) together with its
|
|
* own copies of the harness modules (stores, trpc mock, fetch-json mock, ...).
|
|
* The trpc mock re-seeds its defaults on import, so nothing leaks across cases.
|
|
*
|
|
* Re-importing `@testing-library/react-native` per case would re-run its
|
|
* top-level `afterEach`/`afterAll` registration, which Jest forbids inside a
|
|
* running test; `RNTL_SKIP_AUTO_CLEANUP` suppresses that and we clean up by hand.
|
|
*/
|
|
jest.mock('@/src/contexts/AuthContext', () => require('../mocks/auth-context'))
|
|
|
|
/**
|
|
* `installTrpcNetworkMock()` (tests/setup.ts) is installed from the ROOT module
|
|
* registry, so `global.fetch` answers from the root copy of the procedure
|
|
* registry. Re-requiring the registry inside `jest.isolateModulesAsync` would
|
|
* give this file a copy that the network mock never sees; procedure fixtures and
|
|
* call recording must go through the root instance.
|
|
*/
|
|
const networkTrpc = require('../mocks/trpc-client')
|
|
|
|
async function withFreshModules(fn: (ctx: any) => Promise<void>) {
|
|
await jest.isolateModulesAsync(async () => {
|
|
const previousSkip = process.env.RNTL_SKIP_AUTO_CLEANUP
|
|
process.env.RNTL_SKIP_AUTO_CLEANUP = 'true'
|
|
|
|
const React = require('react')
|
|
const rtl = require('@testing-library/react-native')
|
|
require('../helpers/reset').resetAllMocks()
|
|
// Stored procedures/calls live on the root registry (see above).
|
|
networkTrpc.resetTrpcMocks()
|
|
|
|
if (previousSkip === undefined) delete process.env.RNTL_SKIP_AUTO_CLEANUP
|
|
else process.env.RNTL_SKIP_AUTO_CLEANUP = previousSkip
|
|
|
|
const ctx: any = {
|
|
React,
|
|
rtl,
|
|
auth: require('../mocks/auth-context'),
|
|
locationMock: require('../mocks/expo-location-mock'),
|
|
trpc: networkTrpc,
|
|
testUtils: require('../helpers/test-utils'),
|
|
fixtures: require('../fixtures'),
|
|
secure: require('../mocks/secure-store-mock'),
|
|
fetchJson: require('../mocks/fetch-json'),
|
|
regionStore: require('@/src/store/regionStore').useRegionStore,
|
|
addressStore: require('@/src/store/addressStore').useAddressStore,
|
|
regionCache: require('@/src/lib/region-cache'),
|
|
locationStore: require('@/src/store/locationStore').useLocationStore,
|
|
useRegion: require('@/src/hooks/useRegion').useRegion,
|
|
}
|
|
|
|
ctx.renderRegion = () => {
|
|
const result: { current: any } = { current: null }
|
|
const Probe = () => {
|
|
result.current = ctx.useRegion()
|
|
return null
|
|
}
|
|
const element = React.createElement(Probe)
|
|
const view = ctx.testUtils.renderWithProviders(element, { auth: false })
|
|
// Re-render through a fresh element so the probe re-reads the mocked auth
|
|
// context (React bails out when given the identical element twice).
|
|
const rerender = () => view.rerender(React.createElement(Probe))
|
|
return { result, view, element, rerender }
|
|
}
|
|
|
|
try {
|
|
await fn(ctx)
|
|
} finally {
|
|
rtl.cleanup()
|
|
}
|
|
})
|
|
}
|
|
|
|
describe('useRegion', () => {
|
|
it('REGION-01: with no region and no coords it resolves then falls back to the chooser', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
const { result, view } = ctx.renderRegion()
|
|
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
await ctx.testUtils.flush(3)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
expect(result.current.regionId).toBeNull()
|
|
expect(result.current.locationStatus).toBe('denied')
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-02: a resolved region with no saved region is adopted without clearing the cart', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { CART_REG } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.secure.__set('cart_items', CART_REG)
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
// Adopted as the first region of the launch -> no cart clear, no switch.
|
|
expect(ctx.regionStore.getState().regionGeneration).toBe(1)
|
|
expect(ctx.secure.__snapshot().cart_items).toBe(CART_REG)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-03: a resolved different region with no default confirms the switch', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2 } = ctx.fixtures
|
|
ctx.regionStore.getState().setRegion(REGION_1, 7)
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: { region: REGION_2, bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' } },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(2)
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBe(2)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-04: a resolved same region with a new bound updates the bound', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1 } = ctx.fixtures
|
|
ctx.regionStore.getState().setRegion(REGION_1, 7)
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: { region: REGION_1, bound: { id: 9, regionId: 1, priority: 1, name: 'Bound 9' } },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(9)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-05: a signed-in default in the resolved region adopts the default bound', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
// Default address A1 -> region 1, bound 7.
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-06: a default in another region with a differently resolved region clears for the chooser', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2 } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.regionStore.getState().setRegion(REGION_1, 7)
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: { region: REGION_2, bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' } },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBeNull()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-07: no serviceable region with a default adopts the default', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-08: it waits while auth is loading, then settles once auth resolves', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: true })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
const { result, view, rerender } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(3)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
ctx.auth.__setAuthState({ isLoading: false })
|
|
// Auth is read from the mocked context only on render, so re-render the
|
|
// probe to pick the settled auth state up.
|
|
await ctx.rtl.act(async () => {
|
|
rerender()
|
|
})
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-09: it waits while the default address is loading, then settles', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { A1 } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
// Hold the default-address response instead of using `mode: 'pending'`: the
|
|
// request is still recorded and the address resolves on release, so the
|
|
// same mount can settle once it lands.
|
|
const networkMock = (global as any).fetch
|
|
let releaseAddress!: () => void
|
|
const addressGate = new Promise<void>((resolve) => {
|
|
releaseAddress = resolve
|
|
})
|
|
;(global as any).fetch = jest.fn(async (input: any, init: any) => {
|
|
const response = await networkMock(input, init)
|
|
if (String(input).includes('user.address.getDefaultAddress')) await addressGate
|
|
return response
|
|
})
|
|
|
|
try {
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', {
|
|
mode: 'success',
|
|
data: { data: A1 },
|
|
})
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(3)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
// The address read finishes.
|
|
await ctx.rtl.act(async () => {
|
|
releaseAddress()
|
|
})
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
view.unmount()
|
|
} finally {
|
|
;(global as any).fetch = networkMock
|
|
}
|
|
})
|
|
})
|
|
|
|
it('REGION-10: a manual region pick before the fix resolves is respected', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, BOUND_1 } = ctx.fixtures
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
mode: 'success',
|
|
data: { region: REGION_1, bound: BOUND_1 },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
|
|
// Hold the resolve response so the manual pick happens before the fix
|
|
// lands; releasing it then delivers the late (region 1) fix.
|
|
const networkMock = (global as any).fetch
|
|
let releaseResolve!: () => void
|
|
const resolveGate = new Promise<void>((resolve) => {
|
|
releaseResolve = resolve
|
|
})
|
|
;(global as any).fetch = jest.fn(async (input: any, init: any) => {
|
|
const response = await networkMock(input, init)
|
|
if (String(input).includes('common.resolveRegionByLocation')) await resolveGate
|
|
return response
|
|
})
|
|
|
|
try {
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(3)
|
|
expect(result.current.regionId).toBeNull()
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
|
|
await ctx.rtl.act(async () => {
|
|
ctx.regionStore.getState().setRegion(REGION_2, 8)
|
|
})
|
|
|
|
// The late fix now resolves to region 1; the manual pick must win.
|
|
await ctx.rtl.act(async () => {
|
|
releaseResolve()
|
|
})
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(2)
|
|
view.unmount()
|
|
} finally {
|
|
;(global as any).fetch = networkMock
|
|
}
|
|
})
|
|
})
|
|
|
|
it('REGION-11: a resolve that hangs 8s settles via the safety-net timer', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', { mode: 'pending' })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
jest.useFakeTimers()
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.rtl.act(async () => {
|
|
await Promise.resolve()
|
|
})
|
|
await ctx.rtl.act(async () => {
|
|
await Promise.resolve()
|
|
})
|
|
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
|
|
await ctx.rtl.act(async () => {
|
|
jest.advanceTimersByTime(8000)
|
|
})
|
|
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
jest.useRealTimers()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-12: a saved region absent from listRegions is left untouched (current behaviour)', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGIONS } = ctx.fixtures
|
|
ctx.trpc.setProcedure('common.listRegions', { data: REGIONS })
|
|
ctx.regionStore
|
|
.getState()
|
|
.setRegion({ id: 99, name: 'Ghost', description: 'Stale', effectiveBoundId: 1 } as any, 7)
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(4)
|
|
|
|
// The hook has no membership validation against `listRegions`, so a stale
|
|
// region id is currently retained rather than cleared.
|
|
expect(result.current.regionId).toBe(99)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-13: a missing stored bound is filled from the derived active bound', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1 } = ctx.fixtures
|
|
ctx.regionStore.getState().setRegion(REGION_1, null)
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(4)
|
|
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(7)
|
|
expect(result.current.boundId).toBe(7)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-14: an existing stored bound is not overwritten by the active bound', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1 } = ctx.fixtures
|
|
ctx.regionStore.getState().setRegion(REGION_1, 99)
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(4)
|
|
|
|
expect(result.current.boundId).toBe(99)
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(99)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-15: refreshLocation re-arms the startup check and can adopt a region', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, BOUND_1 } = ctx.fixtures
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(4)
|
|
expect(result.current.regionId).toBeNull()
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
|
|
// The user now grants location and taps "use my location".
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
await ctx.rtl.act(async () => {
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
mode: 'success',
|
|
data: { region: REGION_1, bound: BOUND_1 },
|
|
})
|
|
result.current.refreshLocation()
|
|
})
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-19: a sole non-default address opens when location is off', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { A1 } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-20: a sole non-default address wins over a location in another region', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { A1, REGION_2 } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: {
|
|
region: REGION_2,
|
|
bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' },
|
|
},
|
|
})
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-23: signing in after the gate settled still opens the sole address', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { A1 } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const { result, view, rerender } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(4)
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
// Login from the chooser: auth and the address list arrive after the
|
|
// one-shot launch decision already settled.
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
await ctx.rtl.act(async () => {
|
|
rerender()
|
|
})
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-24: waits for the address list instead of settling early', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { A1 } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: true })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
|
|
const networkMock = (global as any).fetch
|
|
let releaseAddresses!: () => void
|
|
const addressesGate = new Promise<void>((resolve) => {
|
|
releaseAddresses = resolve
|
|
})
|
|
;(global as any).fetch = jest.fn(async (input: any, init: any) => {
|
|
const response = await networkMock(input, init)
|
|
if (String(input).includes('user.address.getUserAddresses')) await addressesGate
|
|
return response
|
|
})
|
|
|
|
try {
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
|
|
const { result, view, rerender } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(2)
|
|
|
|
// Auth lands; the default address is null, but the address list is held.
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
await ctx.rtl.act(async () => {
|
|
rerender()
|
|
})
|
|
await ctx.testUtils.flush(3)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
await ctx.rtl.act(async () => {
|
|
releaseAddresses()
|
|
})
|
|
await ctx.testUtils.flush(5)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
view.unmount()
|
|
} finally {
|
|
;(global as any).fetch = networkMock
|
|
}
|
|
})
|
|
})
|
|
|
|
it('REGION-25: cached regions and address open while live requests are pending', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1 } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
user: { id: 77 },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
ctx.trpc.setProcedure('common.listRegions', { mode: 'pending' })
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { mode: 'pending' })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { mode: 'pending' })
|
|
|
|
ctx.secure.__set(
|
|
ctx.regionCache.REGION_LIST_CACHE_KEY,
|
|
JSON.stringify([REGION_1, REGION_2])
|
|
)
|
|
ctx.secure.__set(
|
|
ctx.regionCache.userAddressesCacheKey(77),
|
|
JSON.stringify({ success: true, data: [sole] })
|
|
)
|
|
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
// Cached data must not stop the background refresh.
|
|
expect(ctx.trpc.procedureQueryCalls('common.listRegions').length).toBeGreaterThan(0)
|
|
expect(ctx.trpc.procedureQueryCalls('user.address.getUserAddresses').length).toBeGreaterThan(0)
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-26: live data that differs from the cache reconciles region and address', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, A2 } = ctx.fixtures
|
|
const cachedSole = { ...A1, isDefault: false }
|
|
const liveSole = { ...A2, isDefault: false }
|
|
ctx.auth.__setAuthState({
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
user: { id: 88 },
|
|
})
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [liveSole] } })
|
|
|
|
ctx.secure.__set(
|
|
ctx.regionCache.REGION_LIST_CACHE_KEY,
|
|
JSON.stringify([REGION_1, REGION_2])
|
|
)
|
|
ctx.secure.__set(
|
|
ctx.regionCache.userAddressesCacheKey(88),
|
|
JSON.stringify({ success: true, data: [cachedSole] })
|
|
)
|
|
|
|
// Hold the live address response so the cached snapshot settles first.
|
|
const networkMock = (global as any).fetch
|
|
let releaseAddresses!: () => void
|
|
const addressesGate = new Promise<void>((resolve) => {
|
|
releaseAddresses = resolve
|
|
})
|
|
;(global as any).fetch = jest.fn(async (input: any, init: any) => {
|
|
const response = await networkMock(input, init)
|
|
if (String(input).includes('user.address.getUserAddresses')) await addressesGate
|
|
return response
|
|
})
|
|
|
|
try {
|
|
const { result, view } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(6)
|
|
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(cachedSole.id)
|
|
|
|
await ctx.rtl.act(async () => {
|
|
releaseAddresses()
|
|
})
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(result.current.regionId).toBe(2)
|
|
expect(result.current.boundId).toBe(8)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(liveSole.id)
|
|
const persistedAddresses = JSON.parse(
|
|
ctx.secure.__store.get(ctx.regionCache.userAddressesCacheKey(88)) as string
|
|
)
|
|
expect(persistedAddresses.data[0].id).toBe(liveSole.id)
|
|
view.unmount()
|
|
} finally {
|
|
;(global as any).fetch = networkMock
|
|
}
|
|
})
|
|
})
|
|
|
|
it('REGION-27: a consumer above AuthProvider does not settle the launch decision', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, A2 } = ctx.fixtures
|
|
// UpdateChecker / HealthTestWrapper / WebViewWrapper consume useRegion
|
|
// through useGetEssentialConsts while mounted above AuthProvider.
|
|
ctx.auth.__setNoAuthContext(true)
|
|
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: A1 } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [A1, A2] } })
|
|
|
|
const { result, view, rerender } = ctx.renderRegion()
|
|
await ctx.testUtils.flush(5)
|
|
|
|
// Without an auth context there is no default/sole address anchor. It must
|
|
// stay unresolved instead of settling with regionId null, otherwise it
|
|
// shows the chooser and blocks the real gate via `startupCheckDone`.
|
|
expect(result.current.isResolvingRegion).toBe(true)
|
|
expect(result.current.regionId).toBeNull()
|
|
|
|
// The instance inside AuthProvider (LocationTestWrapper) can still settle
|
|
// and adopt the default address's region/bound.
|
|
ctx.auth.__setNoAuthContext(false)
|
|
ctx.auth.__setAuthState({
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
user: { id: 388 },
|
|
})
|
|
await ctx.rtl.act(async () => {
|
|
rerender()
|
|
})
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(result.current.isResolvingRegion).toBe(false)
|
|
expect(result.current.regionId).toBe(1)
|
|
expect(result.current.boundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBeNull()
|
|
view.unmount()
|
|
})
|
|
})
|
|
})
|
|
|
|
|
|
describe('Home region integration (REGION-16..18)', () => {
|
|
it('REGION-16: a default address and a location in its region open Home in that region', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, BOUND_1, A1, A2, productsCacheUrl } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: { region: REGION_1, bound: BOUND_1 },
|
|
})
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: A1 } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [A1, A2] } })
|
|
|
|
ctx.testUtils.seedJsonCache({ regionId: 1 })
|
|
ctx.testUtils.seedJsonCache({ regionId: 2 })
|
|
ctx.testUtils.seedCentralStores()
|
|
ctx.testUtils.seedSelectedAddress(A1.id)
|
|
|
|
const Home = require('@/app/(drawer)/(tabs)/home/index').default
|
|
const view = ctx.testUtils.renderWithProviders(ctx.React.createElement(Home), { auth: false })
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBe(1)
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(7)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(1)).length).toBeGreaterThan(0)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(2))).toHaveLength(0)
|
|
expect(view.getByText('Mahabubnagar')).toBeTruthy()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-17: a location in another region clears the default region and opens the chooser', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, A2, productsCacheUrl } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: {
|
|
region: REGION_2,
|
|
bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' },
|
|
},
|
|
})
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: A1 } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [A1, A2] } })
|
|
|
|
ctx.testUtils.seedJsonCache({ regionId: 1 })
|
|
ctx.testUtils.seedJsonCache({ regionId: 2 })
|
|
ctx.testUtils.seedCentralStores()
|
|
|
|
const Home = require('@/app/(drawer)/(tabs)/home/index').default
|
|
const view = ctx.testUtils.renderWithProviders(ctx.React.createElement(Home), { auth: false })
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBeNull()
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(1))).toHaveLength(0)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(2))).toHaveLength(0)
|
|
expect(view.getByText('Choose your address')).toBeTruthy()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-18: without a default address, the location alone opens its region', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, A2, productsCacheUrl } = ctx.fixtures
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: {
|
|
region: REGION_2,
|
|
bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' },
|
|
},
|
|
})
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [A1, A2] } })
|
|
ctx.trpc.setProcedure('common.essentialConsts', {
|
|
data: { ...ctx.fixtures.CONSTS, regionId: 2, boundId: 8 },
|
|
})
|
|
|
|
ctx.testUtils.seedJsonCache({ regionId: 1 })
|
|
ctx.testUtils.seedJsonCache({ regionId: 2 })
|
|
ctx.testUtils.seedCentralStores()
|
|
|
|
const Home = require('@/app/(drawer)/(tabs)/home/index').default
|
|
const view = ctx.testUtils.renderWithProviders(ctx.React.createElement(Home), { auth: false })
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBe(2)
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(8)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(2)).length).toBeGreaterThan(0)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(1))).toHaveLength(0)
|
|
expect(view.getByText('Hyderabad')).toBeTruthy()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-21: a sole non-default address opens Home with location off', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, productsCacheUrl } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'denied' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
|
|
ctx.testUtils.seedJsonCache({ regionId: 1 })
|
|
ctx.testUtils.seedJsonCache({ regionId: 2 })
|
|
ctx.testUtils.seedCentralStores()
|
|
|
|
const Home = require('@/app/(drawer)/(tabs)/home/index').default
|
|
const view = ctx.testUtils.renderWithProviders(ctx.React.createElement(Home), { auth: false })
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBe(1)
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(1)).length).toBeGreaterThan(0)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(2))).toHaveLength(0)
|
|
expect(view.getByText('Mahabubnagar')).toBeTruthy()
|
|
view.unmount()
|
|
})
|
|
})
|
|
|
|
it('REGION-22: a sole non-default address opens Home even when location is elsewhere', async () => {
|
|
await withFreshModules(async (ctx) => {
|
|
const { REGION_1, REGION_2, A1, productsCacheUrl } = ctx.fixtures
|
|
const sole = { ...A1, isDefault: false }
|
|
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
|
|
ctx.locationMock.__configure({ permission: 'granted' })
|
|
ctx.trpc.setProcedure('common.listRegions', { data: [REGION_1, REGION_2] })
|
|
ctx.trpc.setProcedure('common.resolveRegionByLocation', {
|
|
data: {
|
|
region: REGION_2,
|
|
bound: { id: 8, regionId: 2, priority: 1, name: 'Bound 2' },
|
|
},
|
|
})
|
|
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
|
|
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
|
|
|
|
ctx.testUtils.seedJsonCache({ regionId: 1 })
|
|
ctx.testUtils.seedJsonCache({ regionId: 2 })
|
|
ctx.testUtils.seedCentralStores()
|
|
|
|
const Home = require('@/app/(drawer)/(tabs)/home/index').default
|
|
const view = ctx.testUtils.renderWithProviders(ctx.React.createElement(Home), { auth: false })
|
|
await ctx.testUtils.flush(8)
|
|
|
|
expect(ctx.regionStore.getState().selectedRegionId).toBe(1)
|
|
expect(ctx.regionStore.getState().selectedBoundId).toBe(7)
|
|
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(1)).length).toBeGreaterThan(0)
|
|
expect(ctx.fetchJson.jsonCallsFor(productsCacheUrl(2))).toHaveLength(0)
|
|
expect(view.getByText('Mahabubnagar')).toBeTruthy()
|
|
view.unmount()
|
|
})
|
|
})
|
|
})
|