freshyo/apps/user-ui/tests/pages/me-hub.test.tsx
2026-10-24 19:31:30 +05:30

290 lines
9.9 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 16 — Me hub (`tests/pages/me-hub.test.tsx`, ME-01..ME-19).
*
* ME-20 (the me-layout login gate) lives in `tests/pages/me-layout.test.tsx`.
*
* Rendered with the real `AuthProvider` (`{ auth: 'in' }` / `TEST_JWT`) so the
* self-data query populates `userDetails` and `NextOrderGlimpse` mounts. The
* navigation-target store is asserted through the shared
* `common-ui/hooks/useNavigationTarget` singleton.
*/
import React from 'react'
import { Linking, Modal } from 'react-native'
import { act, fireEvent, render, waitFor } from '@testing-library/react-native'
import { useNavigationTarget } from 'common-ui/hooks/useNavigationTarget'
import Me from '@/app/(drawer)/(tabs)/me/index'
import { flush, renderWithProviders, seedRegion } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { setProcedure } from '../mocks/trpc-client'
import { router } from '../mocks/expo-router-mock'
import { __snapshot } from '../mocks/secure-store-mock'
import { CONSTS, REGION_1 } from '../fixtures'
async function renderHub() {
const view = renderWithProviders(<Me />, { auth: 'in' })
await flush(4)
return view
}
const TARGET = '/(drawer)/(tabs)/me/my-orders/1001'
/** The navigation-target store is a module singleton; drive it through the hook. */
function navTargetStore() {
let api: ReturnType<typeof useNavigationTarget> | undefined
const Probe = () => {
api = useNavigationTarget()
return null
}
render(<Probe />)
return api as ReturnType<typeof useNavigationTarget>
}
function setNavigationTarget(target: string | null) {
const api = navTargetStore()
act(() => api.setNavigationTarget(target))
}
describe('Me hub (ME-01..ME-19)', () => {
beforeEach(() => {
resetAllMocks()
setNavigationTarget(null)
})
it('ME-01: renders the header, the three sections and every menu entry', async () => {
const view = await renderHub()
expect(view.getByText('My Account')).toBeTruthy()
expect(view.getByText('Manage your freshyo experience')).toBeTruthy()
expect(view.getByText('Shopping & Activity')).toBeTruthy()
expect(view.getByText('Saved Information')).toBeTruthy()
expect(view.getByText('Support')).toBeTruthy()
const entries = [
'me-menu-my-orders',
'me-menu-my-cart',
'me-menu-coupons',
'me-menu-addresses',
'me-menu-delivery-address',
'me-menu-profile-settings',
'me-menu-live-support',
'me-menu-help-complaints',
'me-menu-terms-conditions',
]
entries.forEach((id) => expect(view.getByTestId(id)).toBeTruthy())
})
it('ME-02: a selected region is shown in the Delivery Address subtitle', async () => {
seedRegion(REGION_1)
const view = await renderHub()
expect(view.getByText(`Delivering in ${REGION_1.name}`)).toBeTruthy()
})
it('ME-03: without a region the Delivery Address subtitle asks to choose one', async () => {
// Signed out: `useRegion` has no default address to adopt, so the region
// stays unset.
const view = renderWithProviders(<Me />, { auth: 'out' })
await flush(2)
expect(view.getByText('Choose or add an address')).toBeTruthy()
})
it('ME-04: My Orders pushes the my-orders route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-my-orders'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/my-orders')
})
it('ME-05: My Cart pushes the cart route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-my-cart'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/home/cart')
})
it('ME-06: Coupons pushes the coupons route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-coupons'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/coupons')
})
it('ME-07: Addresses pushes the addresses route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-addresses'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/addresses')
})
it('ME-08: Delivery Address opens the address switcher sheet', async () => {
const view = await renderHub()
expect(view.queryByTestId('location-add-address')).toBeNull()
fireEvent.press(view.getByTestId('me-menu-delivery-address'))
expect(view.getByTestId('location-add-address')).toBeTruthy()
})
it('ME-09: Profile Settings and the settings icon both push edit-profile', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-profile-settings'))
expect(router.push).toHaveBeenLastCalledWith('/(drawer)/(tabs)/me/edit-profile')
fireEvent.press(view.getByTestId('me-settings'))
expect(router.push).toHaveBeenLastCalledWith('/(drawer)/(tabs)/me/edit-profile')
expect(router.push).toHaveBeenCalledTimes(2)
})
it('ME-10: Help & Complaints pushes the complaints route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-help-complaints'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/complaints')
})
it('ME-11: Terms & Conditions pushes the terms route', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-terms-conditions'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/terms')
})
it('ME-12: Log Out calls the auth logout (the stored token is removed)', async () => {
const view = await renderHub()
expect(__snapshot().authToken).toBeTruthy()
fireEvent.press(view.getByTestId('me-logout'))
await waitFor(() => expect(__snapshot().authToken).toBeUndefined())
})
it('ME-13: a pending navigation target is replaced on focus', async () => {
setNavigationTarget(TARGET)
await renderHub()
expect(router.replace).toHaveBeenCalledWith(TARGET)
})
it('ME-14: with no navigation target nothing is replaced', async () => {
await renderHub()
expect(router.replace).not.toHaveBeenCalled()
})
it('ME-15: the target is consumed so a second focus does not replace again', async () => {
setNavigationTarget(TARGET)
const first = await renderHub()
expect(router.replace).toHaveBeenCalledTimes(1)
expect(navTargetStore().navigationTarget).toBeNull()
first.unmount()
router.replace.mockClear()
const second = await renderHub()
expect(router.replace).not.toHaveBeenCalled()
second.unmount()
})
it('ME-16: NextOrderGlimpse is mounted above the menu', async () => {
const view = await renderHub()
expect(view.getByTestId('next-order-glimpse')).toBeTruthy()
const tree = JSON.stringify(view.toJSON())
const glimpse = tree.indexOf('next-order-glimpse')
const menu = tree.indexOf('me-menu-my-orders')
expect(glimpse).toBeGreaterThan(-1)
expect(menu).toBeGreaterThan(-1)
expect(glimpse).toBeLessThan(menu)
})
it('ME-17: the address bar is absent; Delivery Address opens the switcher', async () => {
const view = await renderHub()
expect(view.queryByTestId('location-bar')).toBeNull()
fireEvent.press(view.getByTestId('me-menu-delivery-address'))
expect(view.getByTestId('location-add-address')).toBeTruthy()
})
it('ME-18: closing the address sheet dismisses it', async () => {
const view = await renderHub()
fireEvent.press(view.getByTestId('me-menu-delivery-address'))
expect(view.getByTestId('location-add-address')).toBeTruthy()
const modal = view.UNSAFE_getAllByType(Modal).find((candidate: any) => candidate.props.visible)
expect(modal).toBeTruthy()
await act(async () => {
fireEvent(modal!, 'requestClose')
})
expect(view.queryByTestId('location-add-address')).toBeNull()
})
it('ME-19: the footer shows the app version', async () => {
const view = await renderHub()
expect(view.getByTestId('me-version')).toBeTruthy()
expect(view.getByText('Version 1.2.0 • Freshyo App')).toBeTruthy()
})
it('ME-20: Live Support opens WhatsApp to the essential-consts support mobile', async () => {
seedRegion(REGION_1, 7)
const openURL = jest.spyOn(Linking, 'openURL').mockResolvedValue(true as any)
const view = await renderHub()
expect(await view.findByText('Chat on WhatsApp • 8688182552')).toBeTruthy()
fireEvent.press(view.getByTestId('me-menu-live-support'))
// Default fixtures leave O1/O2/O5/O6 upcoming (O3 cancelled, O4 delivered).
expect(openURL).toHaveBeenCalledWith(
'whatsapp://send?phone=918688182552&text=Hi%2C%20my%20orders%20are%20ORD-1001%2C%20ORD-1002%2C%20ORD-1005%2C%20ORD-1006'
)
// Placed directly below the Coupons entry.
const tree = JSON.stringify(view.toJSON())
expect(tree.indexOf('me-menu-live-support')).toBeGreaterThan(tree.indexOf('me-menu-coupons'))
})
it('ME-21: Live Support is a no-op when the support mobile is missing', async () => {
seedRegion(REGION_1, 7)
setProcedure('common.essentialConsts', {
data: { ...CONSTS, supportMobile: null },
})
const openURL = jest.spyOn(Linking, 'openURL').mockResolvedValue(true as any)
const view = await renderHub()
expect(view.getByText('Chat with us on WhatsApp')).toBeTruthy()
fireEvent.press(view.getByTestId('me-menu-live-support'))
expect(openURL).not.toHaveBeenCalled()
})
it('ME-22: with no upcoming orders Live Support opens WhatsApp without a message', async () => {
seedRegion(REGION_1, 7)
setProcedure('user.order.getOrders', {
data: { data: [], pagination: { totalPages: 1, total: 0, page: 1, pageSize: 50 } },
})
const openURL = jest.spyOn(Linking, 'openURL').mockResolvedValue(true as any)
const view = await renderHub()
expect(await view.findByText('Chat on WhatsApp • 8688182552')).toBeTruthy()
fireEvent.press(view.getByTestId('me-menu-live-support'))
expect(openURL).toHaveBeenCalledWith('whatsapp://send?phone=918688182552')
})
})