mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
Add "use my current location" button to Journey entry editor
- One-shot getCurrentPositionOnce() helper in useGeolocation with typed error codes (unsupported, insecure context, permission denied, timeout) - Button inside the entry editor Location field fills coordinates from the device and reverse-geocodes the place name via /api/maps/reverse, falling back to formatted coordinates when no name is available - New journey.editor.* i18n keys in all 23 locales - Unit tests for the helper and component tests for the button flow
This commit is contained in:
@@ -3850,4 +3850,95 @@ describe('JourneyDetailPage', () => {
|
||||
expect(updateCalls).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
// ── FE-PAGE-JOURNEYDETAIL-154 ──────────────────────────────────────────
|
||||
describe('FE-PAGE-JOURNEYDETAIL-154: EntryEditor use-current-location button', () => {
|
||||
const originalGeolocation = Object.getOwnPropertyDescriptor(navigator, 'geolocation');
|
||||
|
||||
function stubGeolocation(getCurrentPosition: (success: PositionCallback, error?: PositionErrorCallback) => void) {
|
||||
Object.defineProperty(navigator, 'geolocation', {
|
||||
configurable: true,
|
||||
value: { getCurrentPosition: vi.fn(getCurrentPosition) },
|
||||
});
|
||||
}
|
||||
|
||||
const stubGeoSuccess = () =>
|
||||
stubGeolocation(success => success({
|
||||
coords: {
|
||||
latitude: 41.9, longitude: 12.5, accuracy: 10,
|
||||
heading: null, speed: null, altitude: null, altitudeAccuracy: null,
|
||||
},
|
||||
timestamp: now,
|
||||
} as unknown as GeolocationPosition));
|
||||
|
||||
afterEach(() => {
|
||||
if (originalGeolocation) {
|
||||
Object.defineProperty(navigator, 'geolocation', originalGeolocation);
|
||||
} else {
|
||||
delete (navigator as { geolocation?: unknown }).geolocation;
|
||||
}
|
||||
delete (window as { __addToast?: unknown }).__addToast;
|
||||
});
|
||||
|
||||
it('fills the location field from geolocation and reverse geocoding', async () => {
|
||||
stubGeoSuccess();
|
||||
server.use(
|
||||
http.get('/api/maps/reverse', () => {
|
||||
return HttpResponse.json({ name: 'Colosseum', address: 'Rome, Italy' });
|
||||
}),
|
||||
);
|
||||
|
||||
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
|
||||
await renderAndWait();
|
||||
await openEntryEditor(user);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Use my current location' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByPlaceholderText('Search location...')).toHaveValue('Colosseum');
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to coordinates when reverse geocoding returns nothing', async () => {
|
||||
stubGeoSuccess();
|
||||
server.use(
|
||||
http.get('/api/maps/reverse', () => {
|
||||
return HttpResponse.json({ name: null, address: null });
|
||||
}),
|
||||
);
|
||||
|
||||
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
|
||||
await renderAndWait();
|
||||
await openEntryEditor(user);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Use my current location' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByPlaceholderText('Search location...')).toHaveValue('41.90000, 12.50000');
|
||||
});
|
||||
});
|
||||
|
||||
it('shows an error toast when location permission is denied', async () => {
|
||||
const addToast = vi.fn();
|
||||
(window as { __addToast?: unknown }).__addToast = addToast;
|
||||
stubGeolocation((_success, error) => error?.({
|
||||
code: 1, message: 'denied',
|
||||
PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3,
|
||||
} as GeolocationPositionError));
|
||||
|
||||
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
|
||||
await renderAndWait();
|
||||
await openEntryEditor(user);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Use my current location' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(addToast).toHaveBeenCalledWith(
|
||||
'Location access was denied. Allow it in your browser settings and try again.',
|
||||
'error',
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user