freshyo/apps/user-ui/tests/home-page/home-refresh.test.tsx
2026-09-26 00:46:08 +05:30

85 lines
2.6 KiB
TypeScript

/**
* Home page pull-to-refresh.
*
* The page funnels both useManualRefresh and useMarkDataFetchers into
* handleRefresh, which refetches products, slots, stores and essentialConsts.
* tests/setup.ts records those callbacks (tests/mocks/refresh.ts) so a test can
* invoke them directly.
*
* Availability is deliberately NOT asserted: handleRefresh does not refetch it.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import { act, waitFor } from '@testing-library/react-native'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { refreshHooks, resetRefreshHooks } from '../mocks/refresh'
import { useNavigationStore } from '@/src/store/navigationStore'
import { renderLoadedHome } from './render'
const countRequests = (
mock: { mock: { calls: unknown[][] } },
needle: string
) => mock.mock.calls.filter(([url]) => String(url).includes(needle)).length
describe('Home page refresh', () => {
beforeEach(async () => {
resetRefreshHooks()
await resetStorage()
useNavigationStore.setState({
isNavigatedFromHome: false,
selectedStoreId: null,
})
})
const expectRefetchFanOut = async (invoke: () => void) => {
const api = setupApiMocks()
await renderLoadedHome()
const before = {
products: countRequests(api.axiosGet, 'products.json'),
stores: countRequests(api.axiosGet, 'stores.json'),
slots: countRequests(api.axiosGet, 'slots.json'),
consts: countRequests(api.fetchMock, 'common.essentialConsts'),
}
expect(before.products).toBeGreaterThan(0)
expect(before.stores).toBeGreaterThan(0)
expect(before.slots).toBeGreaterThan(0)
expect(before.consts).toBeGreaterThan(0)
await act(async () => {
invoke()
})
await waitFor(() => {
expect(countRequests(api.axiosGet, 'products.json')).toBeGreaterThan(
before.products
)
expect(countRequests(api.axiosGet, 'stores.json')).toBeGreaterThan(
before.stores
)
expect(countRequests(api.axiosGet, 'slots.json')).toBeGreaterThan(
before.slots
)
expect(countRequests(api.fetchMock, 'common.essentialConsts')).toBeGreaterThan(
before.consts
)
})
}
it('refetches every data source through useManualRefresh', async () => {
await expectRefetchFanOut(() => {
expect(typeof refreshHooks.manualRefresh).toBe('function')
refreshHooks.manualRefresh?.()
})
})
it('refetches every data source through useMarkDataFetchers', async () => {
await expectRefetchFanOut(() => {
expect(typeof refreshHooks.markDataFetchers).toBe('function')
refreshHooks.markDataFetchers?.()
})
})
})