freshyo/apps/user-ui/tests/lib/location-store.test.ts
2026-10-18 21:51:01 +05:30

170 lines
5.5 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 25 — "Region and location" (`tests/lib/location-store.test.ts`).
*
* LOCSTORE-01..09: direct unit tests over the real `useLocationStore`.
* `resetAllMocks()` calls `stop()` (which bumps the module-level `activeToken`
* and clears `started`) so every case starts from a clean one-shot launch.
*/
import { resetAllMocks } from '../helpers/reset'
import { useLocationStore } from '@/src/store/locationStore'
import * as Location from '../mocks/expo-location-mock'
import { __configure } from '../mocks/expo-location-mock'
const store = () => useLocationStore.getState()
const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
async function settle(times = 4) {
for (let index = 0; index < times; index += 1) {
// eslint-disable-next-line no-await-in-loop
await tick()
}
}
afterEach(() => {
jest.restoreAllMocks()
})
describe('locationStore', () => {
beforeEach(() => {
resetAllMocks()
})
it('LOCSTORE-01: start() takes a single permission + fix per launch', async () => {
const permission = jest.spyOn(Location, 'requestForegroundPermissionsAsync')
const fix = jest.spyOn(Location, 'getCurrentPositionAsync')
store().start()
await settle()
store().start() // second start must be a no-op
await settle()
expect(permission).toHaveBeenCalledTimes(1)
expect(fix).toHaveBeenCalledTimes(1)
expect(store().status).toBe('granted')
})
it('LOCSTORE-02: denied permission yields status denied with no coords', async () => {
__configure({ permission: 'denied' })
store().start()
await settle()
expect(store().status).toBe('denied')
expect(store().coords).toBeNull()
expect(store().error).toBeNull()
})
it('LOCSTORE-03: disabled services yield status services-off', async () => {
__configure({ permission: 'granted', servicesEnabled: false })
store().start()
await settle()
expect(store().status).toBe('services-off')
expect(store().coords).toBeNull()
})
it('LOCSTORE-04: success rounds coordinates to 5 decimal places', async () => {
__configure({
permission: 'granted',
servicesEnabled: true,
position: { coords: { latitude: 17.123456789, longitude: 78.987654321 }, timestamp: 1 },
})
store().start()
await settle()
expect(store().status).toBe('granted')
expect(store().coords).toEqual({ latitude: 17.12346, longitude: 78.98765 })
})
it('LOCSTORE-05: a hanging fix times out to unavailable with an error', async () => {
jest.useFakeTimers()
__configure({ permission: 'granted', servicesEnabled: true })
jest
.spyOn(Location, 'getCurrentPositionAsync')
.mockImplementation(() => new Promise<never>(() => {}))
store().start()
await jest.advanceTimersByTimeAsync(0)
expect(store().status).toBe('locating')
await jest.advanceTimersByTimeAsync(8000)
expect(store().status).toBe('unavailable')
expect(store().coords).toBeNull()
expect(store().error).toMatch(/timed out/i)
})
it('LOCSTORE-06: a stale in-flight fix is ignored after stop()', async () => {
__configure({ permission: 'granted', servicesEnabled: true })
let resolveFix: (position: any) => void = () => {}
const pending = new Promise((resolve) => {
resolveFix = resolve
})
jest.spyOn(Location, 'getCurrentPositionAsync').mockReturnValue(pending as any)
store().start()
await settle(2)
expect(store().status).toBe('locating')
store().stop()
expect(store().status).toBe('idle')
resolveFix({ coords: { latitude: 1, longitude: 2 }, timestamp: 1 })
await settle(2)
// The fix landed after the token was bumped, so it must be discarded.
expect(store().status).toBe('idle')
expect(store().coords).toBeNull()
})
it('LOCSTORE-07: stop() resets state and allows start() to run again', async () => {
__configure({ permission: 'granted', servicesEnabled: true })
const permission = jest.spyOn(Location, 'requestForegroundPermissionsAsync')
store().start()
await settle()
expect(store().status).toBe('granted')
store().stop()
expect(store().status).toBe('idle')
expect(store().coords).toBeNull()
expect(store().error).toBeNull()
store().start()
await settle()
expect(store().status).toBe('granted')
expect(permission).toHaveBeenCalledTimes(2)
})
it('LOCSTORE-08: probe() is true when permission is granted and services are on', async () => {
__configure({ permission: 'granted', servicesEnabled: true })
const permission = jest.spyOn(Location, 'getForegroundPermissionsAsync')
const fix = jest.spyOn(Location, 'getCurrentPositionAsync')
await expect(store().probe()).resolves.toBe(true)
expect(permission).toHaveBeenCalledTimes(1)
// probe() never takes a fix and never touches the read state.
expect(fix).not.toHaveBeenCalled()
expect(store().status).toBe('idle')
expect(store().coords).toBeNull()
})
it('LOCSTORE-09: probe() is false when denied / services off / throws, leaving state untouched', async () => {
__configure({ permission: 'denied' })
await expect(store().probe()).resolves.toBe(false)
__configure({ permission: 'granted', servicesEnabled: false })
await expect(store().probe()).resolves.toBe(false)
__configure({ permission: 'granted', servicesEnabled: true, throwOnPermission: true })
await expect(store().probe()).resolves.toBe(false)
expect(store().status).toBe('idle')
expect(store().coords).toBeNull()
expect(store().error).toBeNull()
})
})