163 lines
5.5 KiB
TypeScript
163 lines
5.5 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 4.2 — Variant stock dialog (`src/components/VariantStockDialog.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { fireEvent } from '@testing-library/react-native'
|
|
import Toast from 'react-native-toast-message'
|
|
|
|
import VariantStockDialog from '@/src/components/VariantStockDialog'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { lastMutationInput, procedureMutationCalls, setProcedure } from '../mocks/trpc-client'
|
|
import { P_CHICKEN, P_CUCUMBER, P_PARTIAL } from '../fixtures'
|
|
|
|
function renderDialog(overrides: Partial<React.ComponentProps<typeof VariantStockDialog>> = {}) {
|
|
const onClose = jest.fn()
|
|
const onApplied = jest.fn()
|
|
const view = renderWithProviders(
|
|
<VariantStockDialog
|
|
open
|
|
product={P_CHICKEN as any}
|
|
onClose={onClose}
|
|
onApplied={onApplied}
|
|
{...overrides}
|
|
/>,
|
|
{ auth: false }
|
|
)
|
|
return { view, onClose, onApplied }
|
|
}
|
|
|
|
describe('Variant stock dialog (AVSTOCK-01..12)', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('AVSTOCK-01: opens with the product name and all variants selected', () => {
|
|
const { view } = renderDialog()
|
|
|
|
expect(view.getByText('Chicken Breast')).toBeTruthy()
|
|
expect(view.getByText('Variants (2/2)')).toBeTruthy()
|
|
expect(view.getByTestId('variant-stock-11').props.accessibilityState.checked).toBe(true)
|
|
expect(view.getByTestId('variant-stock-12').props.accessibilityState.checked).toBe(true)
|
|
})
|
|
|
|
it('AVSTOCK-02: shows the amber partial-state copy', () => {
|
|
const { view } = renderDialog({ product: P_PARTIAL as any })
|
|
|
|
expect(view.getByText('1 of 2 variants are out of stock')).toBeTruthy()
|
|
})
|
|
|
|
it('AVSTOCK-03: selecting/deselecting a variant updates the count', () => {
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByTestId('variant-stock-11'))
|
|
|
|
expect(view.getByText('Variants (1/2)')).toBeTruthy()
|
|
expect(view.getByTestId('variant-stock-11').props.accessibilityState.checked).toBe(false)
|
|
})
|
|
|
|
it('AVSTOCK-04: Clear all / Select all toggles every variant', () => {
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Clear all'))
|
|
expect(view.getByText('Variants (0/2)')).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByText('Select all'))
|
|
expect(view.getByText('Variants (2/2)')).toBeTruthy()
|
|
})
|
|
|
|
it('AVSTOCK-05: with no selection both apply buttons are disabled', () => {
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Clear all'))
|
|
|
|
expect(view.getByText('Mark out of stock')).toBeDisabled()
|
|
expect(view.getByText('Mark in stock')).toBeDisabled()
|
|
})
|
|
|
|
it('AVSTOCK-06: Mark out sends the selected variants as out of stock', async () => {
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Mark out of stock'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.product.toggleOutOfStock')).toEqual({
|
|
id: 2,
|
|
skuIds: [11, 12],
|
|
isOutOfStock: true,
|
|
})
|
|
})
|
|
|
|
it('AVSTOCK-07: Mark in sends isOutOfStock false', async () => {
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Mark in stock'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.product.toggleOutOfStock')).toEqual({
|
|
id: 2,
|
|
skuIds: [11, 12],
|
|
isOutOfStock: false,
|
|
})
|
|
})
|
|
|
|
it('AVSTOCK-08: success shows a toast, applies and closes', async () => {
|
|
const { view, onClose, onApplied } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Mark out of stock'))
|
|
await flush()
|
|
|
|
expect(Toast.show).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'success', text1: 'Stock updated' })
|
|
)
|
|
expect(onApplied).toHaveBeenCalled()
|
|
expect(onClose).toHaveBeenCalled()
|
|
})
|
|
|
|
it('AVSTOCK-09: a failed mutation shows an error toast and keeps the dialog open', async () => {
|
|
setProcedure('admin.product.toggleOutOfStock', { mode: 'error', error: new Error('nope') })
|
|
const { view, onClose, onApplied } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Mark out of stock'))
|
|
await flush()
|
|
|
|
expect(Toast.show).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'error', text1: 'nope' })
|
|
)
|
|
expect(onApplied).not.toHaveBeenCalled()
|
|
expect(onClose).not.toHaveBeenCalled()
|
|
expect(view.getByText('Variants (2/2)')).toBeTruthy()
|
|
})
|
|
|
|
it('AVSTOCK-10: pending mutations disable the apply buttons', async () => {
|
|
setProcedure('admin.product.toggleOutOfStock', { mode: 'pending' })
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Mark out of stock'))
|
|
await flush()
|
|
|
|
expect(view.getByText('Mark out of stock')).toBeDisabled()
|
|
expect(view.getByText('Mark in stock')).toBeDisabled()
|
|
})
|
|
|
|
it('AVSTOCK-11: Cancel only closes the dialog', async () => {
|
|
const { view, onClose } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Cancel'))
|
|
await flush()
|
|
|
|
expect(onClose).toHaveBeenCalled()
|
|
expect(procedureMutationCalls('admin.product.toggleOutOfStock')).toHaveLength(0)
|
|
})
|
|
|
|
it('AVSTOCK-12: changing the product while open resets the selection', () => {
|
|
const { view } = renderDialog()
|
|
|
|
view.rerender(
|
|
<VariantStockDialog open product={P_CUCUMBER as any} onClose={jest.fn()} onApplied={jest.fn()} />
|
|
)
|
|
|
|
expect(view.getByText('Variants (1/1)')).toBeTruthy()
|
|
expect(view.getByTestId('variant-stock-13').props.accessibilityState.checked).toBe(true)
|
|
expect(view.queryByTestId('variant-stock-11')).toBeNull()
|
|
})
|
|
})
|