221 lines
7.4 KiB
TypeScript
221 lines
7.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 5.2 — Rebalance orders
|
|
* (`tests/pages/rebalance-orders.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert, RefreshControl } from 'react-native'
|
|
import { fireEvent, act, waitFor } from '@testing-library/react-native'
|
|
import dayjs from 'dayjs'
|
|
|
|
import RebalanceOrders from '@/app/(drawer)/dashboard/rebalance-orders/index'
|
|
import { renderWithProviders, renderAndFlush, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { SLOT_1 } from '../fixtures'
|
|
|
|
const HOUR = 60 * 60 * 1000
|
|
const iso = (offset: number) => new Date(Date.now() + offset).toISOString()
|
|
|
|
function renderRebalance() {
|
|
return renderAndFlush(<RebalanceOrders />, { auth: 'in' })
|
|
}
|
|
|
|
function rebalanceButton(view: Awaited<ReturnType<typeof renderRebalance>>) {
|
|
return view.getByTestId('rebalance-button')
|
|
}
|
|
|
|
describe('Rebalance orders (AREB-01..13)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('AREB-01: shows the loading copy', () => {
|
|
setProcedure('admin.slots.getAll', { mode: 'pending' })
|
|
const view = renderWithProviders(<RebalanceOrders />, { auth: 'in' })
|
|
|
|
// Loading assertions stay synchronous: the query never settles.
|
|
expect(view.getByText('Loading slots...')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-02: shows the empty copy when there are no upcoming slots', async () => {
|
|
setProcedure('admin.slots.getAll', { data: { slots: [], count: 0 } })
|
|
const view = await renderRebalance()
|
|
|
|
expect(view.getByText('No upcoming slots available for rebalancing.')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-03: past slots are filtered out', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: {
|
|
slots: [
|
|
{ ...SLOT_1, id: 1700, deliveryTime: iso(-2 * HOUR), freezeTime: iso(-3 * HOUR) },
|
|
SLOT_1,
|
|
],
|
|
count: 2,
|
|
},
|
|
})
|
|
const view = await renderRebalance()
|
|
|
|
expect(view.queryByText('Slot #1700')).toBeNull()
|
|
expect(view.getByText('Slot #1849')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-04: renders upcoming slot id, delivery/freeze and active status', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
expect(view.getByText('Slot #1849')).toBeTruthy()
|
|
expect(view.getByText('ID: 1849')).toBeTruthy()
|
|
expect(view.getByText(dayjs(SLOT_1.deliveryTime).format('DD MMM, h:mm A'))).toBeTruthy()
|
|
expect(view.getByText(dayjs(SLOT_1.freezeTime).format('DD MMM, h:mm A'))).toBeTruthy()
|
|
expect(view.getAllByText('Active').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.getAllByText('Inactive').length).toBeGreaterThanOrEqual(1)
|
|
})
|
|
|
|
it('AREB-05: more than two products shows "+N more" and opens the dialog', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: {
|
|
slots: [
|
|
{
|
|
...SLOT_1,
|
|
products: [
|
|
{ id: 2, name: 'Chicken Breast' },
|
|
{ id: 5, name: 'Cucumber' },
|
|
{ id: 7, name: 'Paneer' },
|
|
{ id: 9, name: 'Mutton' },
|
|
],
|
|
},
|
|
],
|
|
count: 1,
|
|
},
|
|
})
|
|
const view = await renderRebalance()
|
|
|
|
expect(view.getByText('+2 more')).toBeTruthy()
|
|
fireEvent.press(view.getByText('+2 more'))
|
|
expect(view.getByText('All Products')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-06: selecting and deselecting a slot toggles the selection', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.order.rebalanceSlots')).toEqual({ slotIds: [1849] })
|
|
)
|
|
|
|
// Deselect the slot: the disabled button can no longer submit.
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
await flush(2)
|
|
expect(procedureMutationCalls('admin.order.rebalanceSlots')).toHaveLength(1)
|
|
})
|
|
|
|
it('AREB-07: with no selection the rebalance button is disabled', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
expect(rebalanceButton(view).props.accessibilityState?.disabled).toBe(true)
|
|
fireEvent.press(rebalanceButton(view))
|
|
await flush(2)
|
|
|
|
expect(procedureMutationCalls('admin.order.rebalanceSlots')).toHaveLength(0)
|
|
})
|
|
|
|
it('AREB-08: rebalancing alerts and sends the selected slot ids', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Rebalancing...',
|
|
'Please wait while we rebalance the selected slots.',
|
|
[{ text: 'OK' }]
|
|
)
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.order.rebalanceSlots')).toEqual({ slotIds: [1849] })
|
|
)
|
|
})
|
|
|
|
it('AREB-09: a successful rebalance shows "Rebalance Complete"', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith('Rebalance Complete', 'Slots have been rebalanced.')
|
|
)
|
|
})
|
|
|
|
it('AREB-10: a failed rebalance still alerts on settle', async () => {
|
|
setProcedure('admin.order.rebalanceSlots', { mode: 'error', error: new Error('boom') })
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith('Rebalance Complete', 'Slots have been rebalanced.')
|
|
)
|
|
})
|
|
|
|
it('AREB-11: the products dialog lists every product name', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: {
|
|
slots: [
|
|
{
|
|
...SLOT_1,
|
|
products: [
|
|
{ id: 2, name: 'Chicken Breast' },
|
|
{ id: 5, name: 'Cucumber' },
|
|
{ id: 7, name: 'Paneer' },
|
|
],
|
|
},
|
|
],
|
|
count: 1,
|
|
},
|
|
})
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByText('+1 more'))
|
|
|
|
expect(view.getByText('All Products')).toBeTruthy()
|
|
expect(view.getByText('Chicken Breast')).toBeTruthy()
|
|
expect(view.getByText('Cucumber')).toBeTruthy()
|
|
expect(view.getByText('Paneer')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-12: pull to refresh runs the refresh handler', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
const refreshControl = view.UNSAFE_getByType(RefreshControl)
|
|
expect(typeof refreshControl.props.onRefresh).toBe('function')
|
|
|
|
await act(async () => {
|
|
await refreshControl.props.onRefresh()
|
|
})
|
|
|
|
// The handler runs its refetch cycle and leaves the list rendered.
|
|
expect(view.UNSAFE_getByType(RefreshControl).props.refreshing).toBe(false)
|
|
expect(view.getByText('Slot #1849')).toBeTruthy()
|
|
})
|
|
|
|
it('AREB-13: multiple selected slots are all sent in the payload', async () => {
|
|
const view = await renderRebalance()
|
|
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1849'))
|
|
fireEvent.press(view.getByTestId('rebalance-slot-1850'))
|
|
fireEvent.press(rebalanceButton(view))
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.order.rebalanceSlots')).toEqual({ slotIds: [1849, 1850] })
|
|
)
|
|
})
|
|
})
|