Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

world spec #253

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions app/scripts/components/world-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, {Component, PropTypes} from 'react'
import d3 from 'd3'
import topojson from 'topojson'
import {AutoSizer} from 'react-virtualized'
import ReactFauxDOM from 'react-faux-dom'

import worldData from '../../data/world.json'

export default class WorldMap extends Component {
static propTypes = {
coordinates: PropTypes.array.isRequired
};

_renderMap = ({width, height}) => {
const projection = d3.geo.equirectangular()
.scale(height / Math.PI)
.translate([width / 2, height / 2])
.precision(0.1)

const path = d3.geo.path()
.projection(projection)

const graticule = d3.geo.graticule()

const el = d3.select(ReactFauxDOM.createElement('svg'))
.attr('width', width)
.attr('height', height)

// Fill Pattern
el.append('defs')
.append('pattern')
.attr('id', 'gridpattern')
.attr('x', 0)
.attr('y', 0)
.attr('width', 4)
.attr('height', 4)
.attr('patternUnits', 'userSpaceOnUse')
.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 1)
.attr('style', 'stroke: none; fill: rgba(255, 255, 255, 0.7)')

el.append('path')
.datum(graticule)
.attr('class', 'graticule')
.attr('d', path)

el.insert('path', '.graticule')
.datum(topojson.feature(worldData, worldData.objects.land))
.attr('d', path)
.attr('fill', 'url(#gridpattern)')

el.insert('path', '.graticule')
.datum(topojson.mesh(
worldData,
worldData.objects.countries,
(a, b) => a !== b
))
.attr('d', path)
.attr('fill', 'none')
.attr('stroke', 'none')
.attr('stroke-width', '0.5px')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: this.props.coordinates
})
.attr('d', path.pointRadius((d) => 8))
.attr('class', 'world-locations-base')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: this.props.coordinates
})
.attr('d', path.pointRadius((d) => 2))
.attr('class', 'world-locations-center')

return el.node().toReact()
};

render () {
return <AutoSizer>{this._renderMap}</AutoSizer>
}
}
86 changes: 3 additions & 83 deletions app/scripts/components/world.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React, {Component, PropTypes} from 'react'
import d3 from 'd3'
import ReactFauxDOM from 'react-faux-dom'
import topojson from 'topojson'
import {AutoSizer} from 'react-virtualized'
import {map, isEqual} from 'lodash'

import worldData from '../../data/world.json'
import WorldMap from './world-map'

export default class World extends Component {
static propTypes = {
Expand All @@ -18,93 +13,18 @@ export default class World extends Component {
locations: {}
};

_renderMap = (locations) => {
return ({width, height}) => {
const projection = d3.geo.equirectangular()
.scale(height / Math.PI)
.translate([width / 2, height / 2])
.precision(0.1)

const path = d3.geo.path()
.projection(projection)

const graticule = d3.geo.graticule()

const el = d3.select(ReactFauxDOM.createElement('svg'))
.attr('width', width)
.attr('height', height)

// Fill Pattern
el.append('defs')
.append('pattern')
.attr('id', 'gridpattern')
.attr('x', 0)
.attr('y', 0)
.attr('width', 4)
.attr('height', 4)
.attr('patternUnits', 'userSpaceOnUse')
.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 1)
.attr('style', 'stroke: none; fill: rgba(255, 255, 255, 0.7)')

el.append('path')
.datum(graticule)
.attr('class', 'graticule')
.attr('d', path)

el.insert('path', '.graticule')
.datum(topojson.feature(worldData, worldData.objects.land))
.attr('d', path)
.attr('fill', 'url(#gridpattern)')

el.insert('path', '.graticule')
.datum(topojson.mesh(
worldData,
worldData.objects.countries,
(a, b) => a !== b
))
.attr('d', path)
.attr('fill', 'none')
.attr('stroke', 'none')
.attr('stroke-width', '0.5px')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: locations
})
.attr('d', path.pointRadius((d) => 8))
.attr('class', 'world-locations-base')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: locations
})
.attr('d', path.pointRadius((d) => 2))
.attr('class', 'world-locations-center')

return el.node().toReact()
}
};

shouldComponentUpdate (nextProps) {
return !isEqual(nextProps, this.props)
}

render () {
// Draw peer locations
const locations = map(this.props.locations, ({lon, lat}) => {
const coordinates = map(this.props.locations, ({lon, lat}) => {
return [lon, lat]
})

return (
<div className='world'>
<AutoSizer>
{this._renderMap(locations)}
</AutoSizer>
<WorldMap coordinates={coordinates}/>
<div className='world-peers-counter'>
<div className='counter'>{this.props.peersCount}</div>
<div className='label'>Peers</div>
Expand Down
24 changes: 24 additions & 0 deletions test/components/world-map.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {expect} from 'chai'
import {shallow, render} from 'enzyme'
import React from 'react'

import WorldMap from '../../app/scripts/components/world-map'

describe('WorldMap', () => {
const c = [[12, 14]]

it('should render an svg', () => {
const el = render(<WorldMap coordinates={c}/>)
expect(el.find('svg').length).to.equal(1)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to think of more meaningful assertions to add here but drew a blank. The resulting output of _renderMap is basically all generated by d3 and I couldn't find anything else on the rendered output that could be used to assert correctness.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could assert that the point is drawn that you passed in

})

it('should have the correct coordiates', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo coordinates

const el = shallow(<WorldMap coordinates={c}/>)
const p = el.instance().props

c.forEach((coordinates, i) => {
expect(coordinates[0]).to.equal(p.coordinates[i][0])
expect(coordinates[1]).to.equal(p.coordinates[i][1])
})
})
})
22 changes: 22 additions & 0 deletions test/components/world.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect} from 'chai'
import {shallow} from 'enzyme'
import React from 'react'

import World from '../../app/scripts/components/world'

describe('World', () => {
it('should have the correct defaults', () => {
const el = shallow(<World/>)
expect(el.find('WorldMap').length).to.equal(1)

var inst = el.instance()
expect(inst.props.peersCount).to.equal(0)
expect(Object.keys(inst.props.locations).length)
.to.equal(0)
})

it('should set peersCount', () => {
const el = shallow(<World peersCount={1337}/>)
expect(el.find('.counter').node.props.children).to.equal(1337)
})
})