freshyo/apps/user-ui/tests/components/location-wrapper.test.tsx
2026-10-13 19:10:27 +05:30

85 lines
2.5 KiB
TypeScript

/**
* LocationTestWrapper (components/LocationTestWrapper.tsx).
*
* It is the region gate: once a region is selected it is a passthrough,
* otherwise it derives the region from the location/default address and falls
* back to the address chooser with a notice explaining why.
*/
import React from 'react'
import { Text } from 'react-native'
import { screen, waitFor } from '@testing-library/react-native'
import LocationTestWrapper from '@/components/LocationTestWrapper'
import { useLocationStore } from '@/src/store/locationStore'
import { useRegionStore } from '@/src/store/regionStore'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { renderWithProviders } from '../helpers/render'
function renderGate() {
return renderWithProviders(
<LocationTestWrapper>
<Text>app content</Text>
</LocationTestWrapper>,
{ centralStores: false }
)
}
describe('LocationTestWrapper', () => {
beforeEach(async () => {
const Location = require('expo-location')
Location.requestForegroundPermissionsAsync.mockResolvedValue({
status: 'granted',
granted: true,
})
Location.hasServicesEnabledAsync.mockResolvedValue(true)
Location.getCurrentPositionAsync.mockResolvedValue({
coords: { latitude: 17.385, longitude: 78.4867, accuracy: 5 },
timestamp: Date.now(),
})
await resetStorage()
useLocationStore.getState().stop()
useRegionStore.setState({
selectedRegionId: null,
selectedRegion: null,
selectedBoundId: null,
regionGeneration: 0,
})
})
it('renders its children once a region is selected', async () => {
setupApiMocks()
useRegionStore
.getState()
.setRegion({ id: 1, name: 'Test Region', description: null })
renderGate()
expect(screen.getByText('app content')).toBeVisible()
})
it('shows a spinner while the region is still resolving', () => {
setupApiMocks()
renderGate()
expect(screen.getByText('Finding your region...')).toBeVisible()
})
it('asks for an address with a location notice when the location is denied', async () => {
const Location = require('expo-location')
Location.requestForegroundPermissionsAsync.mockResolvedValue({
status: 'denied',
granted: false,
})
setupApiMocks()
renderGate()
await waitFor(() => expect(screen.getByText('Delivery Address')).toBeVisible())
expect(screen.getByText(/Location access is not granted/)).toBeVisible()
expect(screen.getByTestId('open-location-settings')).toBeVisible()
})
})