Skip to content

Commit

Permalink
Update Jest + fix ScaleControl name
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Sep 8, 2016
1 parent a1888c3 commit 1274184
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 255 deletions.
33 changes: 13 additions & 20 deletions __tests__/LayersControl.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
/* global describe, expect, it, jest */
/* global describe, expect, it */

import Leaflet from 'leaflet';
import React, { Component } from 'react';
import { renderIntoDocument } from 'react-addons-test-utils';
import Leaflet from 'leaflet'
import React, { Component } from 'react'
import { renderIntoDocument } from 'react-addons-test-utils'

import LayersControl from '../src/LayersControl';
import Map from '../src/Map';

jest.unmock('../src/LayersControl');
jest.unmock('../src/Map');
jest.unmock('../src/MapComponent');
jest.unmock('../src/types/bounds');
jest.unmock('../src/types/index');
jest.unmock('../src/types/latlng');
import LayersControl from '../src/LayersControl'
import Map from '../src/Map'

describe('LayersControl', () => {
it('passes its `map` context to its children', () => {
Expand All @@ -21,12 +14,12 @@ describe('LayersControl', () => {
map: React.PropTypes.instanceOf(Leaflet.Map),
};

componentWillMount() {
expect(this.context.map).toBeDefined();
componentWillMount () {
expect(this.context.map).toBeDefined()
}

render() {
return null;
render () {
return null
}
}

Expand All @@ -38,6 +31,6 @@ describe('LayersControl', () => {
</LayersControl.Overlay>
</LayersControl>
</Map>
);
});
});
)
})
})
124 changes: 59 additions & 65 deletions __tests__/Map.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,108 @@
/* global describe, expect, it, jest */
/* global describe, expect, it */

import React, { Component } from 'react';
import { renderIntoDocument } from 'react-addons-test-utils';
import { renderToStaticMarkup } from 'react-dom/server';
import React, { Component } from 'react'
import { renderIntoDocument } from 'react-addons-test-utils'
import { renderToStaticMarkup } from 'react-dom/server'

import Map from '../src/Map';

jest.unmock('../src/Map');
jest.unmock('../src/MapComponent');
jest.unmock('../src/types/bounds');
jest.unmock('../src/types/index');
jest.unmock('../src/types/latlng');
import Map from '../src/Map'

describe('Map', () => {
it('only renders the container div server-side', () => {
class TestComponent extends Component {
render() {
return <span>test</span>;
render () {
return <span>test</span>
}
}
const component = <Map><TestComponent /></Map>;
const html = renderToStaticMarkup(component);
const component = <Map><TestComponent /></Map>
const html = renderToStaticMarkup(component)

expect(html).toBe('<div id="map1"></div>');
});
expect(html).toBe('<div id="map1"></div>')
})

it('sets center and zoom props', () => {
const center = [1.2, 3.4];
const zoom = 10;
const center = [1.2, 3.4]
const zoom = 10

const map = renderIntoDocument(<Map center={center} zoom={zoom} />);
const mapLeaflet = map.leafletElement;
const map = renderIntoDocument(<Map center={center} zoom={zoom} />)
const mapLeaflet = map.leafletElement

expect(mapLeaflet.getCenter().lat).toBe(center[0]);
expect(mapLeaflet.getCenter().lng).toBe(center[1]);
expect(mapLeaflet.getZoom()).toBe(zoom);
});
expect(mapLeaflet.getCenter().lat).toBe(center[0])
expect(mapLeaflet.getCenter().lng).toBe(center[1])
expect(mapLeaflet.getZoom()).toBe(zoom)
})

it('sets bounds', () => {
const bounds = [[0, 0], [2, 2]];
const bounds = [[0, 0], [2, 2]]
const map = renderIntoDocument(<Map bounds={bounds} />)
const mapBounds = map.leafletElement.getBounds();
expect(mapBounds).toBe(bounds);
});
const mapBounds = map.leafletElement.getBounds()
expect(mapBounds).toBe(bounds)
})

it('updates center and zoom props', () => {
class TestComponent extends Component {
constructor() {
super();
constructor () {
super()
this.state = {
center: [1.2, 3.4],
zoom: 10,
};
}
}
getLeafletMap() {
return this.refs.map.leafletElement;
getLeafletMap () {
return this.refs.map.leafletElement
}
updatePosition() {
updatePosition () {
this.setState({
center: [2.3, 4.5],
zoom: 12,
});
})
}
render() {
return <Map center={this.state.center} ref='map' zoom={this.state.zoom} />;
render () {
return <Map center={this.state.center} ref='map' zoom={this.state.zoom} />
}
}

const component = renderIntoDocument(<TestComponent />);
const mapLeaflet = component.getLeafletMap();
const component = renderIntoDocument(<TestComponent />)
const mapLeaflet = component.getLeafletMap()

expect(mapLeaflet.getCenter().lat).toBe(1.2);
expect(mapLeaflet.getCenter().lng).toBe(3.4);
expect(mapLeaflet.getZoom()).toBe(10);
expect(mapLeaflet.getCenter().lat).toBe(1.2)
expect(mapLeaflet.getCenter().lng).toBe(3.4)
expect(mapLeaflet.getZoom()).toBe(10)

component.updatePosition();
expect(mapLeaflet.getCenter().lat).toBe(2.3);
expect(mapLeaflet.getCenter().lng).toBe(4.5);
expect(mapLeaflet.getZoom()).toBe(12);
});
component.updatePosition()
expect(mapLeaflet.getCenter().lat).toBe(2.3)
expect(mapLeaflet.getCenter().lng).toBe(4.5)
expect(mapLeaflet.getZoom()).toBe(12)
})

