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

Add Geolocation API Jest mock #13442

Closed
wants to merge 1 commit into from
Closed

Add Geolocation API Jest mock #13442

wants to merge 1 commit into from

Conversation

willdurand
Copy link
Contributor

Hello!

Motivation (required)

The Geolocation API is currently not mocked in Jest, which often leads to an Invariant Violation: Native module cannot be null error. This patch adds a basic Jest configuration similar to the existing ones for the other modules.

Test Plan (required)

None unfortunately, but this patch makes my test suite all green 😉

Thanks,
William

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. GH Review: review-needed labels Apr 11, 2017
jest/setup.js Outdated
Geolocation: {
getCurrentPosition: jest.fn(),
watchPosition: jest.fn(),
clearWatch: jest.fn(),
Copy link
Member

Choose a reason for hiding this comment

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

Instead of mocking watchPosition and clearWatch, can you just mock startObserving()? We should only need to mock the native APIs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One question though, I looked at this definition to decide on the methods to mock, but you say I should only mock the native APIs. Should I completely omit the Geolocation JS class, and only mock the native methods? (then, I would have to mock getCurrentPosition() and stopObserving() on the LocationObserver too.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, you should only add a mock for the NativeModule, so you probably want to add startObserving there, and remove the LocationObserver mock

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@javache Isn't LocationObserver the native module?

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes, my bad. I guess the mocks you have on Geolocation should move to LocationObserver then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@javache alright, updated to only mock the LocationObserver :)

The [Geolocation
API](https://facebook.github.io/react-native/docs/geolocation.html) is
currently not mocked in Jest, which often leads to an `Invariant
Violation: Native module cannot be null` error.
@facebook-github-bot facebook-github-bot added GH Review: accepted Import Started This pull request has been imported. This does not imply the PR has been approved. and removed GH Review: review-needed labels Apr 13, 2017
@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Maxwell2022 pushed a commit to Maxwell2022/react-native that referenced this pull request Apr 19, 2017
Summary:
Hello!

The [Geolocation API](https://facebook.github.io/react-native/docs/geolocation.html) is currently not mocked in Jest, which often leads to an `Invariant Violation: Native module cannot be null` error. This patch adds a basic Jest configuration similar to the existing ones for the other modules.

None unfortunately, but this patch makes my test suite all green 😉

Thanks,
William
Closes facebook#13442

Differential Revision: D4883830

Pulled By: javache

fbshipit-source-id: c2a976834cca7537bd832a698e8cd25cf877705b
thotegowda pushed a commit to thotegowda/react-native that referenced this pull request May 7, 2017
Summary:
Hello!

The [Geolocation API](https://facebook.github.io/react-native/docs/geolocation.html) is currently not mocked in Jest, which often leads to an `Invariant Violation: Native module cannot be null` error. This patch adds a basic Jest configuration similar to the existing ones for the other modules.

None unfortunately, but this patch makes my test suite all green 😉

Thanks,
William
Closes facebook#13442

Differential Revision: D4883830

Pulled By: javache

fbshipit-source-id: c2a976834cca7537bd832a698e8cd25cf877705b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants