From 46bec3c8ec725240c33abf773b3833217e855576 Mon Sep 17 00:00:00 2001 From: j-devel Date: Wed, 28 Oct 2020 13:37:40 +0700 Subject: [PATCH] Add tests for 'browser' preset --- package.json | 2 +- tests/browser/index.html | 39 +++++++++++++++++ tests/browser/main.test.js | 77 ++++++++++++++++++++++++++++++++ tests/browser/units.js | 1 + tests/node/main.test.js | 8 ++-- tests/units.js | 90 ++++++++++++++++++++++++++------------ 6 files changed, 184 insertions(+), 33 deletions(-) create mode 100644 tests/browser/index.html create mode 100644 tests/browser/main.test.js create mode 120000 tests/browser/units.js diff --git a/package.json b/package.json index 092d661..f695948 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/browser/index.html b/tests/browser/index.html new file mode 100644 index 0000000..cd943ea --- /dev/null +++ b/tests/browser/index.html @@ -0,0 +1,39 @@ + + + + + tests + + + + + + + + diff --git a/tests/browser/main.test.js b/tests/browser/main.test.js new file mode 100644 index 0000000..db81c82 --- /dev/null +++ b/tests/browser/main.test.js @@ -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 +}); diff --git a/tests/browser/units.js b/tests/browser/units.js new file mode 120000 index 0000000..613bfbe --- /dev/null +++ b/tests/browser/units.js @@ -0,0 +1 @@ +../units.js \ No newline at end of file diff --git a/tests/node/main.test.js b/tests/node/main.test.js index 65c30d7..8e55fc4 100644 --- a/tests/node/main.test.js +++ b/tests/node/main.test.js @@ -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); }); diff --git a/tests/units.js b/tests/units.js index b084978..f2b8967 100644 --- a/tests/units.js +++ b/tests/units.js @@ -1,5 +1,3 @@ -const units = {}; - const setupApi = (tgeo, base) => { tgeo.tokenMapbox = 'zzzz'; tgeo.setApiVector(`${base}/custom-terrain-vector`); @@ -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) { @@ -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 @@ -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}; @@ -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}; @@ -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}; @@ -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.)