Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Geolocation to remove all location fields #344

Merged
merged 1 commit into from
Dec 6, 2022
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
7 changes: 4 additions & 3 deletions src/hooks/useGeolocationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { executeSearch } from '../utils/search-operations';
import { getUserLocation } from '../utils/location-operations';
import { useCallback, useState } from 'react';

const LOCATION_FIELD_ID = 'builtin.location';
const GEOLOCATION_FIELD_ID = 'builtin.location';
const LOCATION_FIELD_IDS = [GEOLOCATION_FIELD_ID, 'builtin.region', 'address.countryCode'];
yen-tt marked this conversation as resolved.
Show resolved Hide resolved
const METERS_PER_MILE = 1609.344;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ export function useGeolocationHandler({
selected: true,
filter: {
kind: 'fieldValue',
fieldId: LOCATION_FIELD_ID,
fieldId: GEOLOCATION_FIELD_ID,
matcher: Matcher.Near,
value: {
lat: latitude,
Expand All @@ -60,7 +61,7 @@ export function useGeolocationHandler({
};
const nonLocationFilters = staticFilters.filter(filter => {
return !(filter.filter.kind === 'fieldValue'
&& filter.filter.fieldId === LOCATION_FIELD_ID);
&& LOCATION_FIELD_IDS.includes(filter.filter.fieldId));
});
searchActions.setStaticFilters([...nonLocationFilters, locationFilter]);
executeSearch(searchActions);
Expand Down
28 changes: 24 additions & 4 deletions tests/components/Geolocation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ const mockedStateWithFilters: Partial<State> = {
},
}
},
{
displayName: 'Virginia, US',
selected: true,
filter: {
kind: 'fieldValue',
fieldId: 'builtin.region',
matcher: Matcher.Equals,
value: 'US-VA',
}
},
{
displayName: 'United States',
selected: true,
filter: {
kind: 'fieldValue',
fieldId: 'address.countryCode',
matcher: Matcher.Equals,
value: 'US',
}
},
{
displayName: 'My name',
selected: true,
Expand Down Expand Up @@ -141,7 +161,7 @@ describe('custom click handler', () => {
describe('default click handler', () => {
it('sets a location filter using provided radius', async () => {
const actions = spyOnActions();
render(<Geolocation radius={10}/>);
render(<Geolocation radius={10} />);
clickUpdateLocation();

const expectedLocationFilter: SelectableStaticFilter = createLocationFilter(10 * 1609.344);
Expand All @@ -162,7 +182,7 @@ describe('default click handler', () => {
});
});

it('replace existing location filters with a new location filter in static filters state', async () => {
it('replaces existing location filters with a new location filter in static filters state', async () => {
mockAnswersState(mockedStateWithFilters);
const actions = spyOnActions();
render(<Geolocation />);
Expand All @@ -189,7 +209,7 @@ describe('default click handler', () => {
it('sets a location filter using a larger radius than provided value due to low accuracy of user coordinate', async () => {
jest.spyOn(locationOperations, 'getUserLocation').mockResolvedValue(newGeoPositionWithLowAccuracy);
const actions = spyOnActions();
render(<Geolocation radius={10}/>);
render(<Geolocation radius={10} />);
clickUpdateLocation();

const accuracy = newGeoPositionWithLowAccuracy.coords.accuracy;
Expand Down Expand Up @@ -243,4 +263,4 @@ function createLocationFilter(radius: number = 50 * 1609.344): SelectableStaticF
},
}
};
}
}