/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 16.1 — Customize app * (`tests/pages/customize-app.test.tsx`). */ import React from 'react' import { Alert } from 'react-native' import { fireEvent, waitFor } from '@testing-library/react-native' import CustomizeApp from '@/app/(drawer)/dashboard/customize-app/index' import { renderWithProviders, flush, createTestQueryClient } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { lastMutationInput, lastProcedureInput, procedureMutationCalls, setProcedure, } from '../mocks/trpc-client' import { router } from '../mocks/expo-router-mock' import { BOUND_SETTINGS, BOUNDS, REGION, REGION_CONSTANTS } from '../fixtures' /** Backend returns `[{ key, value }]` rows; the harness fixture is a flat map. */ function regionRows(overrides: Record = {}) { return Object.entries({ ...REGION_CONSTANTS, ...overrides }).map(([key, value]) => ({ key, value, })) } async function renderPage() { const view = renderWithProviders(, { auth: false }) await flush() return view } async function openRegionTab(view: Awaited>) { fireEvent.press(view.getByText('Region')) await flush() } describe('Customize app (ACUST-01..26)', () => { beforeEach(() => { resetAllMocks() // `admin.region.current` returns the region, `bounds` a bare array and // `getRegionConstants` `[{ key, value }]` rows; the harness defaults wrap them. setProcedure('admin.region.current', { data: REGION }) setProcedure('admin.region.bounds', { data: BOUNDS }) setProcedure('admin.region.getBoundSettings', { data: BOUND_SETTINGS }) setProcedure('admin.region.getRegionConstants', { data: regionRows() }) }) afterEach(() => { jest.restoreAllMocks() }) it('ACUST-01: shows the bounds loading state', async () => { setProcedure('admin.region.bounds', { mode: 'pending' }) // The real batch link puts every mount-time query in one request, so a // pending bounds response would hold `region.current` too. Seed the header // (as an earlier screen would have) and keep only the bounds query in flight. const queryClient = createTestQueryClient() queryClient.setQueryData([['admin', 'region', 'current'], { type: 'query' }], REGION) const view = renderWithProviders(, { auth: false, queryClient }) expect(await view.findByText('Region: Mahabubnagar')).toBeTruthy() expect(view.getByText('Customize App')).toBeTruthy() expect(view.getByText('Loading bounds...')).toBeTruthy() }) it('ACUST-02: a bounds failure shows the error copy and Retry', async () => { setProcedure('admin.region.bounds', { mode: 'error', error: new Error('bounds boom') }) const view = await renderPage() expect(await view.findByText('Failed to load bounds')).toBeTruthy() expect(view.getByText('bounds boom')).toBeTruthy() fireEvent.press(view.getByText('Retry')) await flush() // The retry hook is re-invoked without throwing; the page stays on the error. expect(view.getByText('Failed to load bounds')).toBeTruthy() }) it('ACUST-03: no bounds shows the empty-state copy', async () => { setProcedure('admin.region.bounds', { data: [] }) const view = await renderPage() expect(await view.findByText('No bounds configured')).toBeTruthy() expect( view.getByText('This region has no bounds yet. Ask a superadmin to configure one.') ).toBeTruthy() }) it('ACUST-04: lists bounds with priority, current and effective labels', async () => { const view = await renderPage() expect(await view.findByText('Bound #7')).toBeTruthy() expect(view.getByText('Bound #8')).toBeTruthy() expect(view.getByText('Priority 1')).toBeTruthy() expect(view.getByText('Priority 10')).toBeTruthy() expect(view.getByText('Effective')).toBeTruthy() expect(view.getByText('Editing Bound #7 — settings apply to checkout')).toBeTruthy() }) it('ACUST-05: the lowest-priority bound is selected by default', async () => { const view = await renderPage() expect(await view.findByText(/Editing Bound #7/)).toBeTruthy() await waitFor(() => expect(lastProcedureInput('admin.region.getBoundSettings')).toEqual({ boundId: 7 }) ) // Switching to the other bound re-queries its settings and moves the editor. fireEvent.press(view.getByText('Bound #8')) expect(await view.findByText('Editing Bound #8')).toBeTruthy() await waitFor(() => expect(lastProcedureInput('admin.region.getBoundSettings')).toEqual({ boundId: 8 }) ) }) it('ACUST-06: the Bound/Region tabs switch the visible sections', async () => { const view = await renderPage() expect(await view.findByText('Free Delivery Threshold')).toBeTruthy() await openRegionTab(view) expect( await view.findByText('Region settings apply to every bound in this region.') ).toBeTruthy() expect(view.queryByText('Free Delivery Threshold')).toBeNull() fireEvent.press(view.getByText('Bound')) expect(await view.findByText('Free Delivery Threshold')).toBeTruthy() expect(view.queryByText('Region settings apply to every bound in this region.')).toBeNull() }) it('ACUST-07: the bound form shows threshold/charge/flash fields', async () => { const view = await renderPage() expect(await view.findByText('Free Delivery Threshold')).toBeTruthy() expect(view.getByText('Delivery Charge')).toBeTruthy() expect(view.getByText('Flash Free Delivery Threshold')).toBeTruthy() expect(view.getByText('Flash Delivery Charge')).toBeTruthy() expect(view.getByText('Enable Flash Delivery')).toBeTruthy() expect(view.getByDisplayValue('249')).toBeTruthy() expect(view.getByDisplayValue('15')).toBeTruthy() expect(view.getByDisplayValue('199')).toBeTruthy() expect(view.getByDisplayValue('25')).toBeTruthy() }) it('ACUST-08: the flash-delivery boolean renders Enabled/Disabled', async () => { const view = await renderPage() expect(await view.findByText('Enabled')).toBeTruthy() fireEvent.press(view.getByRole('checkbox')) expect(view.getByText('Disabled')).toBeTruthy() expect(view.queryByText('Enabled')).toBeNull() }) it('ACUST-09: saving a bound sends only the changed keys', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() fireEvent.changeText(await view.findByDisplayValue('15'), '20') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(lastMutationInput('admin.region.updateBoundSettings')).toEqual({ boundId: 7, settings: [{ key: 'deliveryCharge', value: '20' }], }) ) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Success', expect.stringContaining('settings successfully')) ) }) it('ACUST-10: saving without edits alerts and skips the API', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() fireEvent.press(await view.findByText('Save Changes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('No Changes', 'No settings were modified.')) expect(procedureMutationCalls('admin.region.updateBoundSettings')).toHaveLength(0) }) it('ACUST-11: a failing bound save alerts', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) jest.spyOn(console, 'error').mockImplementation(() => undefined) setProcedure('admin.region.updateBoundSettings', { mode: 'error', error: new Error('save boom'), }) const view = await renderPage() fireEvent.changeText(await view.findByDisplayValue('15'), '20') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith( 'Error', 'Failed to update settings. Please try again.' ) ) }) it('ACUST-12: empty/invalid numbers are parsed as 0', async () => { jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() const threshold = await view.findByDisplayValue('249') fireEvent.changeText(threshold, '') expect(view.getByDisplayValue('0')).toBeTruthy() fireEvent.changeText(view.getByDisplayValue('0'), 'not-a-number') expect(view.getByDisplayValue('0')).toBeTruthy() fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(lastMutationInput('admin.region.updateBoundSettings')).toEqual({ boundId: 7, settings: [{ key: 'freeDeliveryThreshold', value: '0' }], }) ) }) it('ACUST-13: region arrays are parsed from the comma-separated stored values', async () => { const view = await renderPage() await openRegionTab(view) // REGION_CONSTANTS stores '[2,5]' / '[2,5,7]'; parseConstValue splits on commas. expect(await view.findByText('Manage Popular Items (2 items)')).toBeTruthy() expect(view.getByText('Manage All Visible Items (3 items)')).toBeTruthy() }) it('ACUST-14: the region form shows links, counts, support and welcome copy', async () => { const view = await renderPage() await openRegionTab(view) expect(await view.findByText('Manage Popular Items (2 items)')).toBeTruthy() expect(view.getByText('Manage All Visible Items (3 items)')).toBeTruthy() expect(view.getByText('Support Mobile')).toBeTruthy() expect(view.getByDisplayValue('8688182552')).toBeTruthy() expect(view.getByText('Support Email')).toBeTruthy() expect(view.getByDisplayValue('support@freshyo.in')).toBeTruthy() expect(view.getByText('Welcome Message')).toBeTruthy() expect(view.getByDisplayValue('Welcome')).toBeTruthy() }) it('ACUST-15: the popular-items link navigates to the popular screen', async () => { const view = await renderPage() await openRegionTab(view) fireEvent.press(await view.findByText('Manage Popular Items (2 items)')) expect(router.push).toHaveBeenCalledWith( '/(drawer)/dashboard/customize-app/popular-items' ) }) it('ACUST-16: the all-items link navigates to the all-items screen', async () => { const view = await renderPage() await openRegionTab(view) fireEvent.press(await view.findByText('Manage All Visible Items (3 items)')) expect(router.push).toHaveBeenCalledWith( '/(drawer)/dashboard/customize-app/all-items-order' ) }) it('ACUST-17: saving the region sends only changed keys', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() await openRegionTab(view) fireEvent.changeText(await view.findByDisplayValue('8688182552'), '9999999999') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(lastMutationInput('admin.region.updateRegionConstants')).toEqual({ constants: [{ key: 'supportMobile', value: '9999999999' }], }) ) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith( 'Success', expect.stringContaining('region settings successfully') ) ) }) it('ACUST-18: saving without region edits alerts and skips the API', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() await openRegionTab(view) fireEvent.press(await view.findByText('Save Changes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('No Changes', 'No region settings were modified.') ) expect(procedureMutationCalls('admin.region.updateRegionConstants')).toHaveLength(0) }) it('ACUST-19: a failing region 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('region boom'), }) const view = await renderPage() await openRegionTab(view) fireEvent.changeText(await view.findByDisplayValue('8688182552'), '9999999999') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith( 'Error', 'Failed to update region settings. Please try again.' ) ) }) it('ACUST-20: missing bound settings fall back to numeric 0 / Disabled defaults', async () => { setProcedure('admin.region.getBoundSettings', { data: null }) const view = await renderPage() await waitFor(() => expect(view.getAllByDisplayValue('0')).toHaveLength(4)) expect(view.getByText('Disabled')).toBeTruthy() }) it('ACUST-21: a successful save refreshes the bound settings query', async () => { const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) setProcedure('admin.region.updateBoundSettings', { data: { success: true, updatedCount: 1 } }) const view = await renderPage() fireEvent.changeText(await view.findByDisplayValue('15'), '20') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(lastMutationInput('admin.region.updateBoundSettings')).toEqual({ boundId: 7, settings: [{ key: 'deliveryCharge', value: '20' }], }) ) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Success', 'Updated 1 settings successfully.') ) }) it('ACUST-22: shows the settings loading state while the bound is unknown', async () => { setProcedure('admin.region.getBoundSettings', { mode: 'pending' }) const view = await renderPage() expect(await view.findByText('Loading settings...')).toBeTruthy() expect(view.getByText(/Editing Bound #7/)).toBeTruthy() }) it('ACUST-23: a settings failure shows the error copy and Retry', async () => { setProcedure('admin.region.getBoundSettings', { mode: 'error', error: new Error('settings boom'), }) const view = await renderPage() expect(await view.findByText('Failed to load settings')).toBeTruthy() expect(view.getByText('settings boom')).toBeTruthy() fireEvent.press(view.getByText('Retry')) await flush() expect(view.getByText('Failed to load settings')).toBeTruthy() }) it('ACUST-24: shows the region-constants loading state', async () => { setProcedure('admin.region.getRegionConstants', { mode: 'pending' }) const view = await renderPage() await openRegionTab(view) expect(await view.findByText('Loading region settings...')).toBeTruthy() }) it('ACUST-25: the flash toggle serializes "true"/"false"', async () => { jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() fireEvent.press(await view.findByRole('checkbox')) expect(view.getByText('Disabled')).toBeTruthy() fireEvent.press(view.getByText('Save Changes')) // Flip the stored value so the inverse toggle is also a real change. setProcedure('admin.region.getBoundSettings', { data: { ...BOUND_SETTINGS, isFlashDeliveryEnabled: 'false' }, }) await waitFor(() => expect(view.getByText('Disabled')).toBeTruthy()) await waitFor(() => expect(procedureMutationCalls('admin.region.updateBoundSettings')).toHaveLength(1) ) fireEvent.press(view.getByRole('checkbox')) expect(view.getByText('Enabled')).toBeTruthy() fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect( procedureMutationCalls('admin.region.updateBoundSettings').map( (input: any) => input.settings ) ).toEqual([ [{ key: 'isFlashDeliveryEnabled', value: 'false' }], [{ key: 'isFlashDeliveryEnabled', value: 'true' }], ]) ) }) it('ACUST-26: a multiline welcome message is preserved verbatim', async () => { jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) const view = await renderPage() await openRegionTab(view) fireEvent.changeText(await view.findByDisplayValue('Welcome'), 'Line one\nLine two') fireEvent.press(view.getByText('Save Changes')) await waitFor(() => expect(lastMutationInput('admin.region.updateRegionConstants')).toEqual({ constants: [{ key: 'welcomeMessage', value: 'Line one\nLine two' }], }) ) }) })