Skip to content

Commit

Permalink
Add tests for 'browser' preset
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Oct 28, 2020
1 parent 19d41d0 commit 46bec3c
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@turf/transform-rotate": "^5.1.5",
"@turf/transform-translate": "^5.1.5",
"@turf/union": "6.2.0-alpha.2",
"es-pack-js": "^0.3.6",
"es-pack-js": "^0.3.7",
"get-pixels": "^3.3.0",
"pbf": "^3.1.0",
"regenerator": "^0.14.2",
Expand Down
39 changes: 39 additions & 0 deletions tests/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>tests</title>
</head>
<body>
<script src="__three.min.js"></script>
<script src="__tmp.min.js"></script>
<script src="units.js"></script>
<script type="module">
const output = {};

{ // test `new`
const tgeo = new ThreeGeo();
output['new'] = [
typeof ThreeGeo, // 'function',
tgeo.constUnitsSide, // 1
];
}

(async () => {
const dataDir = './data';
const preset = 'browser';
output['rgb-noexist'] =
await window.units['rgb-noexist'](ThreeGeo, dataDir, preset);
output['rgb-eiger'] =
await window.units['rgb-eiger'](ThreeGeo, dataDir, preset);
output['rgb-table'] =
await window.units['rgb-table'](ThreeGeo, dataDir, preset);
output['vec-table'] =
await window.units['vec-table'](ThreeGeo, dataDir, preset);

// store results
window['output'] = output;
})();
</script>
</body>
</html>
77 changes: 77 additions & 0 deletions tests/browser/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const path = require('path');
const fs = require('fs-extra');
const { Server } = require('es-pack-js');

const libName = 'three-geo';
const outDir = path.join(__dirname, '../../target');

const modPath = `${outDir}/${libName}.min.js`;
// const modPath = `${outDir}/${libName}.js`; // dev !!!!

const tmpModPath = `${__dirname}/__tmp.min.js`;

let output;
let server = null;
beforeAll(async () => {
const serveDir = __dirname;
server = await (new Server(serveDir)).listen();

const tmpThreePath = path.join(__dirname, './__three.min.js');
fs.copySync(path.join(__dirname, '../../node_modules/three/build/three.min.js'),
tmpThreePath);
fs.copySync(modPath, tmpModPath);

const page = await browser.newPage();
await page.goto(`http://localhost:${server.port}/index.html`);

expect(await page.title()).toBe('tests');

await page.waitForFunction(`typeof window.output === "object"`);
output = await page.evaluate(() => window.output);

fs.removeSync(tmpThreePath);
fs.removeSync(tmpModPath);
});
afterAll(async () => {
server.close();
server = null;
});

test('output', () => {
expect(typeof output).toBe('object');
});
test('`new`', () => {
expect(output['new']).toEqual(['function', 1]);
});

