Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should raise an error when call function with invalid arguments', () => {
// $ExpectError: first argument is required
watchPositionAsync();
// $ExpectError: first argument must be a n object
watchPositionAsync(69);
// $ExpectError: second argument must be a function
watchPositionAsync({}, 69);
watchPositionAsync(
{
// $ExpectError: invalid accuracy value
accuracy: 1,
},
() => {}
);
});
});
it('should passes when used properly', () => {
watchPositionAsync({}, data => {
const { coords, timestamp } = data;
(timestamp: number);
(coords.latitude: number);
(coords.speed: number);
// $ExpectError: check any
(coords.speed: string);
});
watchPositionAsync({}, async () => {}).then(result => {
result.remove();
});
});
it('should raise an error when call function with invalid arguments', () => {
// $ExpectError: first argument is required
watchPositionAsync();
// $ExpectError: first argument must be a n object
watchPositionAsync(69);
// $ExpectError: second argument must be a function
watchPositionAsync({}, 69);
watchPositionAsync(
{
// $ExpectError: invalid accuracy value
accuracy: 1,
},
() => {}
);
});
});
async function startTrace (dispatch) {
dispatch({
type: types.TRACE_START
})
let { status } = await Permissions.askAsync(Permissions.LOCATION)
if (status !== 'granted') {
// TODO: show error message
}
const watcher = await Location.watchPositionAsync({
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000, // TODO: get from config
distanceInterval: 5 // TODO: get from config
}, location => {
dispatch({
type: types.TRACE_POINT_CAPTURED,
location
})
})
dispatch({
type: types.TRACE_SET_SUBSCRIPTION,
watcher
})
}
updateStatus = async () => {
try {
const hasLocationPermission =
this.props.permissions[PERMISSIONS.ACCESS_FINE_LOCATION] ===
RESULTS.GRANTED;
if (!hasLocationPermission) return;
clearTimeout(this._timeoutId);
const provider = await Location.getProviderStatusAsync();
// log("Provider status", provider);
if (provider && provider.locationServicesEnabled && !this._watch) {
this._watch = await Location.watchPositionAsync(
positionOptions,
this.onPosition
);
} else {
if (this._watch) this._watch.remove();
this._watch = null;
this._timeoutId = setTimeout(this.updateStatus, LOCATION_TIMEOUT);
}
// If location services are disabled, clear the position stored in state,
// so that we don't create observations with a stale position.
if (!provider || !provider.locationServicesEnabled)
this.setState({ position: undefined });
this.setState({ provider });
} catch (err) {
this.handleError(err);
}
const startWatching = async () => {
try {
await requestPermissionsAsync();
subscriber = await watchPositionAsync(
{
accuracy: Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 10
},
callback
);
} catch (e) {
setErr(e);
}
};