171 lines
5.7 KiB
TypeScript
171 lines
5.7 KiB
TypeScript
/**
|
|
* Customize App hub (app/(drawer)/dashboard/customize-app/index.tsx) — renders
|
|
* the visible constants, links to the popular-items / all-items-order editors,
|
|
* and saves only the constants that changed.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import CustomizeApp from '@/app/(drawer)/dashboard/customize-app/index'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import {
|
|
expectProcedureCalled,
|
|
lastProcedureInput,
|
|
procCount,
|
|
} from '../mocks/procedures'
|
|
import { routerMock } from '../mocks/router'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const constants = [
|
|
{ key: 'deliveryCharge', value: 10 },
|
|
{ key: 'supportMobile', value: '9999999999' },
|
|
{ key: 'isFlashDeliveryEnabled', value: false },
|
|
{ key: 'popularItems', value: [1, 2] },
|
|
{ key: 'allItemsOrder', value: [3] },
|
|
]
|
|
|
|
const renderHub = () =>
|
|
renderWithProviders(<CustomizeApp />)
|
|
|
|
describe('Customize App hub', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
setupApiMocks()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('shows the loading state while constants load', () => {
|
|
setupApiMocks({
|
|
procedures: { 'admin.const.getConstants': () => new Promise(() => {}) },
|
|
})
|
|
renderHub()
|
|
|
|
expect(screen.getByText('Loading constants...')).toBeVisible()
|
|
})
|
|
|
|
it('shows the failed state when constants are missing', async () => {
|
|
renderHub()
|
|
|
|
await waitFor(() => expect(screen.getByText('Failed to load constants')).toBeVisible())
|
|
})
|
|
|
|
it('renders the visible constants and section links', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'admin.const.getConstants': constants },
|
|
})
|
|
renderHub()
|
|
|
|
expect(await screen.findByText('Customize App Constants')).toBeVisible()
|
|
expect(screen.getByText('Delivery Charge')).toBeVisible()
|
|
expect(screen.getByText('Support Mobile')).toBeVisible()
|
|
expect(screen.getByText('Enable Flash Delivery')).toBeVisible()
|
|
expect(screen.getByText('Manage Popular Items (2 items)')).toBeVisible()
|
|
expect(screen.getByText('Manage All Visible Items (1 items)')).toBeVisible()
|
|
expect(screen.getByText('Save Changes')).toBeVisible()
|
|
})
|
|
|
|
it('navigates to the popular items editor', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'admin.const.getConstants': constants },
|
|
})
|
|
renderHub()
|
|
|
|
fireEvent.press(await screen.findByText('Manage Popular Items (2 items)'))
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
'/(drawer)/dashboard/customize-app/popular-items'
|
|
)
|
|
})
|
|
|
|
it('navigates to the all items order editor', async () => {
|
|
setupApiMocks({
|
|
procedures: { 'admin.const.getConstants': constants },
|
|
})
|
|
renderHub()
|
|
|
|
fireEvent.press(await screen.findByText('Manage All Visible Items (1 items)'))
|
|
expect(routerMock.push).toHaveBeenCalledWith(
|
|
'/(drawer)/dashboard/customize-app/all-items-order'
|
|
)
|
|
})
|
|
|
|
it('alerts when saving without changes and skips the mutation', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: { 'admin.const.getConstants': constants },
|
|
})
|
|
renderHub()
|
|
|
|
fireEvent.press(await screen.findByText('Save Changes'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('No Changes', 'No constants were modified.')
|
|
)
|
|
expect(procCount(api.fetchMock, 'admin.const.updateConstants')).toBe(0)
|
|
})
|
|
|
|
it('saves only the changed constant', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'admin.const.getConstants': constants,
|
|
'admin.const.updateConstants': { success: true, updatedCount: 1, keys: ['deliveryCharge'] },
|
|
},
|
|
})
|
|
renderHub()
|
|
|
|
await screen.findByText('Delivery Charge')
|
|
fireEvent.changeText(screen.getAllByPlaceholderText('Enter number')[0], '25')
|
|
fireEvent.press(screen.getByText('Save Changes'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.const.updateConstants'))
|
|
expect(lastProcedureInput(api.fetchMock, 'admin.const.updateConstants')).toEqual({
|
|
constants: [{ key: 'deliveryCharge', value: 25 }],
|
|
})
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith(
|
|
'Success',
|
|
'Updated 1 constants successfully.'
|
|
)
|
|
)
|
|
})
|
|
|
|
it('toggles a boolean constant and saves it', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'admin.const.getConstants': constants,
|
|
'admin.const.updateConstants': { success: true, updatedCount: 1, keys: ['isFlashDeliveryEnabled'] },
|
|
},
|
|
})
|
|
renderHub()
|
|
|
|
await screen.findByText('Enable Flash Delivery')
|
|
expect(screen.getByText('Disabled')).toBeVisible()
|
|
fireEvent.press(screen.getByRole('checkbox'))
|
|
await waitFor(() => expect(screen.getByText('Enabled')).toBeVisible())
|
|
|
|
fireEvent.press(screen.getByText('Save Changes'))
|
|
|
|
await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.const.updateConstants'))
|
|
expect(lastProcedureInput(api.fetchMock, 'admin.const.updateConstants')).toEqual({
|
|
constants: [{ key: 'isFlashDeliveryEnabled', value: true }],
|
|
})
|
|
})
|
|
|
|
it('shows the updating label while the mutation is pending', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'admin.const.getConstants': constants,
|
|
'admin.const.updateConstants': () => new Promise(() => {}),
|
|
},
|
|
})
|
|
renderHub()
|
|
|
|
await screen.findByText('Delivery Charge')
|
|
fireEvent.changeText(screen.getAllByPlaceholderText('Enter number')[0], '25')
|
|
fireEvent.press(screen.getByText('Save Changes'))
|
|
|
|
await waitFor(() => expect(screen.getByText('Updating...')).toBeVisible())
|
|
})
|
|
})
|