Skip to content

Commit

Permalink
Add skip option to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cedarbaum committed Mar 18, 2023
1 parent 8aff56c commit ca8d969
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
5 changes: 3 additions & 2 deletions demo/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import {usePosition} from '../src/usePosition';

export const Demo = ({watch, settings}) => {
export const Demo = ({watch, settings, skip}) => {
const {
latitude,
longitude,
Expand All @@ -11,7 +11,7 @@ export const Demo = ({watch, settings}) => {
speed,
heading,
error,
} = usePosition(watch, settings);
} = usePosition(watch, settings, skip);

const loader = !latitude && !error ? (
<>
Expand Down Expand Up @@ -39,4 +39,5 @@ export const Demo = ({watch, settings}) => {
Demo.propTypes = {
watch: PropTypes.bool,
settings: PropTypes.object,
skip: PropTypes.bool,
};
6 changes: 5 additions & 1 deletion src/usePosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const defaultSettings = {
maximumAge: 0,
};

export const usePosition = (watch = false, userSettings = {}) => {
export const usePosition = (watch = false, userSettings = {}, skip = false) => {
const settings = {
...defaultSettings,
...userSettings,
Expand All @@ -31,6 +31,10 @@ export const usePosition = (watch = false, userSettings = {}) => {
};

useEffect(() => {
if (skip) {
return;
}

if (!navigator || !navigator.geolocation) {
setError('Geolocation is not supported');
return;
Expand Down
24 changes: 24 additions & 0 deletions tests/__snapshots__/usePosition.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`usePosition should not call geolocation when skipped 1`] = `
Array [
<div>
Trying to fetch location...
</div>,
<br />,
<code>
latitude:
<br />
longitude:
<br />
timestamp:
<br />
accuracy:
<br />
speed:
<br />
heading:
<br />
error:
</code>,
]
`;

exports[`usePosition should return empty values by default 1`] = `
Array [
<div>
Expand Down
18 changes: 18 additions & 0 deletions tests/usePosition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,22 @@ describe('usePosition', () => {
const tree = testRenderer.toJSON();
expect(tree).toMatchSnapshot();
});

it('should not call geolocation when skipped', () => {
global.navigator.geolocation = {
watchPosition: jest.fn(),
getCurrentPosition: jest.fn(),
clearWatch: jest.fn(),
};

let testRenderer;
act(() => {
testRenderer = renderer.create(<Demo skip />);
});
const tree = testRenderer.toJSON();
expect(tree).toMatchSnapshot();

expect(global.navigator.geolocation.watchPosition).not.toHaveBeenCalled();
expect(global.navigator.geolocation.getCurrentPosition).not.toHaveBeenCalled();
});
});

0 comments on commit ca8d969

Please sign in to comment.