Skip to content

Commit

Permalink
refactor(unitTests): use sinon for mocking
Browse files Browse the repository at this point in the history
test dataSourceProvider fake fetcher
  • Loading branch information
ftoromanoff committed Dec 20, 2023
1 parent 308770c commit 2c9936f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
4 changes: 2 additions & 2 deletions test/data/unitTest/vectortiles/style.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"sources": {
"geojson": {
"url": "file://test/data/unitTest/vectortiles/geojson.json"
"url": "https://test/geojson.json"
}
},
"sprite": "file://test/data/unitTest/vectortiles/sprite",
"sprite": "https://test/sprite",
"layers": [
{
"type": "background",
Expand Down
29 changes: 16 additions & 13 deletions test/unit/dataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TileMesh from 'Core/TileMesh';
import Extent, { globalExtentTMS } from 'Core/Geographic/Extent';
import OBB from 'Renderer/OBB';
import DataSourceProvider from 'Provider/DataSourceProvider';
import { supportedFetchers, supportedParsers } from 'Source/Source';
import { supportedFetchers } from 'Source/Source';
import TileProvider from 'Provider/TileProvider';
import WMTSSource from 'Source/WMTSSource';
import WMSSource from 'Source/WMSSource';
Expand All @@ -20,17 +20,18 @@ import Style from 'Core/Style';
import Feature2Mesh from 'Converter/Feature2Mesh';
import LayeredMaterial from 'Renderer/LayeredMaterial';
import { EMPTY_TEXTURE_ZOOM } from 'Renderer/RasterTile';
import sinon from 'sinon';

import holes from '../data/geojson/holesPoints.geojson.json';

const stubSupportedFetchers = new Map([
['application/json', () => Promise.resolve(JSON.parse(holes))],
['image/png', () => Promise.resolve(new THREE.Texture())],
]);

describe('Provide in Sources', function () {
// /!\ Avoid to overload fetcher because could troubleshoot the other unit tests?
// formatTag to avoid it
const formatTag = 'dspUnitTest';
supportedFetchers.set(`${formatTag}image/png`, () => Promise.resolve(new THREE.Texture()));
supportedFetchers.set(`${formatTag}application/json`, () => Promise.resolve(JSON.parse(holes)));
supportedParsers.set(`${formatTag}image/png`, supportedParsers.get('image/png'));
supportedParsers.set(`${formatTag}application/json`, supportedParsers.get('application/json'));
const stub = sinon.stub(supportedFetchers, 'get')
.callsFake(format => stubSupportedFetchers.get(format));

// Misc var to initialize a TileMesh instance
const geom = new THREE.BufferGeometry();
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('Provide in Sources', function () {
featureLayer.source = new WFSSource({
url: 'http://',
typeName: 'name',
format: `${formatTag}application/json`,
format: 'application/json',
extent: globalExtent,
crs: 'EPSG:3857',
});
Expand All @@ -103,6 +104,8 @@ describe('Provide in Sources', function () {
context.elevationLayers = [elevationlayer];
context.colorLayers = [colorlayer];

stub.restore();

beforeEach('reset state', function () {
// clear commands array
context.scheduler.commands = [];
Expand All @@ -112,7 +115,7 @@ describe('Provide in Sources', function () {
colorlayer.source = new WMTSSource({
url: 'http://',
name: 'name',
format: `${formatTag}image/png`,
format: 'image/png',
tileMatrixSet: 'PM',
crs: 'EPSG:3857',
extent: globalExtent,
Expand Down Expand Up @@ -144,7 +147,7 @@ describe('Provide in Sources', function () {
elevationlayer.source = new WMTSSource({
url: 'http://',
name: 'name',
format: `${formatTag}image/png`,
format: 'image/png',
tileMatrixSet: 'PM',
crs: 'EPSG:3857',
zoom: {
Expand Down Expand Up @@ -173,7 +176,7 @@ describe('Provide in Sources', function () {
colorlayer.source = new WMSSource({
url: 'http://',
name: 'name',
format: `${formatTag}image/png`,
format: 'image/png',
extent: globalExtent,
crs: 'EPSG:3857',
zoom: {
Expand Down Expand Up @@ -306,7 +309,7 @@ describe('Provide in Sources', function () {
colorlayer.source = new WMTSSource({
url: 'http://',
name: 'name',
format: `${formatTag}image/png`,
format: 'image/png',
tileMatrixSet: 'PM',
crs: 'EPSG:3857',
extent: globalExtent,
Expand Down
60 changes: 42 additions & 18 deletions test/unit/vectortiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,48 @@ import VectorTileParser from 'Parser/VectorTileParser';
import VectorTilesSource from 'Source/VectorTilesSource';
import Extent from 'Core/Geographic/Extent';
import urlParser from 'Parser/MapBoxUrlParser';
import Fetcher from 'Provider/Fetcher';
import sinon from 'sinon';

import style from '../data/unitTest/vectortiles/style.json';
import geojson from '../data/unitTest/vectortiles/geojson.json';
import sprite from '../data/unitTest/vectortiles/sprite.json';

const resources = {
'test/data/unitTest/vectortiles/style.json': style,
'https://test/geojson.json': geojson,
'https://test/sprite.json': sprite,
};

function parse(pbf, layers) {
return VectorTileParser.parse(pbf, {
in: {
layers,
styles: [[]],
},
out: {
crs: 'EPSG:3857',
},
});
}

describe('Vector tiles', function () {
// this PBF file comes from https://github.com/mapbox/vector-tile-js
// it contains two square polygons
const multipolygon = fs.readFileSync('test/data/pbf/multipolygon.pbf');
multipolygon.extent = new Extent('TMS', 1, 1, 1);

function parse(pbf, layers) {
return VectorTileParser.parse(pbf, {
in: {
layers,
styles: [[]],
},
out: {
crs: 'EPSG:3857',
},
});
}
let stub;
let multipolygon;

before(function () {
// this PBF file comes from https://github.com/mapbox/vector-tile-js
// it contains two square polygons
multipolygon = fs.readFileSync('test/data/pbf/multipolygon.pbf');
multipolygon.extent = new Extent('TMS', 1, 1, 1);

stub = sinon.stub(Fetcher, 'json')
.callsFake(url => Promise.resolve(JSON.parse(resources[url])));
});

after(function () {
stub.restore();
});

it('returns two squares', (done) => {
parse(multipolygon, {
Expand Down Expand Up @@ -125,10 +149,10 @@ describe('Vector tiles', function () {
});

it('loads the style from a file', function _it(done) {
const style = 'test/data/unitTest/vectortiles/style.json';
// const style = 'test/data/unitTest/vectortiles/style.json';
const source = new VectorTilesSource({
// style: 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/vectortiles/style.json',
style,
style: 'test/data/unitTest/vectortiles/style.json',
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
});
source.whenReady
Expand Down

0 comments on commit 2c9936f

Please sign in to comment.