145 lines
5 KiB
TypeScript
145 lines
5 KiB
TypeScript
/* 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, waitFor } from '@testing-library/react-native'
|
|
import dayjs from 'dayjs'
|
|
|
|
import SlotDetails from '@/app/(drawer)/dashboard/slots/slot-details'
|
|
import { renderWithProviders, renderAndFlush } 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 renderAndFlush(<SlotDetails />, { 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 = renderWithProviders(<SlotDetails />, { auth: 'in' })
|
|
|
|
// Loading assertions stay synchronous: the query never settles.
|
|
expect(view.getByText('Loading slot details...')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTDET-02: a query error shows the error copy', async () => {
|
|
setProcedure('admin.slots.getSlotById', { mode: 'error', error: new Error('boom') })
|
|
const view = await renderDetails()
|
|
|
|
expect(await view.findByText('Error loading slot details')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTDET-03: with no vendor snippets the empty copy is shown', async () => {
|
|
setProcedure('admin.slots.getSlotById', {
|
|
data: { slot: { ...SLOT_DETAIL, vendorSnippets: [] } },
|
|
})
|
|
const view = await 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', async () => {
|
|
const view = await 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', async () => {
|
|
const view = await 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 = await 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', async () => {
|
|
const view = await 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 = await renderDetails()
|
|
|
|
fireEvent.press(view.getByText('Share'))
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to share link'))
|
|
})
|
|
|
|
it('ASLOTDET-08: a successful share passes the access URL', async () => {
|
|
const view = await renderDetails()
|
|
|
|
fireEvent.press(view.getByText('Share'))
|
|
|
|
await waitFor(() => 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 = await renderDetails()
|
|
|
|
fireEvent.press(view.getByText('Share'))
|
|
|
|
await waitFor(() => expect(Share.share).toHaveBeenCalledWith({ message: undefined }))
|
|
})
|
|
|
|
it('ASLOTDET-10: the snippet products dialog lists the matching products', async () => {
|
|
setProcedure('admin.slots.getSlotById', {
|
|
data: {
|
|
slot: {
|
|
...SLOT_DETAIL,
|
|
vendorSnippets: [{ ...SNIPPET, skuIds: [2, 5] }],
|
|
},
|
|
},
|
|
})
|
|
const view = await 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)
|
|
})
|
|
})
|