-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Geolocation module (#23987)
Summary: This PR adds a number of unit tests for the Geolocation module, as a follow-up of #23903. I also added two missing documentation strings to that module, with references to the online documentation, for consistency with the other methods in the same module. Not applicable, since it only adds tests. Pull Request resolved: #23987 Differential Revision: D14502848 Pulled By: cpojer fbshipit-source-id: 8f7c1cee6be3fae081d9770e5e942fadda65e6c2
- Loading branch information
1 parent
cc3f9a7
commit 6047d42
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('Geolocation', () => { | ||
let Geolocation; | ||
const NativeModules = require('NativeModules'); | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
Geolocation = jest.requireActual('Geolocation'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should set the location observer configuration', () => { | ||
Geolocation.setRNConfiguration({skipPermissionRequests: true}); | ||
expect( | ||
NativeModules.LocationObserver.setConfiguration.mock.calls.length, | ||
).toEqual(1); | ||
}); | ||
|
||
it('should request authorization for location requests', () => { | ||
Geolocation.requestAuthorization(); | ||
expect( | ||
NativeModules.LocationObserver.requestAuthorization.mock.calls.length, | ||
).toEqual(1); | ||
}); | ||
|
||
it('should get the current position and pass it to the given callback', () => { | ||
const callback = () => {}; | ||
Geolocation.getCurrentPosition(callback); | ||
expect( | ||
NativeModules.LocationObserver.getCurrentPosition.mock.calls.length, | ||
).toEqual(1); | ||
expect( | ||
NativeModules.LocationObserver.getCurrentPosition.mock.calls[0][1], | ||
).toBe(callback); | ||
}); | ||
|
||
it('should add a success listener to the geolocation', () => { | ||
const watchID = Geolocation.watchPosition(() => {}); | ||
expect(watchID).toEqual(0); | ||
expect(NativeModules.LocationObserver.addListener.mock.calls[0][0]).toBe( | ||
'geolocationDidChange', | ||
); | ||
}); | ||
|
||
it('should add an error listener to the geolocation', () => { | ||
const watchID = Geolocation.watchPosition(() => {}, () => {}); | ||
expect(watchID).toEqual(0); | ||
expect(NativeModules.LocationObserver.addListener.mock.calls[1][0]).toBe( | ||
'geolocationError', | ||
); | ||
}); | ||
|
||
it('should clear the listeners associated with a watchID', () => { | ||
const watchID = Geolocation.watchPosition(() => {}, () => {}); | ||
Geolocation.clearWatch(watchID); | ||
expect(NativeModules.LocationObserver.stopObserving.mock.calls.length).toBe( | ||
1, | ||
); | ||
}); | ||
|
||
it('should correctly assess if all listeners have been cleared', () => { | ||
const watchID = Geolocation.watchPosition(() => {}, () => {}); | ||
Geolocation.watchPosition(() => {}, () => {}); | ||
Geolocation.clearWatch(watchID); | ||
expect(NativeModules.LocationObserver.stopObserving.mock.calls.length).toBe( | ||
0, | ||
); | ||
}); | ||
|
||
it('should not fail if the watchID one wants to clear does not exist', () => { | ||
Geolocation.watchPosition(() => {}, () => {}); | ||
Geolocation.clearWatch(42); | ||
expect(NativeModules.LocationObserver.stopObserving.mock.calls.length).toBe( | ||
0, | ||
); | ||
}); | ||
|
||
it('should stop observing and warn about removing existing subscriptions', () => { | ||
const warningCallback = jest.fn(); | ||
jest.mock('fbjs/lib/warning', () => warningCallback); | ||
Geolocation.watchPosition(() => {}, () => {}); | ||
Geolocation.stopObserving(); | ||
expect(NativeModules.LocationObserver.stopObserving.mock.calls.length).toBe( | ||
1, | ||
); | ||
expect(warningCallback.mock.calls.length).toBeGreaterThanOrEqual(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters