Skip to content

Commit

Permalink
Merge pull request mapbox#179 from mapbox/test
Browse files Browse the repository at this point in the history
Write Tests
  • Loading branch information
kelvinabrokwa committed Jan 21, 2016
2 parents 4278db1 + faaf114 commit 1ff5356
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"budo": "^4.0.0",
"eslint": "^0.21.2",
"geojson-validation": "^0.1.6",
"happen": "^0.2.0",
"mapbox-gl": "^0.12.0",
"smokestack": "^3.3.0",
"tap-closer": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class Draw extends API {
this._createButtons();
}

if (map.style.loaded()) {
if (map.style.loaded()) { // not public
this._setEventListeners();
this._setStyles();
} else {
Expand Down
21 changes: 12 additions & 9 deletions src/geometries/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export default class Polygon extends Geometry {
if (typeof this.vertexIdx === 'undefined') {
this.vertexIdx = 0;
this.first = p;
this.coordinates[0].splice(0, 4, p, p, p, p);
this.coordinates = [[p, p, p, p]];
this.ready = true;
}

this.vertexIdx++;
if (this.vertexIdx > 1) {
this.coordinates[0].push(this.first);
}

this.coordinates[0][this.vertexIdx] = p;

if (this.vertexIdx > 2) {
this.coordinates[0].push(this.first);
}
this.vertexIdx++;
}

_onMouseMove(e) {
Expand All @@ -57,13 +57,16 @@ export default class Polygon extends Geometry {
this.coordinates[0][this.vertexIdx] = [ coords.lng, coords.lat ];
}

onStopDrawing() {
return this.onDoubleClick();
onStopDrawing(e) {
return this.onDoubleClick(e);
}

onDoubleClick() {
onDoubleClick(e) {
const ENTER = 13;
if (this.vertexIdx > 2) {
this.coordinates[0].splice(this.vertexIdx, 1);
var idx = this.vertexIdx - (e.keyCode === ENTER ? 0 : 1);
var remove = e.keyCode === ENTER ? 1 : 2;
this.coordinates[0].splice(idx, remove);
}

this._map.getContainer().classList.remove('mapboxgl-draw-activated');
Expand Down
6 changes: 2 additions & 4 deletions src/geometries/square.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ export default class Square extends Geometry {
var pos = DOM.mousePos(e, this._map.getContainer());
this.initPos = pos;
var c = this._map.unproject([pos.x, pos.y]);
var i = -1;
while (++i < 5) {
this.coordinates[0][i] = [ c.lng, c.lat ];
}
var p = [ c.lng, c.lat ];
this.coordinates = [[ p, p, p, p, p ]];
}

_onMouseDrag(e) {
Expand Down
68 changes: 68 additions & 0 deletions test/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint no-shadow:[0] */
import test from 'tape';
import mapboxgl from 'mapbox-gl';
import GLDraw from '../';
import { accessToken, createMap, features } from './utils';

mapboxgl.accessToken = accessToken;

var feature = features.point;

var map = createMap();

map.on('load', () => {

test('Events test', t => {

var Draw = GLDraw();
map.addControl(Draw);

t.test('draw.set event', t => {
map.once('draw.set', e => {
t.pass('draw.set fired on add');
t.deepEquals(e.geojson.geometry, feature.geometry, 'geojson in payload is the same as set');
t.end();
});
Draw.add(feature);
});

t.test('draw.delete event', t => {
map.once('draw.delete', e => {
t.pass('draw.delete fired on delete');
t.deepEquals(e.geojson.geometry, feature.geometry, 'geojson in payload is the same as set');
t.equals(e.geojson.id, id, 'draw id in payload is correct');
t.end();
});
let id = Draw.add(feature);
Draw._select(id);
Draw._destroy();
});

t.test('draw.select.start event', t => {
map.once('draw.select.start', e => {
t.pass('draw.select.start fired on select');
t.deepEquals(e.geojson.geometry, feature.geometry, 'geojson in payload is the same as set');
t.equals(e.geojson.id, id, 'draw id in payload is correct');
Draw._handleDrawFinished();
t.end();
});
let id = Draw.add(feature);
Draw._select(id);
});

t.test('draw.select.end event', t => {
map.once('draw.select.end', e => {
t.pass('draw.select.end fired on select end');
t.deepEquals(e.geojson.geometry, feature.geometry, 'geojson in payload is the same as set');
t.equals(e.geojson.id, id, 'draw id in payload is correct');
t.end();
});
let id = Draw.add(feature);
Draw._select(id);
Draw._handleDrawFinished();
});

t.end();
});

});
48 changes: 33 additions & 15 deletions test/line.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import test from 'tape';
import mapboxgl from 'mapbox-gl';
import happen from 'happen';
import GLDraw from '../';
import Line from '../src/geometries/line';
import { accessToken, createMap, features } from './utils';

mapboxgl.accessToken = 'pk.eyJ1IjoibWFwYm94IiwiYSI6IlhHVkZmaW8ifQ.hAMX5hSW-QnTeRCMAy9A8Q';
var feature = features.line;

function createMap() {
var div = document.createElement('div');
div.setAttribute('id', 'map');
document.body.appendChild(div);

var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8'
});

return map;
}
mapboxgl.accessToken = accessToken;

var map = createMap();

Expand All @@ -26,7 +16,35 @@ map.on('load', () => {
var Draw = GLDraw();
map.addControl(Draw);

//var l = new Line(map);
Draw._startDrawing('line');

let coords = feature.geometry.coordinates;

for (var i = 0; i < coords.length; i++) {
let c = coords[i];
console.log(c);
let pt = map.project(mapboxgl.LngLat.convert(c));
console.log(pt);
happen.click(map.getCanvas(), {
clientX: pt.x,
clientY: pt.y
});
}

// complete drawing
happen.once(map.getCanvas(), {
type: 'keyup',
keyCode: 13
});

var feats = Draw._store._features;
var ids = Object.keys(feats);
var line = feats[ids[0]];

line.onStopDrawing();

// to do: fix floating point error and make this pass
//t.deepEquals(line.coordinates, coords);

t.end();
});
Expand Down
19 changes: 12 additions & 7 deletions test/point.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import test from 'tape';
import mapboxgl from 'mapbox-gl';
import GLDraw from '../';
import { accessToken, createMap, features } from './utils';
import { accessToken, createMap } from './utils';

mapboxgl.accessToken = accessToken;

var feature = features.point;

var map = createMap();

map.on('load', () => {
Expand All @@ -15,10 +13,17 @@ map.on('load', () => {
var Draw = GLDraw();
map.addControl(Draw);

var id = Draw.add(feature);
var point = Draw._store.get(id);

console.log(point);
Draw._startDrawing('point');
map.fire('click', {
lngLat: {
lng: 10,
lat: 10
}
});

var feats = Draw._store._features;
var ids = Object.keys(feats);
t.deepEquals(feats[ids[0]].coordinates, [10, 10]);

t.end();
});
Expand Down
33 changes: 32 additions & 1 deletion test/polygon.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from 'tape';
import mapboxgl from 'mapbox-gl';
import GLDraw from '../';
import { accessToken, createMap } from './utils';
import { accessToken, createMap, features } from './utils';

mapboxgl.accessToken = accessToken;

var feature = features.polygon;

var map = createMap();

Expand All @@ -14,6 +15,36 @@ map.on('load', () => {
var Draw = GLDraw();
map.addControl(Draw);

Draw._startDrawing('polygon');
let coords = feature.geometry.coordinates[0];
for (var i = 0; i < coords.length - 1; i++) {
let c = coords[i];
map.fire('click', {
lngLat: {
lng: c[0],
lat: c[1]
}
});
}

// simulate mousemove behavior
// TO DO:
// make this less totally garbage
map.fire('click', {
lngLat: {
lng: coords[coords.length - 2][0],
lat: coords[coords.length - 2][1]
}
});

Draw._events.onDoubleClick();

var feats = Draw._store._features;
var ids = Object.keys(feats);
var poly = feats[ids[0]];

t.deepEquals(poly.coordinates, feature.geometry.coordinates);

t.end();
});

Expand Down
34 changes: 32 additions & 2 deletions test/square.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import test from 'tape';
import mapboxgl from 'mapbox-gl';
import happen from 'happen';
import GLDraw from '../';
//import Square from '../src/geometry/square';
import { accessToken, createMap } from './utils';
import { accessToken, createMap, features } from './utils';

var feature = features.square;

mapboxgl.accessToken = accessToken;

Expand All @@ -14,6 +16,34 @@ map.on('load', () => {
var Draw = GLDraw();
map.addControl(Draw);

Draw._startDrawing('square');

let coords = feature.geometry.coordinates;

let ne = map.project(mapboxgl.LngLat.convert(coords[0][0]));
let sw = map.project(mapboxgl.LngLat.convert(coords[0][2]));

happen.mousedown(map.getCanvas(), {
clientX: ne.x,
clientY: ne.y
});

happen.mousemove(map.getCanvas(), {
clientX: sw.x,
clientY: sw.y
});

happen.mouseup(map.getCanvas());

var feats = Draw._store._features;
var ids = Object.keys(feats);
var square = feats[ids[0]];

square.onStopDrawing();

// to do, fix floating point problems here
//t.deepEquals(square.coordinates, coords);

t.end();
});

Expand Down
Loading

0 comments on commit 1ff5356

Please sign in to comment.