freshyo/apps/user-ui/tests/hooks/prominent-api-hooks.test.tsx
2026-09-26 13:41:46 +05:30

95 lines
3 KiB
TypeScript

/**
* src/hooks/prominent-api-hooks.ts — CDN cache layer.
* Verifies URL derivation from essentialConsts, the availability merge, the
* store-detail fetch and error propagation.
*/
import React from 'react'
import { renderHook, waitFor } from '@testing-library/react-native'
import {
useAllProducts,
useGetEssentialConsts,
useSlots,
useStores,
useStoreWithProducts,
} from '@/src/hooks/prominent-api-hooks'
import {
AVAILABILITY_URL,
PRODUCTS_URL,
SLOTS_URL,
STORES_URL,
resetEssentialConsts,
resetStorage,
setupApiMocks,
storeDetailUrl,
} from '../mocks/api'
import { Providers } from '../helpers/render'
const wrapper = ({ children }: { children?: React.ReactNode }) => (
<Providers centralStores={false}>{children}</Providers>
)
const axiosCalledWith = (
mock: { mock: { calls: unknown[][] } },
needle: string
) => mock.mock.calls.some(([url]) => String(url).includes(needle))
describe('prominent-api-hooks', () => {
beforeEach(async () => {
await resetEssentialConsts()
await resetStorage()
})
it('exposes essentialConsts from the trpc procedure', async () => {
setupApiMocks()
const { result } = renderHook(() => useGetEssentialConsts(), { wrapper })
await waitFor(() => expect(result.current.data?.freeDeliveryThreshold).toBe(249))
})
it('derives the products cache url and merges availability', async () => {
const api = setupApiMocks()
const { result } = renderHook(() => useAllProducts(), { wrapper })
await waitFor(() => expect(result.current.data?.products.length).toBeGreaterThan(0))
expect(axiosCalledWith(api.axiosGet, PRODUCTS_URL)).toBe(true)
expect(axiosCalledWith(api.axiosGet, AVAILABILITY_URL)).toBe(true)
const chicken = result.current.data?.products.find((p) => p.id === 2)
expect(chicken?.price).toBe(215)
expect(chicken?.marketPrice).toBe(220)
})
it('derives the stores cache url', async () => {
const api = setupApiMocks()
const { result } = renderHook(() => useStores(), { wrapper })
await waitFor(() => expect(result.current.data?.stores.length).toBeGreaterThan(0))
expect(axiosCalledWith(api.axiosGet, STORES_URL)).toBe(true)
})
it('derives the slots cache url', async () => {
const api = setupApiMocks()
const { result } = renderHook(() => useSlots(), { wrapper })
await waitFor(() => expect(result.current.data?.slots.length).toBeGreaterThan(0))
expect(axiosCalledWith(api.axiosGet, SLOTS_URL)).toBe(true)
})
it('fetches a single store detail', async () => {
const api = setupApiMocks()
const { result } = renderHook(() => useStoreWithProducts(1), { wrapper })
await waitFor(() => expect(result.current.data).toBeTruthy())
expect(axiosCalledWith(api.axiosGet, storeDetailUrl(1))).toBe(true)
expect(result.current.data?.store.id).toBe(1)
})
it('propagates a products fetch error', async () => {
setupApiMocks({ failProducts: true })
const { result } = renderHook(() => useAllProducts(), { wrapper })
await waitFor(() => expect(result.current.error).toBeTruthy())
})
})