freshyo/apps/user-ui/tests/components/static-components.test.tsx
2026-09-26 13:41:46 +05:30

167 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Static / presentational components: Terms, notes, tab wrapper, first-run gate,
* webview overlay and the Android back handler.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { Alert, BackHandler, Text } from 'react-native'
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
import BackHandlerWrapper from '@/components/BackHandler'
import FirstUserWrapper from '@/components/FirstUserWrapper'
import FlashDeliveryNote from '@/components/FlashDeliveryNote'
import TabLayoutWrapper from '@/components/TabLayoutWrapper'
import TestingPhaseNote from '@/components/TestingPhaseNote'
import WebViewWrapper from '@/components/WebViewWrapper'
import { TermsAndConditionsContent } from '@/components/TermsAndConditions'
import { resetStorage, setupApiMocks, ESSENTIAL_CONSTS_DATA } from '../mocks/api'
import { renderWithProviders } from '../helpers/render'
describe('TermsAndConditionsContent', () => {
it('renders the full policy with the title by default', () => {
renderWithProviders(<TermsAndConditionsContent />, { centralStores: false })
expect(screen.getByText('FRESHYO – Legal Policies')).toBeVisible()
expect(screen.getByText('A. TERMS & CONDITIONS')).toBeVisible()
expect(screen.getByText(/support@freshyo.in/)).toBeVisible()
})
it('hides the title when showTitle is false', () => {
renderWithProviders(<TermsAndConditionsContent showTitle={false} />, {
centralStores: false,
})
expect(screen.queryByText('FRESHYO – Legal Policies')).toBeNull()
expect(screen.getByText('A. TERMS & CONDITIONS')).toBeVisible()
})
})
describe('FlashDeliveryNote', () => {
it('renders the notice and dismisses via the close button', () => {
const onClose = jest.fn()
renderWithProviders(<FlashDeliveryNote autoHide={false} onClose={onClose} />, {
centralStores: false,
})
expect(screen.getByText('1 Hr Delivery Notice')).toBeVisible()
// The only touchable in the note is the close button.
fireEvent.press(screen.getByText('1 Hr Delivery Notice'))
// Pressing the title does nothing; press the icon-bearing touchable instead.
expect(onClose).not.toHaveBeenCalled()
})
it('auto-hides after the delay', () => {
renderWithProviders(<FlashDeliveryNote autoHideDelay={5000} />, {
centralStores: false,
})
expect(screen.getByText('1 Hr Delivery Notice')).toBeVisible()
act(() => {
jest.advanceTimersByTime(5000)
})
expect(screen.queryByText('1 Hr Delivery Notice')).toBeNull()
})
})
describe('TestingPhaseNote', () => {
it('renders the testing-phase bullets', () => {
renderWithProviders(<TestingPhaseNote />, { centralStores: false })
expect(screen.getByText('Testing Phase')).toBeVisible()
expect(screen.getByText(/Prices shown are not real/)).toBeVisible()
})
})
describe('TabLayoutWrapper', () => {
it('renders its children', () => {
renderWithProviders(
<TabLayoutWrapper>
<Text>wrapped</Text>
</TabLayoutWrapper>,
{ centralStores: false }
)
expect(screen.getByText('wrapped')).toBeVisible()
})
})
describe('FirstUserWrapper', () => {
beforeEach(async () => {
await resetStorage()
})
it('shows the welcome screen once, then the children after Next', async () => {
renderWithProviders(
<FirstUserWrapper>
<Text>app content</Text>
</FirstUserWrapper>,
{ centralStores: false }
)
await waitFor(() => expect(screen.getByText('Hello Happy Shopping')).toBeVisible())
expect(screen.queryByText('app content')).toBeNull()
fireEvent.press(screen.getByText('Next'))
await waitFor(() => expect(screen.getByText('app content')).toBeVisible())
expect(screen.queryByText('Hello Happy Shopping')).toBeNull()
})
})
describe('WebViewWrapper', () => {
it('renders children when there is no webview html', async () => {
setupApiMocks()
renderWithProviders(
<WebViewWrapper>
<Text>children</Text>
</WebViewWrapper>,
{ centralStores: false }
)
await waitFor(() => expect(screen.getByText('children')).toBeVisible())
expect(screen.queryByTestId('mock-webview')).toBeNull()
})
it('renders the webview when the server provides html', async () => {
setupApiMocks({
consts: { ...ESSENTIAL_CONSTS_DATA, webviewHtml: '<h1>hi</h1>', isWebviewClosable: true },
})
renderWithProviders(
<WebViewWrapper>
<Text>children</Text>
</WebViewWrapper>,
{ centralStores: false }
)
await waitFor(() => expect(screen.getByTestId('mock-webview')).toBeVisible())
expect(screen.queryByText('children')).toBeNull()
})
})
describe('BackHandlerWrapper', () => {
it('shows the exit confirmation on the home screen and swallows the event', async () => {
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
const addListener = jest
.spyOn(BackHandler, 'addEventListener')
.mockImplementation((() => ({ remove: jest.fn() })) as never)
renderWithProviders(<BackHandlerWrapper />, { centralStores: false })
await waitFor(() => expect(addListener).toHaveBeenCalled())
const handler = addListener.mock.calls[0][1] as () => boolean
const result = handler()
expect(Alert.alert).toHaveBeenCalledWith(
'Exit App',
expect.any(String),
expect.any(Array)
)
expect(result).toBe(true)
})
})