327 lines
11 KiB
TypeScript
327 lines
11 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 16.2 — Popular items
|
|
* (`tests/pages/popular-items.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent, waitFor } from '@testing-library/react-native'
|
|
import DraggableFlatList from 'react-native-draggable-flatlist'
|
|
|
|
import PopularItems from '@/app/(drawer)/dashboard/customize-app/popular-items'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
lastMutationInput,
|
|
procedureMutationCalls,
|
|
procedureQueryCalls,
|
|
setProcedure,
|
|
} from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { PRODUCTS_SUMMARY, REGION_CONSTANTS } from '../fixtures'
|
|
|
|
function constantRows(popularItems?: string) {
|
|
return Object.entries({ ...REGION_CONSTANTS, popularItems })
|
|
.filter(([, value]) => value !== undefined)
|
|
.map(([key, value]) => ({ key, value }))
|
|
}
|
|
|
|
async function renderPage() {
|
|
const view = renderWithProviders(<PopularItems />, { 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 })
|
|
})
|
|
}
|
|
|
|
/** TouchableOpacity encodes `disabled` in accessibilityState on its host View. */
|
|
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))
|
|
|
|
function pressIcon(view: Awaited<ReturnType<typeof renderPage>>, name: string, index = 0) {
|
|
fireEvent.press(view.UNSAFE_getAllByProps({ name })[index])
|
|
}
|
|
|
|
function openAddDialog(view: Awaited<ReturnType<typeof renderPage>>) {
|
|
pressIcon(view, 'add')
|
|
}
|
|
|
|
describe('Popular items (APOP-01..16)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('2,5') })
|
|
setProcedure('common.product.getAllProductsSummary', { data: PRODUCTS_SUMMARY })
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('APOP-01: shows the loading copy while constants load', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { mode: 'pending' })
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Popular Items')).toBeTruthy()
|
|
expect(view.getByText('Loading constants...')).toBeTruthy()
|
|
})
|
|
|
|
it('APOP-02: constants/products failures show Error and Go Back returns', async () => {
|
|
setProcedure('admin.region.getRegionConstants', {
|
|
mode: 'error',
|
|
error: new Error('constants boom'),
|
|
})
|
|
const first = await renderPage()
|
|
|
|
expect(first.getByText('Error')).toBeTruthy()
|
|
expect(first.getByText('Failed to load constants')).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('APOP-03: no popular items shows the empty copy', async () => {
|
|
setProcedure('admin.region.getRegionConstants', {
|
|
data: [{ key: 'supportMobile', value: '1' }],
|
|
})
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('No popular items configured')).toBeTruthy()
|
|
expect(view.getByText('Add products to display as popular items')).toBeTruthy()
|
|
})
|
|
|
|
it('APOP-04: renders the stored products in their stored order', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('5,2') })
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toEqual(['Cucumber', 'Chicken Breast'])
|
|
expect(view.getByText('Long press an item to drag and reorder')).toBeTruthy()
|
|
})
|
|
|
|
it('APOP-05: ids missing from the product summary are skipped', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('2,999') })
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toEqual(['Chicken Breast'])
|
|
})
|
|
|
|
it('APOP-06: drag reorder changes the working order', async () => {
|
|
const view = await renderPage()
|
|
expect(productNames(view)).toEqual(['Chicken Breast', 'Cucumber'])
|
|
|
|
reorder(view)
|
|
await flush()
|
|
|
|
expect(productNames(view)).toEqual(['Cucumber', 'Chicken Breast'])
|
|
// The reorder marks the form dirty so the Save action becomes available.
|
|
expect(disabled(view, 'Save')).toBe(false)
|
|
})
|
|
|
|
it('APOP-07: deleting confirms and removes the product', async () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const view = await renderPage()
|
|
|
|
pressIcon(view, 'delete', 0)
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Remove Product',
|
|
'Are you sure you want to remove this product from popular items?',
|
|
expect.any(Array)
|
|
)
|
|
expect(productNames(view)).toEqual(['Chicken Breast', 'Cucumber'])
|
|
|
|
const buttons = alertSpy.mock.calls[0][2] as any[]
|
|
act(() => {
|
|
buttons.find((button: any) => button.text === 'Remove')?.onPress?.()
|
|
})
|
|
|
|
expect(productNames(view)).toEqual(['Cucumber'])
|
|
})
|
|
|
|
it('APOP-08: the add action opens the product selector dialog', async () => {
|
|
const view = await renderPage()
|
|
|
|
openAddDialog(view)
|
|
|
|
expect(view.getByText('Add Popular Items')).toBeTruthy()
|
|
expect(view.getByText('Select products to add to popular items')).toBeTruthy()
|
|
expect(view.getByText('Choose products...')).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByText('Choose products...'))
|
|
await flush()
|
|
expect(view.getByText('Chicken Breast - ₹215')).toBeTruthy()
|
|
expect(view.getByText('Cucumber - ₹15')).toBeTruthy()
|
|
})
|
|
|
|
it('APOP-09: Add is disabled until at least one product is selected', async () => {
|
|
const view = await renderPage()
|
|
openAddDialog(view)
|
|
|
|
expect(disabled(view, 'Add Products')).toBe(true)
|
|
|
|
fireEvent.press(view.getByText('Choose products...'))
|
|
await flush()
|
|
fireEvent.press(view.getByText('Chicken Breast - ₹215'))
|
|
fireEvent.press(view.getByText('Done'))
|
|
|
|
expect(disabled(view, 'Add 1 Product')).toBe(false)
|
|
})
|
|
|
|
it('APOP-10: selecting multiple SKUs updates the Add label; the id mismatch pins the gap', async () => {
|
|
const view = await renderPage()
|
|
openAddDialog(view)
|
|
|
|
fireEvent.press(view.getByText('Choose products...'))
|
|
await flush()
|
|
fireEvent.press(view.getByText('Chicken Breast - ₹215'))
|
|
fireEvent.press(view.getByText('Cucumber - ₹15'))
|
|
fireEvent.press(view.getByText('Done'))
|
|
|
|
expect(view.getByText('Add 2 Products')).toBeTruthy()
|
|
fireEvent.press(view.getByText('Add 2 Products'))
|
|
|
|
// Catalogue gap #2: the selector emits SKU ids (11/13) while the page looks
|
|
// products up by product id (2/5), so nothing is appended today.
|
|
expect(view.queryByText('Add Popular Items')).toBeNull()
|
|
expect(productNames(view)).toEqual(['Chicken Breast', 'Cucumber'])
|
|
expect(disabled(view, 'Save')).toBe(true)
|
|
})
|
|
|
|
it('APOP-11: a product already in the list is not added twice', async () => {
|
|
setProcedure('admin.region.getRegionConstants', { data: constantRows('11') })
|
|
setProcedure('common.product.getAllProductsSummary', {
|
|
data: {
|
|
products: [
|
|
{ id: 11, name: 'Chicken Breast', images: [] },
|
|
{ id: 5, name: 'Cucumber', images: [] },
|
|
],
|
|
},
|
|
})
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toEqual(['Chicken Breast'])
|
|
|
|
openAddDialog(view)
|
|
fireEvent.press(view.getByText('Choose products...'))
|
|
await flush()
|
|
fireEvent.press(view.getByText('Chicken Breast - ₹215'))
|
|
fireEvent.press(view.getByText('Done'))
|
|
fireEvent.press(view.getByText('Add 1 Product'))
|
|
|
|
expect(view.getAllByText('Chicken Breast')).toHaveLength(1)
|
|
expect(disabled(view, 'Save')).toBe(true)
|
|
})
|
|
|
|
it('APOP-12: saving persists the reordered popular items', 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: 'popularItems', value: '5,2' }],
|
|
})
|
|
})
|
|
|
|
it('APOP-13: with no changes Save is disabled and no mutation is sent', 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('APOP-14: 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()
|
|
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Error',
|
|
'Failed to update popular items. Please try again.'
|
|
)
|
|
)
|
|
})
|
|
|
|
it('APOP-15: the picker is SKU-based while the list/payload is product-id based', async () => {
|
|
const view = await renderPage()
|
|
|
|
openAddDialog(view)
|
|
fireEvent.press(view.getByText('Choose products...'))
|
|
await flush()
|
|
|
|
// Options come from getAllSkusSummary: SKU ids 11/13.
|
|
expect(procedureQueryCalls('common.product.getAllSkusSummary').length).toBeGreaterThan(0)
|
|
|
|
fireEvent.press(view.getByText('Chicken Breast - ₹215'))
|
|
fireEvent.press(view.getByText('Done'))
|
|
fireEvent.press(view.getByText('Add 1 Product'))
|
|
|
|
// SKU 11 does not resolve against product-summary id 2, so the stored list
|
|
// (and any later popularItems payload) stays in the product-id space.
|
|
expect(productNames(view)).toEqual(['Chicken Breast', 'Cucumber'])
|
|
expect(disabled(view, 'Save')).toBe(true)
|
|
})
|
|
|
|
it('APOP-16: a long list renders and can be reordered', async () => {
|
|
setProcedure('admin.region.getRegionConstants', {
|
|
data: constantRows('2,5,9,6,7,8'),
|
|
})
|
|
const view = await renderPage()
|
|
|
|
expect(productNames(view)).toHaveLength(6)
|
|
reorder(view)
|
|
await flush()
|
|
|
|
expect(productNames(view)).toHaveLength(6)
|
|
expect(productNames(view)[0]).toBe('Mixed Stock')
|
|
expect(disabled(view, 'Save')).toBe(false)
|
|
})
|
|
})
|