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

101 lines
3.1 KiB
TypeScript

/**
* Search results (app/(drawer)/(tabs)/home/search-results/index.tsx).
*
* Client-side Fuse.js search over the products cache, debounced by 300ms.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
import SearchResults from '@/app/(drawer)/(tabs)/home/search-results/index'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { routerMock } from '../mocks/router'
import { renderWithProviders } from '../helpers/render'
const CARD = 'product-card'
const INPUT = 'search-input-input'
/** Render and wait until the product grid is populated. */
async function renderLoaded() {
const api = setupApiMocks()
renderWithProviders(<SearchResults />)
await waitFor(() => expect(screen.getAllByTestId(CARD).length).toBeGreaterThan(0))
return api
}
/** Type into the search bar and let the 300ms debounce fire. */
async function typeQuery(text: string) {
fireEvent.changeText(screen.getByTestId(INPUT), text)
await act(async () => {
jest.advanceTimersByTime(300)
})
}
describe('Search results', () => {
beforeEach(async () => {
await resetStorage()
})
it('lists all products when the query is empty', async () => {
await renderLoaded()
expect(screen.getByText('All Products')).toBeVisible()
expect(screen.getAllByTestId(CARD).length).toBeGreaterThan(0)
})
it('filters products by the debounced query', async () => {
await renderLoaded()
const before = screen.getAllByTestId(CARD).length
await typeQuery('chicken liver')
await waitFor(() =>
expect(screen.getByText(/Search Results for "chicken liver"/)).toBeVisible()
)
const after = screen.getAllByTestId(CARD).length
expect(after).toBeGreaterThan(0)
expect(after).toBeLessThanOrEqual(before)
})
it('shows the no-results state for an unmatched query', async () => {
await renderLoaded()
await typeQuery('zzzzzzzz')
await waitFor(() => expect(screen.getByText('No products found')).toBeVisible())
expect(screen.getByText(/Try adjusting your search/)).toBeVisible()
})
it('restores all products when the query is cleared', async () => {
await renderLoaded()
await typeQuery('chicken liver')
await waitFor(() =>
expect(screen.getByText(/Search Results for "chicken liver"/)).toBeVisible()
)
await typeQuery('')
await waitFor(() => expect(screen.getByText('All Products')).toBeVisible())
expect(screen.getAllByTestId(CARD).length).toBeGreaterThan(0)
})
it('shows the loading state while products are pending', async () => {
setupApiMocks({ pendingProducts: true })
renderWithProviders(<SearchResults />)
await waitFor(() => expect(screen.getByText('Loading products...')).toBeVisible())
})
it('opens the product detail page when a card is pressed', async () => {
await renderLoaded()
fireEvent.press(screen.getAllByTestId(CARD)[0])
expect(routerMock.push).toHaveBeenCalledWith(
expect.stringContaining('/home/product-detail/')
)
})
})