/**
* Store detail (stores/store-detail/[id].tsx) — store meta, tag chips,
* products and the tag filter.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
import StoreDetail from '@/app/(drawer)/(tabs)/stores/store-detail/[id]'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { routerMock, searchParamsMock } from '../mocks/router'
import { renderWithProviders } from '../helpers/render'
const CARD = 'product-card'
describe('Store detail', () => {
beforeEach(async () => {
await resetStorage()
searchParamsMock.current = { id: '1' }
})
it('renders the store, its tags and products', async () => {
setupApiMocks()
renderWithProviders()
await waitFor(() => expect(screen.getByText('Meat Store')).toBeVisible())
expect(screen.getByText('Chicken (3)')).toBeVisible()
await waitFor(() => expect(screen.getAllByTestId(CARD).length).toBeGreaterThan(0))
})
it('filters by tag and clears the filter', async () => {
setupApiMocks()
renderWithProviders()
await waitFor(() => expect(screen.getByText('Chicken (3)')).toBeVisible())
fireEvent.press(screen.getByText('Chicken (3)'))
await waitFor(() => expect(screen.getByText('Chicken items')).toBeVisible())
fireEvent.press(screen.getByText('Clear'))
await waitFor(() => expect(screen.getByText(/\d+ products/)).toBeVisible())
})
it('navigates to a product detail when a card is pressed', async () => {
setupApiMocks()
renderWithProviders()
await waitFor(() => expect(screen.getAllByTestId(CARD).length).toBeGreaterThan(0))
fireEvent.press(screen.getAllByTestId(CARD)[0])
expect(routerMock.push).toHaveBeenCalledWith(
expect.stringContaining('/stores/store-detail/product-detail/')
)
})
it('shows the error state when the store cannot be loaded', async () => {
setupApiMocks({ failStores: true })
renderWithProviders()
await waitFor(() =>
expect(screen.getByText('Store not found or error loading')).toBeVisible()
)
})
})