283 lines
11 KiB
TypeScript
283 lines
11 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* APP-01..APP-24 — app entry + every route `_layout.tsx`.
|
|
*
|
|
* The `expo-router` mock records navigation and renders layout primitives as
|
|
* pass-throughs, so the layouts can be mounted directly. `Stack` / `Tabs` /
|
|
* `Slot` are the mock components themselves: their props (and `Stack.Screen`'s)
|
|
* are read back with `UNSAFE_getByType` / `UNSAFE_getAllByType` because the
|
|
* screen elements render `null`.
|
|
*/
|
|
import React from 'react'
|
|
import { ActivityIndicator } from 'react-native'
|
|
import { fireEvent, render, waitFor } from '@testing-library/react-native'
|
|
import { MyTouchableOpacity, theme } from 'common-ui'
|
|
|
|
import Index from '@/app/index'
|
|
import AuthLayout from '@/app/(auth)/_layout'
|
|
import DrawerLayout from '@/app/(drawer)/_layout'
|
|
import FlashIndex from '@/app/(drawer)/(tabs)/flash-delivery/index'
|
|
import FlashBaseLayout from '@/app/(drawer)/(tabs)/flash-delivery/_layout'
|
|
import HomeLayout from '@/app/(drawer)/(tabs)/home/_layout'
|
|
import SearchResultsLayout from '@/app/(drawer)/(tabs)/home/search-results/_layout'
|
|
import ProductDetailLayout from '@/app/(drawer)/(tabs)/home/product-detail/_layout'
|
|
import SlotViewLayout from '@/app/(drawer)/(tabs)/home/slot-view/_layout'
|
|
import StoreDetailLayout from '@/app/(drawer)/(tabs)/stores/store-detail/_layout'
|
|
import CouponsLayout from '@/app/(drawer)/(tabs)/me/coupons/_layout'
|
|
import MeLayout from '@/app/(drawer)/(tabs)/me/_layout'
|
|
import MyOrdersLayout from '@/app/(drawer)/(tabs)/me/my-orders/_layout'
|
|
import FloatingCartBar from '@/components/floating-cart-bar'
|
|
|
|
import { renderWithProviders, seedJsonCache, seedCentralStores, seedRegion } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { Stack, Tabs, Slot, router, __setParams } from '../mocks/expo-router-mock'
|
|
import { __setAuthState } from '../mocks/auth-context'
|
|
import { useAppStore } from '@/src/store/appStore'
|
|
import { useStoreHeaderStore } from '@/src/store/storeHeaderStore'
|
|
import { setProcedure } from '../mocks/trpc-client'
|
|
import { CONSTS, CONSTS_FLASH_OFF } from '../fixtures'
|
|
|
|
jest.mock('@/src/contexts/AuthContext', () => require('../mocks/auth-context'))
|
|
|
|
function authState(next: Record<string, any>) {
|
|
__setAuthState({ isLoading: false, isAuthenticated: false, user: null, ...next })
|
|
}
|
|
|
|
function renderLayout(ui: React.ReactElement) {
|
|
seedRegion()
|
|
return renderWithProviders(ui, { auth: false })
|
|
}
|
|
|
|
const allScreens = (view: any) => view.UNSAFE_getAllByType(Stack.Screen)
|
|
|
|
describe('app entry + route layouts (APP-01..APP-24)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
})
|
|
|
|
it('APP-01: auth loading renders the large spinner', () => {
|
|
authState({ isLoading: true })
|
|
const view = renderWithProviders(<Index />, { auth: false })
|
|
|
|
expect(view.UNSAFE_getByType(ActivityIndicator)).toBeTruthy()
|
|
expect(router.replace).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('APP-02: settled authenticated auth redirects to the home tab', () => {
|
|
authState({ isAuthenticated: true })
|
|
renderWithProviders(<Index />, { auth: false })
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/(tabs)/home')
|
|
})
|
|
|
|
it('APP-03: settled signed-out auth still redirects home', () => {
|
|
authState({ isAuthenticated: false })
|
|
renderWithProviders(<Index />, { auth: false })
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/(tabs)/home')
|
|
})
|
|
|
|
it('APP-04: auth layout while loading shows a spinner and no stack', () => {
|
|
authState({ isLoading: true })
|
|
const view = renderWithProviders(<AuthLayout />, { auth: false })
|
|
|
|
expect(view.UNSAFE_getByType(ActivityIndicator)).toBeTruthy()
|
|
expect(view.UNSAFE_queryByType(Stack)).toBeNull()
|
|
})
|
|
|
|
it('APP-05: loaded auth layout renders the stack', () => {
|
|
authState({ isLoading: false })
|
|
const view = renderWithProviders(<AuthLayout />, { auth: false })
|
|
|
|
expect(view.UNSAFE_getByType(Stack)).toBeTruthy()
|
|
})
|
|
|
|
it('APP-06: drawer layout while loading shows the brand spinner', () => {
|
|
authState({ isLoading: true })
|
|
const view = renderWithProviders(<DrawerLayout />, { auth: false })
|
|
|
|
const spinner = view.UNSAFE_getByType(ActivityIndicator)
|
|
expect(spinner.props.color).toBe(theme.colors.brand500)
|
|
})
|
|
|
|
it('APP-07: loaded drawer layout declares the five tabs', () => {
|
|
authState({ isLoading: false, isAuthenticated: true })
|
|
const view = renderLayout(<DrawerLayout />)
|
|
|
|
const screens = view.UNSAFE_getAllByType(Tabs.Screen)
|
|
expect(screens).toHaveLength(5)
|
|
expect(screens.map((screen: any) => screen.props.name)).toEqual([
|
|
'(tabs)/home',
|
|
'(tabs)/order-again',
|
|
'(tabs)/flash-delivery',
|
|
'(tabs)/stores',
|
|
'(tabs)/me',
|
|
])
|
|
expect(screens[0].props.options.tabBarLabel).toBe('Home')
|
|
expect(screens[1].props.options.tabBarLabel).toBe('Offers')
|
|
expect(screens[3].props.options.tabBarLabel).toBe('Stores')
|
|
expect(screens[4].props.options.tabBarLabel).toBe('Me')
|
|
})
|
|
|
|
it('APP-08: a single true hideTabsNav key hides the tab bar', () => {
|
|
authState({ isAuthenticated: true })
|
|
useAppStore.setState({ hideTabsNav: { quick_delivery: true } })
|
|
const view = renderLayout(<DrawerLayout />)
|
|
|
|
const tabs = view.UNSAFE_getByType(Tabs)
|
|
expect(tabs.props.screenOptions.tabBarStyle).toEqual({ display: 'none' })
|
|
})
|
|
|
|
it('APP-09: all-false hide keys keep the tab bar visible', () => {
|
|
authState({ isAuthenticated: true })
|
|
useAppStore.setState({ hideTabsNav: { quick_delivery: false, cart: false } })
|
|
const view = renderLayout(<DrawerLayout />)
|
|
|
|
const tabs = view.UNSAFE_getByType(Tabs)
|
|
expect(tabs.props.screenOptions.tabBarStyle.display).toBeUndefined()
|
|
expect(tabs.props.screenOptions.tabBarStyle.backgroundColor).toBe('white')
|
|
})
|
|
|
|
it('APP-10: the centre flash tab renders the gradient button with its selected state', () => {
|
|
authState({ isAuthenticated: true })
|
|
const view = renderLayout(<DrawerLayout />)
|
|
|
|
const center = view
|
|
.UNSAFE_getAllByType(Tabs.Screen)
|
|
.find((screen: any) => screen.props.name === '(tabs)/flash-delivery')
|
|
const button = render(
|
|
center!.props.options.tabBarButton({ style: {}, accessibilityState: { selected: true } })
|
|
)
|
|
|
|
expect(button.getByText('1 Hr Delivery')).toBeTruthy()
|
|
expect(button.UNSAFE_getByType(MyTouchableOpacity).props.accessibilityState).toEqual({
|
|
selected: true,
|
|
})
|
|
})
|
|
|
|
it('APP-11: flash-delivery index redirects to the products route', () => {
|
|
renderWithProviders(<FlashIndex />, { auth: false })
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/(tabs)/flash-delivery/(products)')
|
|
})
|
|
|
|
it('APP-12: an undefined flash flag defaults to enabled and renders the slot tree', () => {
|
|
// `common.essentialConsts` never resolves -> `essentialConsts` is undefined.
|
|
setProcedure('common.essentialConsts', { mode: 'pending' })
|
|
const view = renderLayout(<FlashBaseLayout />)
|
|
|
|
expect(view.queryByText('1 Hr Delivery Unavailable')).toBeNull()
|
|
expect(view.UNSAFE_getByType(Slot)).toBeTruthy()
|
|
})
|
|
|
|
it('APP-13: isFlashDeliveryEnabled false shows the unavailable gate and replaces home', async () => {
|
|
setProcedure('common.essentialConsts', { data: CONSTS_FLASH_OFF })
|
|
const view = renderLayout(<FlashBaseLayout />)
|
|
|
|
await waitFor(() => expect(view.getByText('1 Hr Delivery Unavailable')).toBeTruthy())
|
|
fireEvent.press(view.getByText('Go to Scheduled Delivery'))
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/(drawer)/(tabs)/home')
|
|
})
|
|
|
|
it('APP-14: isFlashDeliveryEnabled true renders the Slot children', () => {
|
|
setProcedure('common.essentialConsts', { data: CONSTS })
|
|
const view = renderLayout(<FlashBaseLayout />)
|
|
|
|
expect(view.UNSAFE_getByType(Slot)).toBeTruthy()
|
|
expect(view.queryByText('1 Hr Delivery Unavailable')).toBeNull()
|
|
})
|
|
|
|
it('APP-15: a set product-detail title flows into the stack screen options', () => {
|
|
useStoreHeaderStore.setState({ title: 'Chicken Breast' })
|
|
const view = renderLayout(<ProductDetailLayout />)
|
|
|
|
const screen = allScreens(view).find((candidate: any) => candidate.props.name === '[id]')
|
|
expect(screen.props.options.title).toBe('Chicken Breast')
|
|
})
|
|
|
|
it('APP-16: an empty product-detail title falls back to Product Details', () => {
|
|
useStoreHeaderStore.setState({ title: '' })
|
|
const view = renderLayout(<ProductDetailLayout />)
|
|
|
|
const screen = allScreens(view).find((candidate: any) => candidate.props.name === '[id]')
|
|
expect(screen.props.options.title).toBe('Product Details')
|
|
})
|
|
|
|
it('APP-17: a set store title updates the header and hides the product-detail header', () => {
|
|
useStoreHeaderStore.setState({ title: 'The Freshyo Meat House' })
|
|
const view = renderLayout(<StoreDetailLayout />)
|
|
|
|
const screens = allScreens(view)
|
|
const store = screens.find((candidate: any) => candidate.props.name === '[id]')
|
|
const productDetail = screens.find(
|
|
(candidate: any) => candidate.props.name === 'product-detail'
|
|
)
|
|
expect(store.props.options.title).toBe('The Freshyo Meat House')
|
|
expect(productDetail.props.options.headerShown).toBe(false)
|
|
})
|
|
|
|
it('APP-18: coupons opened from the cart hide the header back button', () => {
|
|
__setParams({ from: 'cart' })
|
|
const view = renderLayout(<CouponsLayout />)
|
|
|
|
expect(allScreens(view)[0].props.options.headerBackVisible).toBe(false)
|
|
})
|
|
|
|
it('APP-19: coupons opened without a source keep the header back button', () => {
|
|
__setParams({})
|
|
const view = renderLayout(<CouponsLayout />)
|
|
|
|
expect(allScreens(view)[0].props.options.headerBackVisible).toBe(true)
|
|
})
|
|
|
|
it('APP-20: me layout while loading shows the loading copy', () => {
|
|
authState({ isLoading: true })
|
|
const view = renderWithProviders(<MeLayout />, { auth: false })
|
|
|
|
expect(view.getByText('Loading...')).toBeTruthy()
|
|
})
|
|
|
|
it('APP-21: me layout signed out shows the login wall', () => {
|
|
authState({ isLoading: false, isAuthenticated: false })
|
|
const view = renderWithProviders(<MeLayout />, { auth: false })
|
|
|
|
expect(view.getByText('Log in to view your profile, orders and more.')).toBeTruthy()
|
|
})
|
|
|
|
it('APP-22: me layout signed in renders the stack', () => {
|
|
authState({ isLoading: false, isAuthenticated: true })
|
|
const view = renderWithProviders(<MeLayout />, { auth: false })
|
|
|
|
expect(view.UNSAFE_getByType(Stack)).toBeTruthy()
|
|
})
|
|
|
|
it('APP-23: the my-orders stack carries both titles', () => {
|
|
const view = renderLayout(<MyOrdersLayout />)
|
|
|
|
const stack = view.UNSAFE_getByType(Stack)
|
|
const detail = allScreens(view).find((candidate: any) => candidate.props.name === '[id]')
|
|
expect(stack.props.screenOptions.title).toBe('My Orders')
|
|
expect(detail.props.options.title).toBe('Order Details')
|
|
})
|
|
|
|
it('APP-24: the floating cart bar is mounted by the layouts that include it', () => {
|
|
seedJsonCache()
|
|
seedCentralStores()
|
|
|
|
const search = renderLayout(<SearchResultsLayout />)
|
|
expect(search.UNSAFE_getByType(FloatingCartBar)).toBeTruthy()
|
|
search.unmount()
|
|
|
|
const productDetail = renderLayout(<ProductDetailLayout />)
|
|
expect(productDetail.UNSAFE_getByType(FloatingCartBar)).toBeTruthy()
|
|
productDetail.unmount()
|
|
|
|
const home = renderLayout(<HomeLayout />)
|
|
expect(home.UNSAFE_queryByType(FloatingCartBar)).toBeNull()
|
|
home.unmount()
|
|
|
|
const slotView = renderLayout(<SlotViewLayout />)
|
|
expect(slotView.UNSAFE_getByType(FloatingCartBar)).toBeTruthy()
|
|
})
|
|
})
|