test('rgb-noexist`: case when no rgb DEM files were fetched', () => {
expect(output['rgb-noexist']).toEqual(null);
});
test('rgb-eiger', () => {
const { err, name, len, tile } = output['rgb-eiger'];
expect(err).toEqual(null);
expect(name).toEqual('dem-rgb');
expect(len).toEqual(4);

const [t0, t1, t2] = tile;
expect(t0 === 2138 || t0 === 2139).toBeTruthy();
expect(t1 === 1447 || t1 === 1448).toBeTruthy();
expect(t2).toBe(12);
});
test('rgb-table', () => {
const { err, name, len, tile } = output['rgb-table'];
expect(err).toEqual(null);
expect(name).toEqual('dem-rgb');
expect(len).toEqual(4);

const [t0, t1, t2] = tile;
expect(t0 === 9029 || t0 === 9030).toBeTruthy();
expect(t1 === 9836 || t1 === 9837).toBeTruthy();
expect(t2).toBe(14);
});
test('vec-table', () => {
const { err, name, len } = output['vec-table'];
expect(err).toEqual(null);
expect(name).toEqual('dem-vec');
expect(len).toBe(0); // Expect 0 for now; using an empty pbf
});
1 change: 1 addition & 0 deletions tests/browser/units.js
8 changes: 4 additions & 4 deletions tests/node/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ test('`new`', () => {
const units = require(path.join(__dirname, '../units.js'));
const dataDir = path.join(__dirname, '../data');

test('`getTerrainRgb()`: case when no DEM files were fetched', async () => {
test('rgb-noexist`: case when no rgb DEM files were fetched', async () => {
await units['rgb-noexist'](ThreeGeo, dataDir);
});

test('`getTerrainRgb()`: eiger', async () => {
test('rgb-eiger', async () => {
await units['rgb-eiger'](ThreeGeo, dataDir);
});

test('`getTerrainRgb()`: table', async () => {
test('rgb-table', async () => {
await units['rgb-table'](ThreeGeo, dataDir);
});

test('`getTerrainVector()`: table', async () => {
test('vec-table', async () => {
await units['vec-table'](ThreeGeo, dataDir);
});
90 changes: 62 additions & 28 deletions tests/units.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const units = {};

const setupApi = (tgeo, base) => {
tgeo.tokenMapbox = 'zzzz';
tgeo.setApiVector(`${base}/custom-terrain-vector`);
Expand All @@ -8,7 +6,7 @@ const setupApi = (tgeo, base) => {
};

const run = async fn => {
let err, out;
let err = null, out;
try {
out = await fn();
} catch (e) {
Expand All @@ -17,7 +15,9 @@ const run = async fn => {
return { err, out };
};

units['rgb-noexist'] = async (ThreeGeo, dataDir) => {
const units = {};

units['rgb-noexist'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

// The API call should return even when no rgb DEM files are fetched
Expand All @@ -27,10 +27,14 @@ units['rgb-noexist'] = async (ThreeGeo, dataDir) => {
const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));

expect(ret.err).toBe(undefined);
if (preset === 'browser') {
return ret.err;
} else {
expect(ret.err).toBe(null);
}
};

units['rgb-eiger'] = async (ThreeGeo, dataDir) => {
units['rgb-eiger'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'eiger', origin: [46.5763, 7.9904], radius: 5.0, zoom: 12};
Expand All @@ -39,17 +43,26 @@ units['rgb-eiger'] = async (ThreeGeo, dataDir) => {
const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));

expect(ret.err).toBe(undefined);
expect(ret.out.name).toBe('dem-rgb');
expect(ret.out.children.length).toBe(4);

const [t0, t1, t2] = ret.out.children[0].userData.threeGeo.tile;
expect(t0 === 2138 || t0 === 2139).toBeTruthy();
expect(t1 === 1447 || t1 === 1448).toBeTruthy();
expect(t2).toBe(12);
const err = ret.err;
const name = ret.out.name;
const len = ret.out.children.length;
const tile = ret.out.children[0].userData.threeGeo.tile;

if (preset === 'browser') {
return { err, name, len, tile };
} else {
expect(err).toBe(null);
expect(name).toBe('dem-rgb');
expect(len).toBe(4);

const [t0, t1, t2] = tile;
expect(t0 === 2138 || t0 === 2139).toBeTruthy();
expect(t1 === 1447 || t1 === 1448).toBeTruthy();
expect(t2).toBe(12);
}
};

units['rgb-table'] = async (ThreeGeo, dataDir) => {
units['rgb-table'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'table', origin: [-33.9625, 18.4107], radius: 1.25, zoom: 14};
Expand All @@ -58,17 +71,26 @@ units['rgb-table'] = async (ThreeGeo, dataDir) => {
const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));

expect(ret.err).toBe(undefined);
expect(ret.out.name).toBe('dem-rgb');
expect(ret.out.children.length).toBe(4);

const [t0, t1, t2] = ret.out.children[0].userData.threeGeo.tile;
expect(t0 === 9029 || t0 === 9030).toBeTruthy();
expect(t1 === 9836 || t1 === 9837).toBeTruthy();
expect(t2).toBe(14);
const err = ret.err;
const name = ret.out.name;
const len = ret.out.children.length;
const tile = ret.out.children[0].userData.threeGeo.tile;

if (preset === 'browser') {
return { err, name, len, tile };
} else {
expect(err).toBe(null);
expect(name).toBe('dem-rgb');
expect(len).toBe(4);

const [t0, t1, t2] = tile;
expect(t0 === 9029 || t0 === 9030).toBeTruthy();
expect(t1 === 9836 || t1 === 9837).toBeTruthy();
expect(t2).toBe(14);
}
};

units['vec-table'] = async (ThreeGeo, dataDir) => {
units['vec-table'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'table', origin: [-33.9625, 18.4107], radius: 1.25, zoom: 14};
Expand All @@ -77,9 +99,21 @@ units['vec-table'] = async (ThreeGeo, dataDir) => {
const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainVector(origin, radius, zoom));

expect(ret.err).toBe(undefined);
expect(ret.out.name).toBe('dem-vec');
expect(ret.out.children.length).toBe(0); // Expect 0 since using an empty pbf
const err = ret.err;
const name = ret.out.name;
const len = ret.out.children.length;

if (preset === 'browser') {
return { err, name, len };
} else {
expect(err).toBe(null);
expect(name).toBe('dem-vec');
expect(len).toBe(0); // Expect 0 for now; using an empty pbf
}
};

module.exports = units;
if (typeof document !== 'undefined') {
window.units = units;
}

module.exports = units; // (This should be a top-level statement.)

0 comments on commit 46bec3c

Please sign in to comment.