it('updates bounds props', () => {
const firstBounds = [[0, 0], [2, 2]];
const firstBounds = [[0, 0], [2, 2]]
const secondBounds = [[0, 0], [-2, -2]]

class TestComponent extends Component {
constructor() {
super();
constructor () {
super()
this.state = {
bounds: firstBounds,
};
}
}
getLeafletMap() {
return this.refs.map.leafletElement;
getLeafletMap () {
return this.refs.map.leafletElement
}
updatePosition() {
updatePosition () {
this.setState({
bounds: secondBounds,
});
})
}
render() {
return <Map bounds={this.state.bounds} ref='map' />;
render () {
return <Map bounds={this.state.bounds} ref='map' />
}
}

const component = renderIntoDocument(<TestComponent />);
const mapLeaflet = component.getLeafletMap();
const component = renderIntoDocument(<TestComponent />)
const mapLeaflet = component.getLeafletMap()

expect(mapLeaflet.getBounds()).toBe(firstBounds);
component.updatePosition();
expect(mapLeaflet.getBounds()).toBe(secondBounds);
});
});
expect(mapLeaflet.getBounds()).toBe(firstBounds)
component.updatePosition()
expect(mapLeaflet.getBounds()).toBe(secondBounds)
})
})
112 changes: 55 additions & 57 deletions __tests__/MapComponent.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,100 @@
/* global describe, expect, it, jest */

import Leaflet from 'leaflet';
import React, { Component } from 'react';
import { renderIntoDocument } from 'react-addons-test-utils';
import Leaflet from 'leaflet'
import React, { Component } from 'react'
import { renderIntoDocument } from 'react-addons-test-utils'

import MapComponent from '../src/MapComponent';

jest.unmock('../src/MapComponent');
import MapComponent from '../src/MapComponent'

describe('MapComponent', () => {
class TestComponent extends MapComponent {
componentWillMount() {
super.componentWillMount();
this.leafletElement = Leaflet.map('test');
componentWillMount () {
super.componentWillMount()
this.leafletElement = Leaflet.map('test')
}
render() {
return null;
render () {
return null
}
}

it('exposes a `leafletElement` getter', () => {
const component = renderIntoDocument(<TestComponent />);
expect(component.leafletElement._container).toBeDefined();
});
const component = renderIntoDocument(<TestComponent />)
expect(component.leafletElement._container).toBeDefined()
})

it('binds the event', () => {
const callback = jest.genMockFn();
const component = renderIntoDocument(<TestComponent onClick={callback} />);
component.fireLeafletEvent('click');
expect(callback.mock.calls.length).toBe(1);
});
const callback = jest.fn()
const component = renderIntoDocument(<TestComponent onClick={callback} />)
component.fireLeafletEvent('click')
expect(callback.mock.calls.length).toBe(1)
})

it('unbinds the event', () => {
const callback = jest.genMockFn();
const callback = jest.fn()

class EventComponent extends Component {
constructor() {
super();
this.state = {bindEvent: true};
constructor () {
super()
this.state = {bindEvent: true}
}

dontBind() {
this.setState({bindEvent: false});
dontBind () {
this.setState({bindEvent: false})
}

fire() {
this.refs.c.fireLeafletEvent('click');
fire () {
this.refs.c.fireLeafletEvent('click')
}

render() {
render () {
return this.state.bindEvent
? <TestComponent onClick={callback} ref='c' />
: <TestComponent ref='c' />;
: <TestComponent ref='c' />
}
}

const component = renderIntoDocument(<EventComponent />);
const component = renderIntoDocument(<EventComponent />)

component.fire();
expect(callback.mock.calls.length).toBe(1);
component.fire()
expect(callback.mock.calls.length).toBe(1)

component.dontBind();
component.fire();
expect(callback.mock.calls.length).toBe(1);
});
component.dontBind()
component.fire()
expect(callback.mock.calls.length).toBe(1)
})

it('replaces the event', () => {
const callback1 = jest.genMockFn();
const callback2 = jest.genMockFn();
const callback1 = jest.fn()
const callback2 = jest.fn()

class EventComponent extends Component {
constructor() {
super();
this.state = {cb: callback1};
constructor () {
super()
this.state = {cb: callback1}
}

replaceCallback() {
this.setState({cb: callback2});
replaceCallback () {
this.setState({cb: callback2})
}

fire() {
this.refs.c.fireLeafletEvent('click');
fire () {
this.refs.c.fireLeafletEvent('click')
}

render() {
return <TestComponent onClick={this.state.cb} ref='c' />;
render () {
return <TestComponent onClick={this.state.cb} ref='c' />
}
}

const component = renderIntoDocument(<EventComponent />);
const component = renderIntoDocument(<EventComponent />)

component.fire();
expect(callback1.mock.calls.length).toBe(1);
expect(callback2.mock.calls.length).toBe(0);
component.fire()
expect(callback1.mock.calls.length).toBe(1)
expect(callback2.mock.calls.length).toBe(0)

component.replaceCallback();
component.fire();
expect(callback1.mock.calls.length).toBe(1);
expect(callback2.mock.calls.length).toBe(1);
});
});
component.replaceCallback()
component.fire()
expect(callback1.mock.calls.length).toBe(1)
expect(callback2.mock.calls.length).toBe(1)
})
})
Loading

0 comments on commit 1274184

Please sign in to comment.