Skip to content

Commit d29e0b3

Browse files
committed
make the component testable and test it
1 parent ef5d746 commit d29e0b3

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/components/geolocated.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ const geolocated = (config) => (WrappedComponent) => {
1111
maximumAge: 0,
1212
timeout: Infinity,
1313
},
14+
geolocationProvider: (config && config.geolocationProvider) || (typeof (navigator) !== 'undefined' && navigator.geolocation),
1415
};
1516

1617
let result = class Geolocated extends Component {
1718
constructor(props) {
1819
super(props);
1920
this.state = {
2021
coords: null,
21-
isGeolocationAvailable: Boolean(navigator && navigator.geolocation),
22+
isGeolocationAvailable: Boolean(activeConfig.geolocationProvider),
2223
isGeolocationEnabled: true, // be optimistic
2324
positionError: null,
2425
};
@@ -46,7 +47,11 @@ const geolocated = (config) => (WrappedComponent) => {
4647
}
4748

4849
componentDidMount() {
49-
navigator.geolocation.getCurrentPosition(this.onPositionSuccess, this.onPositionError, activeConfig.positionOptions);
50+
const {geolocationProvider} = activeConfig;
51+
if (!geolocationProvider || !geolocationProvider.getCurrentPosition) {
52+
throw new Error('The provided geolocation provider is invalid');
53+
}
54+
geolocationProvider.getCurrentPosition(this.onPositionSuccess, this.onPositionError, activeConfig.positionOptions);
5055
}
5156

5257
render() {

tests/geolocated_test.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
1-
import {geolocated} from '../src/index';
1+
import React, {Component} from 'react';
2+
import ReactDOM from 'react-dom';
3+
import TestUtils from 'react-addons-test-utils';
4+
import {geolocated, geoPropTypes} from '../src/index';
25

3-
describe('Geolocated', function() {
4-
it('should export a function', function() {
6+
class SimpleComponent extends Component {
7+
render() {
8+
const {coords} = this.props;
9+
return (<div>
10+
{coords && coords.latitude}, {coords && coords.longitude}
11+
</div>);
12+
}
13+
}
14+
15+
SimpleComponent.propTypes = {...SimpleComponent.propTypes, ...geoPropTypes };
16+
17+
describe('Geolocated', () => {
18+
it('should export a function', () => {
519
expect(geolocated).to.exist;
620
expect(geolocated).to.be.instanceof(Function);
721
});
22+
23+
it('should inject the location', () => {
24+
const mockGeolocationProvider = {
25+
getCurrentPosition(onSuccess) {
26+
return onSuccess({
27+
coords: {
28+
latitude: 50,
29+
longitude: 20,
30+
},
31+
})
32+
},
33+
};
34+
35+
const Wrapped = geolocated({
36+
geolocationProvider: mockGeolocationProvider,
37+
})(SimpleComponent);
38+
39+
const rendered = TestUtils.renderIntoDocument(<Wrapped />);
40+
const renderedNode = ReactDOM.findDOMNode(rendered);
41+
42+
expect(renderedNode.textContent).to.equal('50, 20');
43+
});
44+
45+
it('should throw on invalid geolocation provider', () => {
46+
const Wrapped = geolocated({
47+
geolocationProvider: {},
48+
})(SimpleComponent);
49+
50+
expect(() => TestUtils.renderIntoDocument(<Wrapped />)).to.throw();
51+
});
852
});

0 commit comments

Comments
 (0)