Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion __tests__/components/FindHelpEntry.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,36 @@ describe('FindHelpEntry', () => {
await waitFor(() => {
expect(alertSpy).toHaveBeenCalledWith(expect.stringMatching(/something went wrong/i));
});
});
});

it('uses browser geolocation when available', async () => {
const mockGetCurrentPosition = jest.fn((success) => {
success({ coords: { latitude: 10, longitude: 20 } });
});
Object.defineProperty(global.navigator, 'geolocation', {
value: { getCurrentPosition: mockGetCurrentPosition },
configurable: true,
});

renderWithProvider(<FindHelpEntry organisation={mockOrgWithNoServices} />);

await waitFor(() => {
expect(mockGetCurrentPosition).toHaveBeenCalled();
expect(screen.getByText(/location set/i)).toBeInTheDocument();
});
});

it('shows postcode form when geolocation fails', async () => {
const mockGetCurrentPosition = jest.fn((_s, error) => error());
Object.defineProperty(global.navigator, 'geolocation', {
value: { getCurrentPosition: mockGetCurrentPosition },
configurable: true,
});

renderWithProvider(<FindHelpEntry organisation={mockOrgWithNoServices} />);

await waitFor(() => {
expect(screen.getByLabelText(/enter your postcode/i)).toBeInTheDocument();
});
});
});
54 changes: 54 additions & 0 deletions __tests__/contexts/LocationContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import React from 'react';
import { LocationProvider, useLocation } from '@/contexts/LocationContext';

function Consumer() {
const { location, setLocation, requestLocation } = useLocation();
return (
<div>
<span data-testid="loc">{location ? JSON.stringify(location) : 'null'}</span>
<button onClick={() => setLocation({ lat: 1, lng: 2 })}>set</button>
<button onClick={requestLocation}>request</button>
</div>
);
}

describe('LocationContext', () => {
it('throws if useLocation is used outside provider', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
expect(() => render(<Consumer />)).toThrow('useLocation must be used within a LocationProvider');
(console.error as jest.Mock).mockRestore();
});

it('updates location via setLocation', () => {
render(
<LocationProvider>
<Consumer />
</LocationProvider>
);
fireEvent.click(screen.getByText('set'));
expect(screen.getByTestId('loc').textContent).toBe('{"lat":1,"lng":2}');
});

it('requests location using geolocation API', async () => {
const mockGetCurrentPosition = jest.fn((success) => {
success({ coords: { latitude: 3, longitude: 4 } });
});
Object.defineProperty(global.navigator, 'geolocation', {
value: { getCurrentPosition: mockGetCurrentPosition },
configurable: true,
});

render(
<LocationProvider>
<Consumer />
</LocationProvider>
);

fireEvent.click(screen.getByText('request'));

await waitFor(() => {
expect(screen.getByTestId('loc').textContent).toBe('{"lat":3,"lng":4}');
});
});
});
Loading