/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 6.1 — Store list (`tests/pages/stores.test.tsx`).
*/
import React from 'react'
import { Alert } from 'react-native'
import { fireEvent, act } from '@testing-library/react-native'
import Stores from '@/app/(drawer)/dashboard/stores/index'
import { renderAndFlush, renderWithProviders } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
import { router } from '../mocks/expo-router-mock'
import { STORES, STORE_1, STORE_GLOBAL } from '../fixtures'
const mockFocusCallbacks: Array<() => any> = []
jest.mock('@react-navigation/native', () => {
const actual = jest.requireActual('../mocks/react-navigation-mock')
const ReactModule = require('react')
return {
...actual,
useFocusEffect: (callback: () => any) => {
mockFocusCallbacks.push(callback)
ReactModule.useEffect(() => {
const cleanup = callback()
return typeof cleanup === 'function' ? cleanup : undefined
}, [callback])
},
}
})
function renderStores() {
return renderAndFlush(, { auth: 'in' })
}
describe('Store list (ASTOREUI-01..16)', () => {
let alertSpy: jest.SpyInstance
beforeEach(() => {
resetAllMocks()
mockFocusCallbacks.length = 0
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
})
afterEach(() => alertSpy?.mockRestore())
it('ASTOREUI-01: shows the loading copy', () => {
setProcedure('admin.store.getStores', { mode: 'pending' })
// Loading state is asserted synchronously, before the query settles.
const view = renderWithProviders(, { auth: 'in' })
expect(view.getByText('Loading stores...')).toBeTruthy()
})
it('ASTOREUI-02: an error shows Oops! and a retry action', async () => {
setProcedure('admin.store.getStores', { mode: 'error', error: new Error('boom') })
const view = await renderStores()
expect(view.getByText('Oops!')).toBeTruthy()
fireEvent.press(view.getByText('Retry'))
})
it('ASTOREUI-03: an empty response shows the empty state and the add action', async () => {
setProcedure('admin.store.getStores', { data: { stores: [], count: 0 } })
const view = await renderStores()
expect(view.getByText('No stores found')).toBeTruthy()
fireEvent.press(view.getByTestId('add-store-fab'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores/add')
})
it('ASTOREUI-04: renders store name, id and image', async () => {
const view = await renderStores()
expect(view.getByText('The Freshyo Meat House')).toBeTruthy()
expect(view.getByText('ID: #1')).toBeTruthy()
expect(view.getByText('2 Locations')).toBeTruthy()
expect(view.getAllByText('View Details')).toHaveLength(2)
})
it('ASTOREUI-05: a global store has no edit/delete action', async () => {
const view = await renderStores()
fireEvent.press(view.getByText('Edit'))
expect(view.queryByTestId('delete-store-2')).toBeNull()
fireEvent.press(view.getByText('Freshyo Central'))
expect(router.push).not.toHaveBeenCalledWith(
expect.objectContaining({ pathname: '/(drawer)/dashboard/stores/edit' })
)
})
it('ASTOREUI-06: edit mode toggles the delete actions', async () => {
const view = await renderStores()
expect(view.queryByTestId('delete-store-1')).toBeNull()
fireEvent.press(view.getByText('Edit'))
expect(view.getByTestId('delete-store-1')).toBeTruthy()
expect(view.getByText('Done')).toBeTruthy()
fireEvent.press(view.getByText('Done'))
expect(view.queryByTestId('delete-store-1')).toBeNull()
})
it('ASTOREUI-07: tapping a store opens its edit route with the id', async () => {
const view = await renderStores()
fireEvent.press(view.getByText('The Freshyo Meat House'))
expect(router.push).toHaveBeenCalledWith({
pathname: '/(drawer)/dashboard/stores/edit',
params: { id: 1 },
})
})
it('ASTOREUI-08: a global store never opens the edit route', async () => {
const view = await renderStores()
fireEvent.press(view.getByText('Freshyo Central'))
expect(router.push).not.toHaveBeenCalled()
})
it('ASTOREUI-09: delete asks for confirmation and Cancel makes no API call', async () => {
const view = await renderStores()
fireEvent.press(view.getByText('Edit'))
fireEvent.press(view.getByTestId('delete-store-1'))
expect(alertSpy).toHaveBeenCalledTimes(1)
const [title, message, buttons] = alertSpy.mock.calls[0]
expect(title).toBe('Delete Store')
expect(message).toContain('Are you sure you want to delete this store?')
expect(buttons[0].text).toBe('Cancel')
expect(procedureMutationCalls('admin.store.deleteStore')).toHaveLength(0)
})
it('ASTOREUI-10: confirming delete calls deleteStore and alerts on success', async () => {
const view = await renderStores()
fireEvent.press(view.getByText('Edit'))
fireEvent.press(view.getByTestId('delete-store-1'))
const buttons = alertSpy.mock.calls[0][2]
const deleteButton = buttons.find((button: any) => button.text === 'Delete')
await act(async () => {
await deleteButton.onPress()
})
expect(lastMutationInput('admin.store.deleteStore')).toEqual({ storeId: 1 })
expect(alertSpy).toHaveBeenCalledWith('Success', 'Store deleted successfully')
})
it('ASTOREUI-11: a failed delete alerts with the error message', async () => {
setProcedure('admin.store.deleteStore', { mode: 'error', error: new Error('nope') })
const view = await renderStores()
fireEvent.press(view.getByText('Edit'))
fireEvent.press(view.getByTestId('delete-store-1'))
const buttons = alertSpy.mock.calls[0][2]
await act(async () => {
await buttons.find((button: any) => button.text === 'Delete').onPress()
})
expect(alertSpy).toHaveBeenCalledWith('Error', 'nope')
})
it('ASTOREUI-12: a pending delete keeps the card and shows no success alert', async () => {
setProcedure('admin.store.deleteStore', { mode: 'pending' })
const view = await renderStores()
fireEvent.press(view.getByText('Edit'))
fireEvent.press(view.getByTestId('delete-store-1'))
const buttons = alertSpy.mock.calls[0][2]
await act(async () => {
buttons.find((button: any) => button.text === 'Delete').onPress()
})
expect(view.getByText('The Freshyo Meat House')).toBeTruthy()
expect(alertSpy).not.toHaveBeenCalledWith('Success', 'Store deleted successfully')
})
it('ASTOREUI-13: the add FAB opens the add route', async () => {
const view = await renderStores()
fireEvent.press(view.getByTestId('add-store-fab'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores/add')
})
it('ASTOREUI-14: the page registers a focus callback that triggers a refetch', async () => {
await renderStores()
const callbacks = [...new Set(mockFocusCallbacks)]
expect(callbacks).toHaveLength(1)
expect(() => callbacks[0]()).not.toThrow()
})
it('ASTOREUI-15: a store without an image renders its fallback without crashing', async () => {
const view = await renderStores()
expect(view.getByText('Freshyo Central')).toBeTruthy()
})
it('ASTOREUI-16: a long store name is truncated to a single line', async () => {
const longName = 'The Freshyo Meat House With An Extremely Long Branch Name'
setProcedure('admin.store.getStores', {
data: { stores: [{ ...STORE_1, name: longName }, STORE_GLOBAL], count: 2 },
})
const view = await renderStores()
expect(view.getByText(longName).props.numberOfLines).toBe(1)
})
})