100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
/**
|
|
* Stores list (app/(drawer)/(tabs)/stores/index.tsx).
|
|
*
|
|
* Reads stores.json, deep-links to a store picked on the home page via
|
|
* navigationStore, and offers a retry on error.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import Stores from '@/app/(drawer)/(tabs)/stores/index'
|
|
import storesResponse from '../mocks/mock-stores-response.json'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { routerMock } from '../mocks/router'
|
|
import { useNavigationStore } from '@/src/store/navigationStore'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const countStoresRequests = (mock: { mock: { calls: unknown[][] } }) =>
|
|
mock.mock.calls.filter(([url]) => String(url).includes('stores.json')).length
|
|
|
|
describe('Stores screen', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
useNavigationStore.setState({
|
|
isNavigatedFromHome: false,
|
|
selectedStoreId: null,
|
|
})
|
|
})
|
|
|
|
it('lists every store with its product count', async () => {
|
|
setupApiMocks()
|
|
renderWithProviders(<Stores />)
|
|
|
|
// The header is static, so wait for the cards before asserting names.
|
|
await waitFor(() =>
|
|
expect(screen.getAllByText('Explore Store').length).toBeGreaterThan(0)
|
|
)
|
|
|
|
for (const store of storesResponse.stores) {
|
|
expect(screen.getByText(store.name)).toBeVisible()
|
|
}
|
|
expect(screen.getAllByText('Explore Store')).toHaveLength(
|
|
storesResponse.stores.length
|
|
)
|
|
})
|
|
|
|
it('navigates to a store detail when Explore Store is pressed', async () => {
|
|
setupApiMocks()
|
|
renderWithProviders(<Stores />)
|
|
|
|
await waitFor(() =>
|
|
expect(screen.getAllByText('Explore Store').length).toBeGreaterThan(0)
|
|
)
|
|
fireEvent.press(screen.getAllByText('Explore Store')[0])
|
|
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
expect.stringContaining('/stores/store-detail/')
|
|
)
|
|
})
|
|
|
|
it('deep-links to the store chosen on the home page and clears the flag', async () => {
|
|
setupApiMocks()
|
|
useNavigationStore.setState({ isNavigatedFromHome: true, selectedStoreId: 4 })
|
|
|
|
renderWithProviders(<Stores />)
|
|
|
|
await waitFor(() =>
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
'/(drawer)/(tabs)/stores/store-detail/4'
|
|
)
|
|
)
|
|
|
|
const state = useNavigationStore.getState()
|
|
expect(state.isNavigatedFromHome).toBe(false)
|
|
expect(state.selectedStoreId).toBeNull()
|
|
})
|
|
|
|
it('shows the error state and refetches on Retry', async () => {
|
|
const api = setupApiMocks({ failStores: true })
|
|
renderWithProviders(<Stores />)
|
|
|
|
await waitFor(() => expect(screen.getByText('Store Fetch Failed')).toBeVisible())
|
|
|
|
const before = countStoresRequests(api.axiosGet)
|
|
fireEvent.press(screen.getByText('Retry'))
|
|
|
|
await waitFor(() =>
|
|
expect(countStoresRequests(api.axiosGet)).toBeGreaterThan(before)
|
|
)
|
|
})
|
|
|
|
it('shows the empty state when there are no stores', async () => {
|
|
setupApiMocks({ stores: { stores: [] } })
|
|
renderWithProviders(<Stores />)
|
|
|
|
await waitFor(() => expect(screen.getByText('No Stores Available')).toBeVisible())
|
|
})
|
|
})
|