-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
198 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Oops, something went wrong.