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

ReferenceError: navigator is not defined #11

Closed
chmac opened this issue Nov 18, 2017 · 3 comments
Closed

ReferenceError: navigator is not defined #11

chmac opened this issue Nov 18, 2017 · 3 comments

Comments

@chmac
Copy link

chmac commented Nov 18, 2017

I've added this package to my dev dependencies, I've imported it and run mock.initAll() in my test setup file, but I still see the following error:

ReferenceError: navigator is not defined

I'm accessing geolocation like navigator.geolocation.requestAuthorization().

I had a look at the mock-geolocatin implementation and it looks like it specifically targets the navigator global. Maybe the solution is to do something similar here?

@Xabadu
Copy link
Owner

Xabadu commented Nov 18, 2017

Hey @chmac, thanks for opening this issue. The whole navigator mock definitely needs some extra work to make it more complete.

Quick question: What version of RN are you using? I remember that when I added the Geolocation mock I was using navigator.geolocation.getCurrentPosition in my app, and the navigator global was injected by default, so I'm wondering if maybe something changed between versions.

Thanks for adding the link as well. I'm gonna dig into it to fix this. If you come up with a solution in the meantime, feel free to open a PR 👍

@chmac
Copy link
Author

chmac commented Nov 18, 2017

I spent a little time on it, but I couldn't get it working. I added navigator to the globals property of the jest settings in package.json so then the navigator is created, but as I read the code in this package, it's mocking an import called Geolocation. I'm using the latest stable react native (0.50.3). I wondered if the API used to be import foo from Geolocation and so the mock would work as jest.mock('Geolocation', ...) but to be honest, this is my first experiment with jest, so I really don't know much about it.

I tried naively changing it to jest.mock('navigator.geolocation, ...)but that didn't seem to work. I'm not sure ifjest.mock()is setup for that, I have a feeling thatnavigator.geolocationneeds to be replaced with a mock, but I could be wrong. I also look a bit at [mock-geolocation](https://github.com/2gis/mock-geolocation). It only works in the browser, but it provides a nice API to mock different scenarios. It's hard coded aroundwindow.navigator`, but perhaps the author is open to supporting React Native, and maybe there's a way to integrate it into your package somehow.

EDIT: This thread could be useful.

@Xabadu
Copy link
Owner

Xabadu commented May 25, 2018

Hey @chmac! I know it's been a while, but I finally fixed this bug. A new version has been published (1.3.0) with the fix in case you still need to use it.

Sorry for the delay, and thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants