172 lines
5.8 KiB
TypeScript
172 lines
5.8 KiB
TypeScript
/**
|
|
* Edit store screen (app/(drawer)/dashboard/stores/edit.tsx) — prefill,
|
|
* not-found/loading states and the update -> list flow.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import EditStore from '@/app/(drawer)/dashboard/stores/edit'
|
|
import { setupApiMocks, resetStorage } from '../mocks/api'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import {
|
|
expectProcedureCalled,
|
|
lastProcedureInput,
|
|
procCount,
|
|
} from '../mocks/procedures'
|
|
import { routerMock, searchParamsMock } from '../mocks/router'
|
|
import { createQueryClient, renderWithProviders } from '../helpers/render'
|
|
|
|
const store = {
|
|
id: 3,
|
|
name: 'Freshyo Edit',
|
|
description: 'Editable outlet',
|
|
imageUrl: 'https://cdn.freshyo.in/store.png',
|
|
owner: { id: 1, name: 'shafi' },
|
|
createdAt: '2026-03-01T00:00:00.000Z',
|
|
}
|
|
|
|
const procedures = {
|
|
'admin.store.getStoreById': { store },
|
|
'admin.staffUser.getStaff': {
|
|
staff: [{ id: 1, name: 'shafi', role: null, permissions: [] }],
|
|
},
|
|
'admin.product.getProducts': {
|
|
products: [{ id: 1, name: 'Chicken', storeId: null, skus: [] }],
|
|
count: 1,
|
|
},
|
|
'common.product.getAllSkusSummary': { skus: [] },
|
|
'admin.store.updateStore': {
|
|
store: { id: 3 },
|
|
message: 'Store updated successfully',
|
|
},
|
|
'admin.store.getStores': { stores: [], count: 0 },
|
|
}
|
|
|
|
describe('Edit store', () => {
|
|
beforeEach(async () => {
|
|
resetStaffAuthMock()
|
|
await resetStorage()
|
|
searchParamsMock.current = { id: '3' }
|
|
setupApiMocks({ procedures })
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
routerMock.push.mockClear()
|
|
})
|
|
|
|
it('shows a loader while the store is loading', () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
fetchMock.mockImplementation(() => new Promise(() => {}))
|
|
|
|
renderWithProviders(<EditStore />)
|
|
|
|
expect(screen.getByText('Loading store...')).toBeVisible()
|
|
})
|
|
|
|
it('shows not-found when the store does not exist', async () => {
|
|
setupApiMocks({ procedures: { 'admin.store.getStoreById': null } })
|
|
|
|
renderWithProviders(<EditStore />)
|
|
|
|
expect(await screen.findByText('Store not found')).toBeVisible()
|
|
})
|
|
|
|
it('prefills the form and submits the update', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<EditStore />)
|
|
|
|
expect(await screen.findByText('Edit Store')).toBeVisible()
|
|
expect(screen.getByDisplayValue('Freshyo Edit')).toBeVisible()
|
|
expect(screen.getByDisplayValue('Editable outlet')).toBeVisible()
|
|
expect(lastProcedureInput(fetchMock, 'admin.store.getStoreById')).toEqual({ id: 3 })
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.product.getProducts')
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Update Store'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.store.updateStore')
|
|
)
|
|
expect(lastProcedureInput(fetchMock, 'admin.store.updateStore')).toEqual({
|
|
id: 3,
|
|
name: 'Freshyo Edit',
|
|
description: 'Editable outlet',
|
|
owner: 1,
|
|
products: [],
|
|
})
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Success', 'Store updated successfully')
|
|
)
|
|
await waitFor(() =>
|
|
expect(routerMock.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores')
|
|
)
|
|
})
|
|
|
|
it('surfaces an update error', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
const original = fetchMock.getMockImplementation()!
|
|
fetchMock.mockImplementation((url: any, options: any) =>
|
|
String(url).includes('admin.store.updateStore')
|
|
? Promise.reject(new Error('Failed to update store'))
|
|
: original(url, options)
|
|
)
|
|
|
|
renderWithProviders(<EditStore />)
|
|
await screen.findByText('Edit Store')
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.product.getProducts')
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Update Store'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Error', 'Failed to update store')
|
|
)
|
|
})
|
|
|
|
it('refetches the mounted store and invalidates the stores list by array path after a successful update', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
// The injected client is the one trpc.useUtils() acts on, so spying on it
|
|
// observes the real invalidation (spyOn keeps calling through).
|
|
const client = createQueryClient()
|
|
const invalidateSpy = jest.spyOn(client, 'invalidateQueries')
|
|
|
|
renderWithProviders(<EditStore />, { queryClient: client })
|
|
|
|
expect(await screen.findByText('Edit Store')).toBeVisible()
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.product.getProducts')
|
|
)
|
|
|
|
// Baseline immediately before the interaction: the app shares one
|
|
// module-singleton httpBatchLink client, so counts can leak across cases
|
|
// in a full-file run — always assert a delta, never an absolute number.
|
|
const before = procCount(fetchMock, 'admin.store.getStoreById')
|
|
|
|
fireEvent.press(screen.getByText('Update Store'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.store.updateStore')
|
|
)
|
|
|
|
// (a) `refetch()` re-runs the getStoreById query this screen has mounted.
|
|
await waitFor(() =>
|
|
expect(procCount(fetchMock, 'admin.store.getStoreById')).toBeGreaterThan(
|
|
before
|
|
)
|
|
)
|
|
|
|
// (b) the list invalidation used the array query path, not the old dotted
|
|
// string: `queryClient.invalidateQueries({ queryKey: [['admin','store','getStores']] })`.
|
|
const paths = invalidateSpy.mock.calls.map((call) => {
|
|
const key = (call[0] as { queryKey?: unknown[] } | undefined)?.queryKey
|
|
return Array.isArray(key) ? key[0] : null
|
|
})
|
|
expect(paths).toContainEqual(['admin', 'store', 'getStores'])
|
|
})
|
|
})
|