154 lines
4.8 KiB
TypeScript
154 lines
4.8 KiB
TypeScript
/**
|
|
* Slot details screen (app/(drawer)/dashboard/slots/slot-details.tsx) —
|
|
* loading/error states, snippet + product rendering, share and navigation.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { Alert, Share } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import SlotDetails from '@/app/(drawer)/dashboard/slots/slot-details'
|
|
import { setupApiMocks, resetStorage } from '../mocks/api'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures'
|
|
import { routerMock, searchParamsMock } from '../mocks/router'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const slot = {
|
|
id: 7,
|
|
deliveryTime: '2026-10-01T10:00:00.000Z',
|
|
freezeTime: '2026-10-01T06:00:00.000Z',
|
|
isActive: true,
|
|
isCapacityFull: false,
|
|
groupIds: [2],
|
|
products: [
|
|
{ id: 11, name: 'Chicken' },
|
|
{ id: 12, name: 'Mutton' },
|
|
{ id: 13, name: 'Fish', shortDescription: 'Fresh catch' },
|
|
],
|
|
vendorSnippets: [
|
|
{
|
|
id: 100,
|
|
snippetCode: 'SNIP-AAA',
|
|
slotId: 7,
|
|
skuIds: [11, 13],
|
|
isPermanent: false,
|
|
validTill: '2030-01-01T00:00:00.000Z',
|
|
createdAt: '2026-09-01T00:00:00.000Z',
|
|
accessUrl: 'https://app.freshyo.in/vendor-order-list?id=SNIP-AAA',
|
|
},
|
|
],
|
|
}
|
|
|
|
const procedures = {
|
|
'admin.slots.getSlotById': { slot },
|
|
}
|
|
|
|
describe('Slot details', () => {
|
|
beforeEach(async () => {
|
|
resetStaffAuthMock()
|
|
await resetStorage()
|
|
searchParamsMock.current = { slotId: '7' }
|
|
setupApiMocks({ procedures })
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
jest.spyOn(Share, 'share').mockResolvedValue({ action: 'sharedAction' } as any)
|
|
routerMock.push.mockClear()
|
|
})
|
|
|
|
it('shows a loader while fetching', () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
fetchMock.mockImplementation(() => new Promise(() => {}))
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
|
|
expect(screen.getByText('Loading slot details...')).toBeVisible()
|
|
})
|
|
|
|
it('shows an error state when the slot is missing', async () => {
|
|
setupApiMocks({ procedures: { 'admin.slots.getSlotById': null } })
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
|
|
expect(await screen.findByText('Error loading slot details')).toBeVisible()
|
|
})
|
|
|
|
it('renders slot header, products and snippets', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
|
|
expect(await screen.findByText('Slot #7')).toBeVisible()
|
|
expect(lastProcedureInput(fetchMock, 'admin.slots.getSlotById')).toEqual({ id: 7 })
|
|
|
|
expect(screen.getByText('3 items')).toBeVisible()
|
|
expect(screen.getByText('Chicken')).toBeVisible()
|
|
expect(screen.getByText('Mutton')).toBeVisible()
|
|
expect(screen.getByText('Fish')).toBeVisible()
|
|
|
|
expect(screen.getByText('1 snippets')).toBeVisible()
|
|
expect(screen.getByText('SNIP-AAA')).toBeVisible()
|
|
expect(screen.getByText('2 products')).toBeVisible()
|
|
expect(screen.getByText('Share')).toBeVisible()
|
|
})
|
|
|
|
it('renders empty states when there are no snippets or products', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'admin.slots.getSlotById': {
|
|
slot: { ...slot, products: [], vendorSnippets: [] },
|
|
},
|
|
},
|
|
})
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
|
|
expect(await screen.findByText('No vendor snippets for this slot')).toBeVisible()
|
|
expect(screen.getByText('No products in this slot')).toBeVisible()
|
|
})
|
|
|
|
it('opens the snippet products dialog with only the snippet skus', async () => {
|
|
setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
await screen.findByText('Slot #7')
|
|
|
|
expect(screen.queryByText('Snippet Products')).toBeNull()
|
|
fireEvent.press(screen.getByText('2 products'))
|
|
|
|
expect(await screen.findByText('Snippet Products')).toBeVisible()
|
|
expect(screen.getByText('Fresh catch')).toBeVisible()
|
|
expect(screen.getAllByText('Chicken').length).toBe(2)
|
|
expect(screen.getAllByText('Fish').length).toBe(2)
|
|
expect(screen.getAllByText('Mutton').length).toBe(1)
|
|
})
|
|
|
|
it('shares the snippet access url', async () => {
|
|
setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
await screen.findByText('Slot #7')
|
|
|
|
fireEvent.press(screen.getByText('Share'))
|
|
|
|
await waitFor(() =>
|
|
expect(Share.share).toHaveBeenCalledWith({
|
|
message: 'https://app.freshyo.in/vendor-order-list?id=SNIP-AAA',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('navigates to the edit screen from the FAB', async () => {
|
|
setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<SlotDetails />)
|
|
await screen.findByText('Slot #7')
|
|
|
|
fireEvent.press(screen.getByTestId('edit-slot-fab'))
|
|
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
'/(drawer)/dashboard/slots/edit/7'
|
|
)
|
|
})
|
|
})
|