239 lines
7.7 KiB
TypeScript
239 lines
7.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 16.3 — All items order
|
|
* (`tests/pages/all-items-order.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent } from '@testing-library/react-native'
|
|
import DraggableFlatList from 'react-native-draggable-flatlist'
|
|
|
|
import AllItemsOrder from '@/app/(drawer)/dashboard/customize-app/all-items-order'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
lastMutationInput,
|
|
procedureMutationCalls,
|
|
setProcedure,
|
|
} from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { PRODUCTS_SUMMARY, REGION_CONSTANTS } from '../fixtures'
|
|
|
|
function constantRows(allItemsOrder?: string) {
|
|
return Object.entries({ ...REGION_CONSTANTS, allItemsOrder })
|
|
.filter(([, value]) => value !== undefined)
|
|
.map(([key, value]) => ({ key, value }))
|
|
}
|
|
|
|
async function renderPage() {
|
|
const view = renderWithProviders(<AllItemsOrder />, { auth: false })
|
|
await flush()
|
|
return view
|
|
}
|
|
|
|
function productNames(view: Awaited<ReturnType<typeof renderPage>>) {
|
|
return view
|
|
.getAllByText(/^(Chicken Breast|Cucumber|Global Paneer|Mutton|No Image Item|Mixed Stock)$/)
|
|
.map((node) => node.props.children)
|
|
}
|
|
|
|
function reorder(view: Awaited<ReturnType<typeof renderPage>>) {
|
|
const list = view.UNSAFE_getByType(DraggableFlatList as any)
|
|
const data = [...list.props.data].reverse()
|
|
act(() => {
|
|
list.props.onDragEnd({ data })
|
|
})
|
|
}
|
|
|
|
function isDisabled(node: any): boolean {
|
|
let current = node
|
|
while (current) {
|
|
const state = current.props?.accessibilityState
|
|
if (state && typeof state.disabled === 'boolean') return state.disabled
|
|
current = current.parent
|
|
}
|
|
return false
|
|
}
|
|
|
|
const disabled = (view: Awaited<ReturnType<typeof renderPage>>, text: string) =>
|
|
isDisabled(view.getByText(text))
|
|
|
|
describe('All items order (AALL-01..13)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('2,5') })
|
|
setProcedure('common.product.getAllProductsSummary', { data: PRODUCTS_SUMMARY })
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('AALL-01: shows the loading order copy', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { mode: 'pending' })
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('All Items Order')).toBeTruthy()
|
|
expect(view.getByText('Loading order...')).toBeTruthy()
|
|
})
|
|
|
|
it('AALL-02: constants/products failures show Error and Go Back returns', async () => {
|
|
setProcedure('admin.region.getRegionConstants', {
|
|
mode: 'error',
|
|
error: new Error('order boom'),
|
|
})
|
|
const first = await renderPage()
|
|
|
|
expect(first.getByText('Error')).toBeTruthy()
|
|
expect(first.getByText('Failed to load order')).toBeTruthy()
|
|
fireEvent.press(first.getByText('Go Back'))
|
|
expect(router.back).toHaveBeenCalledTimes(1)
|
|
first.unmount()
|
|
|
|
setProcedure('admin.region.getRegionConstants', {
|
|
mode: 'success',
|
|
data: constantRows('2,5'),
|
|
})
|
|
setProcedure('common.product.getAllProductsSummary', {
|
|
mode: 'error',
|
|
error: new Error('products boom'),
|
|
})
|
|
const second = await renderPage()
|
|
|
|
expect(second.getByText('Error')).toBeTruthy()
|
|
expect(second.getByText('Failed to load products')).toBeTruthy()
|
|
})
|
|
|
|
it('AALL-03: no products shows the empty copy', async () => {
|
|
setProcedure('common.product.getAllProductsSummary', { data: { products: [] } })
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('No products available')).toBeTruthy()
|
|
})
|
|
|
|
it('AALL-04: products listed in allItemsOrder first, then the rest', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('5,2') })
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toEqual([
|
|
'Cucumber',
|
|
'Chicken Breast',
|
|
'Global Paneer',
|
|
'Mutton',
|
|
'No Image Item',
|
|
'Mixed Stock',
|
|
])
|
|
})
|
|
|
|
it('AALL-05: unknown ids in allItemsOrder are skipped', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('999,2') })
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)[0]).toBe('Chicken Breast')
|
|
expect(productNames(view)).toHaveLength(6)
|
|
})
|
|
|
|
it('AALL-06: duplicate ids in allItemsOrder are deduped', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('2,2,5') })
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toHaveLength(6)
|
|
expect(view.getAllByText('Chicken Breast')).toHaveLength(1)
|
|
expect(view.getByText('Long press and drag to reorder • 6 items')).toBeTruthy()
|
|
})
|
|
|
|
it('AALL-07: drag reorder updates the working order', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)[0]).toBe('Chicken Breast')
|
|
reorder(view)
|
|
await flush()
|
|
|
|
expect(productNames(view)[0]).toBe('Mixed Stock')
|
|
expect(disabled(view, 'Save')).toBe(false)
|
|
})
|
|
|
|
it('AALL-08: saving persists allItemsOrder', async () => {
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const view = await renderPage()
|
|
|
|
reorder(view)
|
|
await flush()
|
|
fireEvent.press(view.getByText('Save'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.region.updateRegionConstants')).toEqual({
|
|
constants: [{ key: 'allItemsOrder', value: '8,7,6,9,5,2' }],
|
|
})
|
|
})
|
|
|
|
it('AALL-09: with no changes Save is disabled and nothing is saved', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(disabled(view, 'Save')).toBe(true)
|
|
fireEvent.press(view.getByText('Save'))
|
|
await flush()
|
|
|
|
expect(procedureMutationCalls('admin.region.updateRegionConstants')).toHaveLength(0)
|
|
})
|
|
|
|
it('AALL-10: a failed save alerts', async () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
setProcedure('admin.region.updateRegionConstants', {
|
|
mode: 'error',
|
|
error: new Error('save boom'),
|
|
})
|
|
const view = await renderPage()
|
|
|
|
reorder(view)
|
|
await flush()
|
|
fireEvent.press(view.getByText('Save'))
|
|
await flush()
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Error',
|
|
'Failed to update items order. Please try again.'
|
|
)
|
|
})
|
|
|
|
it('AALL-11: a product without an image renders the placeholder icon', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('No Image Item')).toBeTruthy()
|
|
expect(view.UNSAFE_getAllByProps({ name: 'image' }).length).toBeGreaterThanOrEqual(1)
|
|
})
|
|
|
|
it('AALL-12: the header reports the item count', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Long press and drag to reorder • 6 items')).toBeTruthy()
|
|
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('2') })
|
|
await flush()
|
|
expect(view.getByText('Long press and drag to reorder • 6 items')).toBeTruthy()
|
|
})
|
|
|
|
it('AALL-13: a long list renders and reorders', async () => {
|
|
const products = Array.from({ length: 20 }, (_, index) => ({
|
|
id: 100 + index,
|
|
name: `Product ${index + 1}`,
|
|
images: [],
|
|
isOutOfStock: index === 0,
|
|
}))
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('119,100') })
|
|
setProcedure('common.product.getAllProductsSummary', {
|
|
data: { products },
|
|
})
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Long press and drag to reorder • 20 items')).toBeTruthy()
|
|
expect(view.getByText('Product 20')).toBeTruthy()
|
|
|
|
reorder(view)
|
|
await flush()
|
|
|
|
expect(view.getByText('Long press and drag to reorder • 20 items')).toBeTruthy()
|
|
expect(disabled(view, 'Save')).toBe(false)
|
|
})
|
|
})
|