Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 915: Stop/start Location manager based on listeners registered. #999

Merged
merged 16 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix unit test cases for location manager
  • Loading branch information
soumyashisPR committed Aug 24, 2020
commit 5491ca38cd5875a36035bf131b8b659cfa4d7067
10 changes: 6 additions & 4 deletions __tests__/modules/location/locationManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('LocationManager', () => {

describe('#addListener', () => {
const myListener = jest.fn();
MapboxGL.LocationCallbackName = {Update: 'MapboxUserLocationUpdate'};

afterEach(() => {
locationManager._listeners = [];
Expand All @@ -88,17 +89,18 @@ describe('LocationManager', () => {
expect(myListener).not.toHaveBeenCalled();
});

test('calls listener with "lastKnownLocation"', () => {
test('listener is not called just after being added', () => {
locationManager._lastKnownLocation = location;

locationManager.addListener(myListener);
expect(locationManager._listeners).toStrictEqual([myListener]);
expect(myListener).toHaveBeenCalledWith(location);
expect(myListener).toHaveBeenCalledTimes(1);
expect(myListener).not.toHaveBeenCalled();
});
});

describe('#removeListener', () => {
MapboxGLLocationManager.stop = jest.fn();

ferdicus marked this conversation as resolved.
Show resolved Hide resolved
test('removes selected listener', () => {
// just two different functions
const listenerA = jest.fn(() => 'listenerA');
Expand Down Expand Up @@ -142,7 +144,7 @@ describe('LocationManager', () => {
jest.spyOn(MapboxGLLocationManager, 'start');
jest.spyOn(LocationModuleEventEmitter, 'addListener');

afterEach(() => {
beforeEach(() => {
locationManager._isListening = false;
});

Expand Down
2 changes: 2 additions & 0 deletions javascript/components/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class UserLocation extends React.Component {
this._isLocationManagerRequired = required;
if (required) {
locationManager.addListener(this._onLocationUpdate);
const location = await locationManager.getLastKnownLocation();
this._onLocationUpdate(location);
} else {
locationManager.removeListener(this._onLocationUpdate);
}
Expand Down
4 changes: 0 additions & 4 deletions javascript/modules/location/locationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class LocationManager {
}
if (!this._listeners.includes(listener)) {
this._listeners.push(listener);

if (this._lastKnownLocation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this removed?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change reverted.
Reason for removing this, I was trying to keep addListener() and the method call listener() independent, but didn't work out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfazekas Please let me know if any more changes are required with this PR.
Thanks.

listener(this._lastKnownLocation);
}
}
}

Expand Down