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

64 lines
2 KiB
TypeScript

/**
* LocationAttacher (src/components/LocationAttacher.tsx).
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import * as Location from 'expo-location'
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
import { LocationAttacher } from '@/src/components/LocationAttacher'
import { renderWithProviders } from '../helpers/render'
describe('LocationAttacher', () => {
it('attaches the current location on success', async () => {
const onLocationChange = jest.fn()
renderWithProviders(<LocationAttacher onLocationChange={onLocationChange} />, {
centralStores: false,
})
fireEvent.press(screen.getByText('Attach Current Location'))
await waitFor(() =>
expect(onLocationChange).toHaveBeenCalledWith({
latitude: 17.385,
longitude: 78.4867,
})
)
expect(screen.getByText('✓ Location Attached')).toBeVisible()
})
it('shows an error when permission is denied', async () => {
;(Location.requestForegroundPermissionsAsync as jest.Mock).mockResolvedValueOnce({
status: 'denied',
granted: false,
})
const onLocationChange = jest.fn()
renderWithProviders(<LocationAttacher onLocationChange={onLocationChange} />, {
centralStores: false,
})
fireEvent.press(screen.getByText('Attach Current Location'))
await waitFor(() =>
expect(screen.getByText('Location Permission denied')).toBeVisible()
)
expect(onLocationChange).not.toHaveBeenCalled()
})
it('does nothing while disabled', () => {
const onLocationChange = jest.fn()
renderWithProviders(
<LocationAttacher onLocationChange={onLocationChange} disabled />,
{ centralStores: false }
)
fireEvent.press(screen.getByText('Attach Current Location'))
expect(
(Location.requestForegroundPermissionsAsync as jest.Mock).mock.calls.length
).toBe(0)
expect(onLocationChange).not.toHaveBeenCalled()
})
})