Skip to content

Commit

Permalink
Test that GeoJSONWorkerSource properly rebuilds vector data
Browse files Browse the repository at this point in the history
  • Loading branch information
ezheidtmann committed Jun 9, 2017
1 parent 8a9051b commit c811f9c
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/unit/source/geojson_worker_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = require('mapbox-gl-js-test').test;
const GeoJSONWorkerSource = require('../../../src/source/geojson_worker_source');
const StyleLayerIndex = require('../../../src/style/style_layer_index');
const TileCoord = require('../../../src/source/tile_coord');

test('removeSource', (t) => {
t.test('removes the source from _geoJSONIndexes', (t) => {
Expand Down Expand Up @@ -48,3 +49,82 @@ test('removeSource', (t) => {

t.end();
});

test('reloadTile', (t) => {
t.test('does not rebuild vector data unless data has changed', (t) => {
const layers = [
{
id: 'mylayer',
source: 'sourceId',
type: 'symbol',
}
];
const layerIndex = new StyleLayerIndex(layers);
const source = new GeoJSONWorkerSource(null, layerIndex);
const originalLoadVectorData = source.loadVectorData;
let loadVectorCallCount = 0;
source.loadVectorData = function(params, callback) {
loadVectorCallCount++;
return originalLoadVectorData.call(this, params, callback);
};
const geoJson = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
};

function addData(callback) {
source.loadData({ source: 'sourceId', data: JSON.stringify(geoJson) }, (err) => {
t.equal(err, null);
callback();
});
}

function reloadTile(callback) {
let params = {
source: 'sourceId',
uid: 0,
coord: new TileCoord(0, 0, 0),
maxZoom: 10
};
source.reloadTile(params, (err, data) => {
t.equal(err, null);
return callback(data);
})
}

addData(() => {
// first call should load vector data from geojson
let firstData;
reloadTile(data => {
firstData = data;
});
t.equal(loadVectorCallCount, 1);

// second call won't give us new rawTileData
reloadTile(data => {
t.notOk('rawTileData' in data);
data.rawTileData = firstData.rawTileData;
t.deepEqual(data, firstData);
})

// also shouldn't call loadVectorData again
t.equal(loadVectorCallCount, 1);

// replace geojson data
addData(() => {
// should call loadVectorData again after changing geojson data
reloadTile(data => {
t.ok('rawTileData' in data);
t.deepEqual(data, firstData)
});
t.equal(loadVectorCallCount, 2);
t.end();
})
});
});

t.end();
})

0 comments on commit c811f9c

Please sign in to comment.