all files / tests/ geolocated_test.js

100% Statements 69/69
100% Branches 39/39
100% Functions 17/17
100% Lines 22/22
4 statements, 14 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                  
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import {geolocated, geoPropTypes} from '../src/index';
 
class SimpleComponent extends Component {
  render() {
    const {coords} = this.props;
    return (<div>
      {coords && coords.latitude}, {coords && coords.longitude}
    </div>);
  }
}
 
SimpleComponent.propTypes = {...SimpleComponent.propTypes, ...geoPropTypes };
 
describe('Geolocated', () => {
  it('should export a function', () => {
    expect(geolocated).to.exist;
    expect(geolocated).to.be.instanceof(Function);
  });
 
  it('should inject the location', () => {
    const mockGeolocationProvider = {
      getCurrentPosition(onSuccess) {
        return onSuccess({
          coords: {
            latitude: 50,
            longitude: 20,
          },
        })
      },
    };
 
    const Wrapped = geolocated({
      geolocationProvider: mockGeolocationProvider,
    })(SimpleComponent);
 
    const rendered = TestUtils.renderIntoDocument(<Wrapped />);
    const renderedNode = ReactDOM.findDOMNode(rendered);
 
    expect(renderedNode.textContent).to.equal('50, 20');
  });
 
  it('should throw on invalid geolocation provider', () => {
    const Wrapped = geolocated({
      geolocationProvider: {},
    })(SimpleComponent);
 
    expect(() => TestUtils.renderIntoDocument(<Wrapped />)).to.throw();
  });
});