I am trying to write tests for a reactive component that I created that uses navigator.geolocation.getCurrentPosition()in a method like this (a rough example of my component):
class App extends Component {
constructor() {
...
}
method() {
navigator.geolocation.getCurrentPosition((position) => {
...code...
}
}
render() {
return(...)
}
}
I am using a create-responsive application that includes a test:
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
This test failed by printing this in the console:
TypeError: Cannot read property 'getCurrentPosition' of undefined
I am new to React, but have little experience with Angular 1.x. Angular typically models (as part of the tests in beforeEach) functions, services, and global object methods such as navigator.geolocation.etc. I spent time studying this problem, and this piece of code is the closest I could come up with:
global.navigator = {
geolocation: {
getCurrentPosition: jest.fn()
}
}
I put this in my test file for the application, but it had no effect.
"" ?
: , navigator.getCurrentPosition . , jest JSDOM , window. JSDOM navigator. . getCurrentPosition , App.