/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 7.2 — Slot details (`tests/pages/slot-details.test.tsx`). */ import React from 'react' import { Alert, Share } from 'react-native' import { fireEvent } from '@testing-library/react-native' import dayjs from 'dayjs' import SlotDetails from '@/app/(drawer)/dashboard/slots/slot-details' import { renderWithProviders, flush } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { setProcedure } from '../mocks/trpc-client' import { __setParams, router } from '../mocks/expo-router-mock' import { SLOT_DETAIL } from '../fixtures' function renderDetails() { return renderWithProviders(, { auth: 'in' }) } const SNIPPET = SLOT_DETAIL.vendorSnippets[0] describe('Slot details (ASLOTDET-01..10)', () => { let alertSpy: jest.SpyInstance beforeEach(() => { resetAllMocks() __setParams({ slotId: '1849' }) alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) jest.spyOn(Share, 'share').mockResolvedValue({ action: 'sharedAction' } as any) }) afterEach(() => { alertSpy?.mockRestore() jest.restoreAllMocks() }) it('ASLOTDET-01: shows the loading copy', () => { setProcedure('admin.slots.getSlotById', { mode: 'pending' }) const view = renderDetails() expect(view.getByText('Loading slot details...')).toBeTruthy() }) it('ASLOTDET-02: a query error shows the error copy', () => { setProcedure('admin.slots.getSlotById', { mode: 'error', error: new Error('boom') }) const view = renderDetails() expect(view.getByText('Error loading slot details')).toBeTruthy() }) it('ASLOTDET-03: with no vendor snippets the empty copy is shown', () => { setProcedure('admin.slots.getSlotById', { data: { slot: { ...SLOT_DETAIL, vendorSnippets: [] } }, }) const view = renderDetails() expect(view.getByText('No vendor snippets for this slot')).toBeTruthy() expect(view.getByText('0 snippets')).toBeTruthy() }) it('ASLOTDET-04: renders snippet code, expiry and share action', () => { const view = renderDetails() expect(view.getByText('ABC123')).toBeTruthy() expect(view.getByText('1 snippets')).toBeTruthy() expect(view.getByText('Share')).toBeTruthy() expect(view.getByText(`Expires ${dayjs(SNIPPET.validTill).format('MMM DD')}`)).toBeTruthy() }) it('ASLOTDET-05: renders product names/count and the empty copy', () => { const view = renderDetails() expect(view.getByText('2 items')).toBeTruthy() expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.getByText('Cucumber')).toBeTruthy() view.unmount() setProcedure('admin.slots.getSlotById', { data: { slot: { ...SLOT_DETAIL, products: [], vendorSnippets: [] } }, }) const empty = renderDetails() expect(empty.getByText('0 items')).toBeTruthy() expect(empty.getByText('No products in this slot')).toBeTruthy() }) it('ASLOTDET-06: the edit FAB opens the edit route', () => { const view = renderDetails() fireEvent.press(view.getByTestId('edit-slot-fab')) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots/edit/1849') }) it('ASLOTDET-07: a failed share alerts', async () => { ;(Share.share as jest.Mock).mockRejectedValue(new Error('share failed')) const view = renderDetails() fireEvent.press(view.getByText('Share')) await flush(2) expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to share link') }) it('ASLOTDET-08: a successful share passes the access URL', async () => { const view = renderDetails() fireEvent.press(view.getByText('Share')) await flush(2) expect(Share.share).toHaveBeenCalledWith({ message: SNIPPET.accessUrl }) }) it('ASLOTDET-09: a snippet without a link shares an undefined message', async () => { setProcedure('admin.slots.getSlotById', { data: { slot: { ...SLOT_DETAIL, vendorSnippets: [{ ...SNIPPET, accessUrl: undefined }], }, }, }) const view = renderDetails() fireEvent.press(view.getByText('Share')) await flush(2) expect(Share.share).toHaveBeenCalledWith({ message: undefined }) }) it('ASLOTDET-10: the snippet products dialog lists the matching products', () => { setProcedure('admin.slots.getSlotById', { data: { slot: { ...SLOT_DETAIL, vendorSnippets: [{ ...SNIPPET, skuIds: [2, 5] }], }, }, }) const view = renderDetails() fireEvent.press(view.getByText('2 products')) expect(view.getByText('Snippet Products')).toBeTruthy() expect(view.getAllByText('Chicken Breast').length).toBeGreaterThanOrEqual(2) expect(view.getAllByText('Cucumber').length).toBeGreaterThanOrEqual(2) }) })