Skip to content

Commit

Permalink
onClick > handleClick
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Nov 16, 2022
1 parent bd5ce80 commit 146d4f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/components/Geolocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ export interface GeolocationProps {
label?: string,
/** Custom icon component to display along with the button. */
GeolocationIcon?: React.FunctionComponent,
/**
* A function which is called when the geolocation button is clicked.
* This is called prior to executing the existing click behavior.
*/
onClick?: () => void,
/** A function which is called when the geolocation button is clicked. */
handleClick?: (position: GeolocationPosition) => void,
/** CSS classes for customizing the component styling. */
customCssClasses?: GeolocationCssClasses
}
Expand All @@ -70,7 +67,7 @@ export function Geolocation({
label = 'Use my location',
//TODO: replace default icon with SVG create from design team
GeolocationIcon = YextIcon,
onClick,
handleClick,
customCssClasses,
}: GeolocationProps): JSX.Element | null {
const searchActions = useSearchActions();
Expand All @@ -79,10 +76,13 @@ export function Geolocation({
const cssClasses = useComposedCssClasses(builtInCssClasses, customCssClasses);

const handleGeolocationClick = useCallback(async () => {
onClick?.();
setIsFetchingLocation(true);
try {
const position = await getUserLocation(geolocationOptions);
if (handleClick) {
handleClick(position);
return;
}
const { latitude, longitude, accuracy } = position.coords;
const locationFilter: SelectableStaticFilter = {
displayName: 'Current Location',
Expand All @@ -109,7 +109,7 @@ export function Geolocation({
} finally {
setIsFetchingLocation(false);
}
}, [geolocationOptions, onClick, radius, searchActions, staticFilters]);
}, [geolocationOptions, handleClick, radius, searchActions, staticFilters]);

return (
<div className={cssClasses.geolocationContainer}>
Expand Down
8 changes: 7 additions & 1 deletion tests/components/Geolocation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ const meta: ComponentMeta<typeof Geolocation> = {
argTypes: {
geolocationOptions: {
control: false
}
},
GeolocationIcon: {
control: false
},
handleClick: {
control: false
},
}
};
export default meta;
Expand Down
12 changes: 8 additions & 4 deletions tests/components/Geolocation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ it('renders custom icon when provided', () => {
expect(LocationIcon).toBeDefined();
});

it('executes onClick when provided', () => {
const mockedOnClickFn = jest.fn();
render(<Geolocation onClick={mockedOnClickFn} />);
it('executes handleClick when provided', async () => {
const mockedHandleClickFn = jest.fn();
const actions = spyOnActions();
render(<Geolocation handleClick={mockedHandleClickFn} />);
clickUpdateLocation();
expect(mockedOnClickFn).toBeCalledTimes(1);
await waitFor(() => {
expect(mockedHandleClickFn).toBeCalledTimes(1);
});
expect(actions.executeVerticalQuery).not.toBeCalled();
});

it('sets a location filter with user\'s coordinates in static filters state when clicked', async () => {
Expand Down

0 comments on commit 146d4f0

Please sign in to comment.