Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

wip

Mike McGann3f21ed858034
diff --git a/.babelrc b/.babelrc
index d678299fc..a25fd73a6 100644
--- a/.babelrc
+++ b/.babelrc
@@ -3,5 +3,10 @@
"env",
"react",
"stage-2"
- ]
+ ],
+ "env": {
+ "test": {
+ "plugins": ["transform-es2015-modules-commonjs"]
+ }
+ }
}
\ No newline at end of file
diff --git a/.eslintrc b/.eslintrc
index 7c1aaeda9..73d56eaaf 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -1,7 +1,8 @@
{
"env": {
"browser": true,
- "node": true
+ "node": true,
+ "jest/globals": true
},
"parserOptions": {
"ecmaVersion": 2017,
@@ -10,7 +11,7 @@
"jsx": true
}
},
- "plugins": ["react"],
+ "plugins": ["react", "jest"],
"extends": ["standard", "plugin:react/recommended"],
"rules": {
"semi": ["error", "always"],
diff --git a/package.json b/package.json
index c3e3c4a46..24188d7e1 100644
--- a/package.json
+++ b/package.json
@@ -59,12 +59,9 @@
"python:win32": "bash -c 'tasks/pythonInstall.sh'",
"report": "node e2e/create-report.js",
"start": "node ./tasks/start.js",
- "test": "npm run lint && run-p test:server test:run",
- "test:server": "buster-server -c",
- "test:run": "sleep 2 && buster-test && npm run test:end",
- "test:end": "run-script-os",
- "test:end:darwin:linux": "pkill -f run-p",
- "test:end:win32": "tskill node",
+ "test": "npm run test:unit && npm run test:lint",
+ "test:lint": "npx eslint .",
+ "test:unit": "npx jest",
"updateconfig": "run-s getcapabilities build:config",
"watch": "cross-env NODE_ENV=development webpack-dev-server",
"upload": "node tasks/upload.js"
@@ -94,6 +91,7 @@
"eslint": "^4.13.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
+ "eslint-plugin-jest": "^21.22.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-react": "^5.2.2",
@@ -108,6 +106,7 @@
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"imports-loader": "^0.8.0",
+ "jest": "^23.5.0",
"less": "^3.8.0",
"less-loader": "^4.1.0",
"mini-css-extract-plugin": "^0.4.1",
@@ -189,5 +188,13 @@
"isotope": "./node_modules/isotope-layout/dist/isotope.pkgd.js",
"jquery-jcrop": "./node_modules/jquery-jcrop/js/jquery.Jcrop.js",
"jquery": "./node_modules/jquery/dist/jquery.js"
+ },
+ "jest": {
+ "setupFiles": [
+ "./test-env.js"
+ ],
+ "transformIgnorePatterns": [
+ "node_modules/(?!(ol)/)"
+ ]
}
}
diff --git a/test-env.js b/test-env.js
new file mode 100644
index 000000000..b8b3275d7
--- /dev/null
+++ b/test-env.js
@@ -0,0 +1,2 @@
+import $ from 'jquery';
+global.$ = global.jQuery = $;
diff --git a/test/projection/model-spec.js b/test/projection/model-spec.js
deleted file mode 100644
index a04a1b831..000000000
--- a/test/projection/model-spec.js
+++ /dev/null
@@ -1,80 +0,0 @@
-buster.testCase('wv.proj.model', {
-
- config: null,
-
- setUp: function () {
- this.config = {
- defaults: {
- projection: 'geographic'
- },
- projections: {
- 'geographic': {
- id: 'geographic',
- epsg: '4326',
- crs: 'EPSG:4326'
- },
- 'arctic': {
- id: 'arctic',
- epsg: '3413',
- crs: 'EPSG:3413'
- }
- }
- };
- },
-
- 'Initializes with default': function () {
- var model = wv.proj.model(this.config);
- buster.assert.equals(model.selected.id, 'geographic');
- buster.assert.equals(model.selected.crs, 'EPSG:4326');
- },
-
- 'Throws exception witn an invalid default': function () {
- this.config.defaults.projection = 'invalid';
- var self = this;
- buster.assert.exception(function () {
- wv.proj.model(self.config);
- });
- },
-
- 'Selects projection': function () {
- var model = wv.proj.model(this.config);
- var listener = this.stub();
- model.events.on('select', listener);
- model.select('arctic');
- buster.assert.equals(model.selected.id, 'arctic');
- buster.assert.equals(model.selected.crs, 'EPSG:3413');
- buster.assert.calledOnce(listener);
- },
-
- /* FIXME: OL3
- "Registers projection": function() {
- var model = wv.proj.model(this.config);
- model.select("arctic");
- buster.assert.equals(Proj4js.defs["EPSG:3413"], "Proj4 def");
- },
- */
-
- 'Event not fired if selection does not change': function () {
- var model = wv.proj.model(this.config);
- var listener = this.stub();
- model.events.on('select', listener);
- model.select('geographic');
- buster.refute.called(listener);
- },
-
- 'Saves state': function () {
- var model = wv.proj.model(this.config);
- var state = {};
- model.save(state);
- buster.assert.equals(state.p, 'geographic');
- },
-
- 'Loads state': function () {
- var model = wv.proj.model(this.config);
- var state = {
- 'p': 'arctic'
- };
- model.load(state);
- buster.assert.equals(model.selected.id, 'arctic');
- }
-});
diff --git a/test/projection/projection-spec.js b/test/projection/projection-spec.js
deleted file mode 100644
index 554a36350..000000000
--- a/test/projection/projection-spec.js
+++ /dev/null
@@ -1,43 +0,0 @@
-buster.testCase('wv.proj', {
-
- config: null,
-
- setUp: function () {
- this.config = {
- projections: {
- geographic: {}
- }
- };
- },
-
- '1.1: Parses valid projection': function () {
- var state = {
- 'switch': 'geographic'
- };
- var errors = [];
- wv.proj.parse(state, errors, this.config);
- buster.assert.equals(state.p, 'geographic');
- buster.assert.equals(errors.length, 0);
- },
-
- '1.2: Parses valid projection': function () {
- var state = {
- 'p': 'geographic'
- };
- var errors = [];
- wv.proj.parse(state, errors, this.config);
- buster.assert.equals(state.p, 'geographic');
- buster.assert.equals(errors.length, 0);
- },
-
- 'Rejects unsupported projection': function () {
- var state = {
- 'p': 'albers'
- };
- var errors = [];
- wv.proj.parse(state, errors, this.config);
- buster.refute(state.p);
- buster.assert.equals(errors.length, 1);
- }
-
-});
diff --git a/web/js/projection/model.test.js b/web/js/projection/model.test.js
new file mode 100644
index 000000000..71f832af0
--- /dev/null
+++ b/web/js/projection/model.test.js
@@ -0,0 +1,67 @@
+import { projectionModel } from './model';
+
+function testConfig() {
+ return {
+ defaults: {
+ projection: 'geographic'
+ },
+ projections: {
+ 'geographic': {
+ id: 'geographic',
+ epsg: '4326',
+ crs: 'EPSG:4326'
+ },
+ 'arctic': {
+ id: 'arctic',
+ epsg: '3413',
+ crs: 'EPSG:3413'
+ }
+ }
+ };
+};
+
+test('initializes with default', () => {
+ let model = projectionModel(testConfig());
+ expect(model.selected.id).toBe('geographic');
+ expect(model.selected.crs).toBe('EPSG:4326');
+});
+
+test('throws exception with an invalid default', () => {
+ let config = testConfig();
+ config.defaults.projection = 'invalid';
+ expect(() => projectionModel(config)).toThrow();
+});
+
+test('selects projection', () => {
+ let model = projectionModel(testConfig());
+ let listener = jest.fn();
+ model.events.on('select', listener);
+ model.select('arctic');
+ expect(model.selected.id).toBe('arctic');
+ expect(model.selected.crs).toBe('EPSG:3413');
+ expect(listener.mock.calls.length === 1);
+});
+
+test('event not fired if selection does not change', () => {
+ let model = projectionModel(testConfig());
+ let listener = jest.fn();
+ model.events.on('select', listener);
+ model.select('geographic');
+ expect(listener.mock.calls.length === 0);
+});
+
+test('saves state', () => {
+ let model = projectionModel(testConfig());
+ let state = {};
+ model.save(state);
+ expect(state.p).toBe('geographic');
+});
+
+test('loads state', () => {
+ let model = projectionModel(testConfig());
+ let state = {
+ 'p': 'arctic'
+ };
+ model.load(state);
+ expect(model.selected.id).toBe('arctic');
+});
diff --git a/web/js/projection/projection.test.js b/web/js/projection/projection.test.js
new file mode 100644
index 000000000..67063d142
--- /dev/null
+++ b/web/js/projection/projection.test.js
@@ -0,0 +1,39 @@
+import { parse } from './projection';
+
+function testConfig() {
+ return {
+ projections: {
+ geographic: {}
+ }
+ };
+};
+
+test('1.1: parses valid projection', () => {
+ let state = {
+ 'switch': 'geographic'
+ };
+ let errors = [];
+ parse(state, errors, testConfig());
+ expect(state.p).toBe('geographic');
+ expect(errors).toHaveLength(0);
+});
+
+test('1.2: Parses valid projection', () => {
+ let state = {
+ 'p': 'geographic'
+ };
+ let errors = [];
+ parse(state, errors, testConfig());
+ expect(state.p).toBe('geographic');
+ expect(errors).toHaveLength(0);
+});
+
+test('Rejects unsupported projection', () => {
+ let state = {
+ 'p': 'albers'
+ };
+ let errors = [];
+ parse(state, errors, testConfig());
+ expect(state.p).toBeFalsy();
+ expect(errors).toHaveLength(1);
+});
diff --git a/web/js/util/util.test.js b/web/js/util/util.test.js
new file mode 100644
index 000000000..bbaa00f7f
--- /dev/null
+++ b/web/js/util/util.test.js
@@ -0,0 +1,112 @@
+import util from './util';
+
+test('repeat', () => {
+ expect(util.repeat('.', 5)).toBe('.....');
+});
+
+describe('fromQueryString', () => {
+ let tests = [
+ { qs: '', obj: {}, name: 'zero' },
+ { qs: '?foo=a&bar=b', obj: { foo: 'a', bar: 'b' }, name: 'two' },
+ { qs: 'foo=a&bar=b', obj: { foo: 'a', bar: 'b' }, name: 'no leading ?' },
+ { qs: '?foo=image%2fjpeg', obj: { foo: 'image/jpeg' }, name: 'decodes' }
+ ];
+
+ tests.forEach((t) => {
+ test(t.name, () => {
+ expect(util.fromQueryString(t.qs)).toEqual(t.obj);
+ });
+ });
+});
+
+describe('toQueryString', () => {
+ let tests = [
+ { qs: '', obj: {}, name: 'zero' },
+ { qs: '?foo=a&bar=b', obj: { foo: 'a', bar: 'b' }, name: 'two' },
+ { qs: '?foo=image%2Fjpeg', obj: { foo: 'image/jpeg' }, name: 'encodes' },
+ { qs: '?path=/a/b', obj: { path: '/a/b' }, exceptions: ['%2f'], name: 'exceptions' }
+ ];
+
+ tests.forEach((t) => {
+ test(t.name, () => {
+ expect(util.toQueryString(t.obj, t.exceptions)).toEqual(t.qs);
+ });
+ });
+});
+
+describe('parseDateUTC', () => {
+ let tests = [{
+ str: '2013-03-15T11:22:33Z',
+ date: new Date(Date.UTC(2013, 2, 15, 11, 22, 33)),
+ name: 'timestamp'
+ }, {
+ str: '2013-03-15T11:22:33Z',
+ date: new Date(Date.UTC(2013, 2, 15, 11, 22, 33)),
+ name: 'timestamp without Z'
+ }, {
+ str: '2013-03-15',
+ date: new Date(Date.UTC(2013, 2, 15)),
+ name: 'time'
+ }, {
+ str: 'x',
+ name: 'invalid'
+ }];
+
+ tests.forEach((t) => {
+ test(t.name, () => {
+ if (t.date) {
+ expect(util.parseDateUTC(t.str)).toEqual(t.date);
+ } else {
+ expect(() => util.parseDateUTC(t.str)).toThrow();
+ }
+ });
+ });
+});
+
+test('toJulianDate', () => {
+ let d = new Date(Date.UTC(2013, 0, 15));
+ expect(util.toJulianDate(d)).toBe('2013015');
+});
+
+test('toISOStringDate', () => {
+ let d = new Date(Date.UTC(2013, 0, 15));
+ expect(util.toISOStringDate(d)).toBe('2013-01-15');
+});
+
+test('toISOStringSeconds', () => {
+ let d = new Date(Date.UTC(2013, 0, 15, 11, 22, 33));
+ expect(util.toISOStringSeconds(d)).toBe('2013-01-15T11:22:33Z');
+});
+
+test('toHourMinutes', () => {
+ var d = new Date(Date.UTC(2013, 0, 15, 11, 22, 33));
+ expect(util.toHourMinutes(d)).toBe('11:22');
+});
+
+test('toCompactTimestamp', () => {
+ var d = new Date(Date.UTC(2013, 0, 15, 11, 22, 33, 444));
+ expect(util.toCompactTimestamp(d)).toBe('20130115112233444');
+});
+
+describe('fromCompactTimestamp', () => {
+ test('parses timestamp', () => {
+ let answer = new Date(Date.UTC(2013, 0, 15, 11, 22, 33, 444));
+ let result = util.fromCompactTimestamp('20130115112233444');
+ expect(answer.getTime()).toEqual(result.getTime());
+ });
+
+ test('invalid', () => {
+ expect(() => util.fromCompactTimestamp('x0130115112233444')).toThrow();
+ });
+});
+
+test('clearTimeUTC', () => {
+ var d = new Date(2013, 2, 15, 12, 34, 56, 789);
+ util.clearTimeUTC(d);
+ expect(2013).toBe(d.getUTCFullYear());
+ expect(2).toBe(d.getUTCMonth());
+ expect(15).toBe(d.getUTCDate());
+ expect(0).toBe(d.getUTCHours());
+ expect(0).toBe(d.getUTCMinutes());
+ expect(0).toBe(d.getUTCSeconds());
+});