generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Authorizations list GPS location toggle (#205)
- Loading branch information
1 parent
0460a83
commit b5f85be
Showing
13 changed files
with
245 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react' | ||
import { screen } from '@testing-library/react' | ||
import { LatLngTuple } from 'leaflet' | ||
|
||
import { render } from '@/test-utils' | ||
import { useUserLocation } from '@/features/omrr/omrr-slice' | ||
import { LocationIconButton } from './LocationIconButton' | ||
import { Mock } from 'vitest' | ||
|
||
interface State { | ||
userLocation?: LatLngTuple | ||
} | ||
|
||
describe('Test suite for LocationIconButton', () => { | ||
it('should render LocationIconButton with geolocation granted', async () => { | ||
const state: State = {} | ||
const TestComponent = () => { | ||
Object.assign(state, { | ||
userLocation: useUserLocation(), | ||
}) | ||
return <LocationIconButton /> | ||
} | ||
const { user } = render(<TestComponent />, { | ||
withStateProvider: true, | ||
}) | ||
|
||
// Initially hidden, but the permission will be 'granted' | ||
const btn = await screen.findByTitle('Sort results by my location') | ||
const icon = screen.getByTitle('My location icon') | ||
expect(icon).not.toHaveClass('search-input-icon--active') | ||
expect(state.userLocation).toBeUndefined() | ||
expect(navigator.permissions.query).toHaveBeenCalledOnce() | ||
expect(navigator.permissions.query).toHaveBeenCalledWith({ | ||
name: 'geolocation', | ||
}) | ||
expect(navigator.geolocation.getCurrentPosition).not.toHaveBeenCalled() | ||
|
||
await user.click(btn) | ||
expect(icon).toHaveClass('search-input-icon--active') | ||
// See test-setup.ts | ||
expect(state.userLocation).toEqual([48, -123]) | ||
expect(navigator.geolocation.getCurrentPosition).toHaveBeenCalledOnce() | ||
|
||
await user.click(btn) | ||
expect(icon).not.toHaveClass('search-input-icon--active') | ||
expect(state.userLocation).toBeUndefined() | ||
}) | ||
|
||
it('should render LocationIconButton with geolocation prompt then denied', async () => { | ||
// The getCurrentPosition function is actually a vi.fn() - see test-utils.ts | ||
const getLocationMock = navigator.geolocation.getCurrentPosition as Mock | ||
getLocationMock.mockImplementationOnce( | ||
(_success: any, error: (err: GeolocationPositionError) => void) => { | ||
error({ | ||
code: 1, | ||
message: 'User denied geolocation', | ||
} as GeolocationPositionError) | ||
}, | ||
) | ||
|
||
const { user } = render(<LocationIconButton />, { | ||
withStateProvider: true, | ||
}) | ||
|
||
const btn = await screen.findByTitle('Sort results by my location') | ||
|
||
await user.click(btn) | ||
// permission will be denied, and button hidden | ||
expect( | ||
screen.queryByTitle('Sort results by my location'), | ||
).not.toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { IconButton } from '@mui/material' | ||
import clsx from 'clsx' | ||
|
||
import { sortFilteredResultsByPosition } from '@/features/omrr/omrr-slice' | ||
import { setUserLocation, useUserLocation } from '@/features/omrr/omrr-slice' | ||
import { getMyLocation } from '@/utils/utils' | ||
import { useGeolocationPermission } from '@/hooks/useMyLocation' | ||
|
||
import GpsIcon from '@/assets/svgs/fa-gps.svg?react' | ||
|
||
export function LocationIconButton() { | ||
const dispatch = useDispatch() | ||
const state = useGeolocationPermission() | ||
const [visible, setVisible] = useState<boolean>(false) | ||
const userLocation = useUserLocation() | ||
|
||
// Only show this button if the user hasn't denied the permission | ||
useEffect(() => { | ||
setVisible(state === 'prompt' || state === 'granted') | ||
}, [state]) | ||
|
||
if (!visible) { | ||
return null | ||
} | ||
|
||
const onClick = () => { | ||
// Load user's location, then sort the filtered results by distance (closest first) | ||
getMyLocation(({ position }) => { | ||
if (position) { | ||
dispatch(sortFilteredResultsByPosition(position)) | ||
} | ||
}) | ||
if (userLocation) { | ||
dispatch(setUserLocation(undefined)) | ||
} else { | ||
// Load user's location, then sort the filtered results by distance (closest first) | ||
getMyLocation( | ||
({ position }) => { | ||
dispatch(setUserLocation(position)) | ||
}, | ||
(err) => { | ||
// User denied the permission | ||
console.log('Geolocation error', err.message) | ||
setVisible(false) | ||
}, | ||
) | ||
} | ||
} | ||
|
||
return ( | ||
<IconButton onClick={onClick} title="Search by my location"> | ||
<GpsIcon className="search-input-icon search-input-icon--location" /> | ||
<IconButton onClick={onClick} title="Sort results by my location"> | ||
<GpsIcon | ||
className={clsx( | ||
'search-input-icon search-input-icon--location', | ||
Boolean(userLocation) && 'search-input-icon--active', | ||
)} | ||
title="My location icon" | ||
/> | ||
</IconButton> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tend/src/pages/guidance/Guidance.test.tsx → .../src/pages/guidance/GuidancePage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.