98 lines
3 KiB
TypeScript
98 lines
3 KiB
TypeScript
/**
|
|
* NotifRegionSync — carries the browsing region with the push token and records a
|
|
* location-derived user<->region association.
|
|
*
|
|
* The notification context and useRegion are mocked so the test drives the
|
|
* token/region directly; the tRPC layer is real against the fetch stub, so the
|
|
* outbound mutation input is asserted.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { waitFor } from '@testing-library/react-native'
|
|
|
|
import NotifRegionSync from '@/src/components/NotifRegionSync'
|
|
import { useRegion } from '@/src/hooks/useRegion'
|
|
import { setupApiMocks } from '../mocks/api'
|
|
import { lastProcedureInput, procedureCalls } from '../mocks/procedures'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
jest.mock('@/services/notif-service/notif-context', () => ({
|
|
useNotification: () => ({
|
|
expoPushToken: 'ExponentPushToken[test]',
|
|
notifPermission: 'granted',
|
|
notification: null,
|
|
error: null,
|
|
}),
|
|
}))
|
|
|
|
jest.mock('@/src/hooks/useRegion', () => ({ useRegion: jest.fn() }))
|
|
|
|
const mockedUseRegion = useRegion as unknown as jest.Mock
|
|
|
|
const regionValue = (over: Record<string, unknown> = {}) => ({
|
|
region: null,
|
|
regionId: 1,
|
|
boundId: null,
|
|
bound: null,
|
|
regionGeneration: 0,
|
|
hydrated: true,
|
|
coords: null,
|
|
locationStatus: 'idle',
|
|
isResolvingRegion: false,
|
|
setRegion: jest.fn(),
|
|
clearRegion: jest.fn(),
|
|
refreshLocation: jest.fn(),
|
|
...over,
|
|
})
|
|
|
|
const procedures = {
|
|
'user.user.savePushToken': { success: true },
|
|
'user.user.recordTrackedRegion': { success: true },
|
|
}
|
|
|
|
const renderSync = () =>
|
|
renderWithProviders(<NotifRegionSync />, { centralStores: false })
|
|
|
|
describe('NotifRegionSync', () => {
|
|
it('sends the region with the push token, without a tracked-region call for an address-derived region', async () => {
|
|
mockedUseRegion.mockReturnValue(regionValue({ regionId: 1, bound: null }))
|
|
const api = setupApiMocks({ procedures })
|
|
|
|
renderSync()
|
|
|
|
await waitFor(() =>
|
|
expect(lastProcedureInput(api.fetchMock, 'user.user.savePushToken')).toEqual({
|
|
token: 'ExponentPushToken[test]',
|
|
regionId: 1,
|
|
})
|
|
)
|
|
expect(procedureCalls(api.fetchMock, 'user.user.recordTrackedRegion')).toHaveLength(0)
|
|
})
|
|
|
|
it('records a tracked region when the region came from the device fix', async () => {
|
|
mockedUseRegion.mockReturnValue(regionValue({ regionId: 3, bound: { id: 9 } }))
|
|
const api = setupApiMocks({ procedures })
|
|
|
|
renderSync()
|
|
|
|
await waitFor(() =>
|
|
expect(lastProcedureInput(api.fetchMock, 'user.user.recordTrackedRegion')).toEqual({
|
|
regionId: 3,
|
|
})
|
|
)
|
|
expect(lastProcedureInput(api.fetchMock, 'user.user.savePushToken')).toEqual({
|
|
token: 'ExponentPushToken[test]',
|
|
regionId: 3,
|
|
})
|
|
})
|
|
|
|
it('does nothing until a region is known', () => {
|
|
mockedUseRegion.mockReturnValue(regionValue({ regionId: null }))
|
|
const api = setupApiMocks({ procedures })
|
|
|
|
renderSync()
|
|
|
|
expect(procedureCalls(api.fetchMock, 'user.user.savePushToken')).toHaveLength(0)
|
|
expect(procedureCalls(api.fetchMock, 'user.user.recordTrackedRegion')).toHaveLength(0)
|
|
})
|
|
})
|