Skip to content

Commit ca8d969

Browse files
committed
Add skip option to hook
1 parent 8aff56c commit ca8d969

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

demo/Demo.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {usePosition} from '../src/usePosition';
44

5-
export const Demo = ({watch, settings}) => {
5+
export const Demo = ({watch, settings, skip}) => {
66
const {
77
latitude,
88
longitude,
@@ -11,7 +11,7 @@ export const Demo = ({watch, settings}) => {
1111
speed,
1212
heading,
1313
error,
14-
} = usePosition(watch, settings);
14+
} = usePosition(watch, settings, skip);
1515

1616
const loader = !latitude && !error ? (
1717
<>
@@ -39,4 +39,5 @@ export const Demo = ({watch, settings}) => {
3939
Demo.propTypes = {
4040
watch: PropTypes.bool,
4141
settings: PropTypes.object,
42+
skip: PropTypes.bool,
4243
};

src/usePosition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const defaultSettings = {
66
maximumAge: 0,
77
};
88

9-
export const usePosition = (watch = false, userSettings = {}) => {
9+
export const usePosition = (watch = false, userSettings = {}, skip = false) => {
1010
const settings = {
1111
...defaultSettings,
1212
...userSettings,
@@ -31,6 +31,10 @@ export const usePosition = (watch = false, userSettings = {}) => {
3131
};
3232

3333
useEffect(() => {
34+
if (skip) {
35+
return;
36+
}
37+
3438
if (!navigator || !navigator.geolocation) {
3539
setError('Geolocation is not supported');
3640
return;

tests/__snapshots__/usePosition.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`usePosition should not call geolocation when skipped 1`] = `
4+
Array [
5+
<div>
6+
Trying to fetch location...
7+
</div>,
8+
<br />,
9+
<code>
10+
latitude:
11+
<br />
12+
longitude:
13+
<br />
14+
timestamp:
15+
<br />
16+
accuracy:
17+
<br />
18+
speed:
19+
<br />
20+
heading:
21+
<br />
22+
error:
23+
</code>,
24+
]
25+
`;
26+
327
exports[`usePosition should return empty values by default 1`] = `
428
Array [
529
<div>

tests/usePosition.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ describe('usePosition', () => {
124124
const tree = testRenderer.toJSON();
125125
expect(tree).toMatchSnapshot();
126126
});
127+
128+
it('should not call geolocation when skipped', () => {
129+
global.navigator.geolocation = {
130+
watchPosition: jest.fn(),
131+
getCurrentPosition: jest.fn(),
132+
clearWatch: jest.fn(),
133+
};
134+
135+
let testRenderer;
136+
act(() => {
137+
testRenderer = renderer.create(<Demo skip />);
138+
});
139+
const tree = testRenderer.toJSON();
140+
expect(tree).toMatchSnapshot();
141+
142+
expect(global.navigator.geolocation.watchPosition).not.toHaveBeenCalled();
143+
expect(global.navigator.geolocation.getCurrentPosition).not.toHaveBeenCalled();
144+
});
127145
});

0 commit comments

Comments
 (0)