From d583b2d80b1880514a75ac9c5e75a7c2f6ff7dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Sun, 4 Aug 2024 03:55:08 -0700 Subject: [PATCH] Added unit tests --- CONTRIBUTING.md | 4 ++++ index.js | 8 +++++++- index.spec.mjs | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 2 +- test/setup.mjs | 3 +++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 index.spec.mjs create mode 100644 test/setup.mjs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 822e580..a4569e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,3 +3,7 @@ ## Manual testing To test the plugin manually, run `npm start`. This command builds the plugin and serves it up along with a basic map webpage on a local Web server. + +## Automated testing + +There are a number of test cases for basic and edge cases. Run `npm test`. diff --git a/index.js b/index.js index ea9ded7..8e13080 100644 --- a/index.js +++ b/index.js @@ -100,8 +100,14 @@ function constrainFilterByDate(filter, decimalYear) { return dateFilter; } -if (window.maplibregl) { +if (typeof window !== 'undefined' && 'maplibregl' in window) { maplibregl.Map.prototype.filterByDate = function (date) { filterByDate(this, date); }; +} else if (typeof module !== 'undefined') { + module.exports = { + filterByDate: filterByDate, + decimalYearFromDate: decimalYearFromDate, + dateFromISODate: dateFromISODate, + }; } diff --git a/index.spec.mjs b/index.spec.mjs new file mode 100644 index 0000000..6146c5f --- /dev/null +++ b/index.spec.mjs @@ -0,0 +1,38 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { + dateFromISODate, + decimalYearFromDate, +} from './index.js'; + +describe('dateFromISODate', () => { + it('should convert date strings to Date objects', () => { + assert.equal(+dateFromISODate('2013-01-01'), +new Date('2013-01-01')); + assert.equal(+dateFromISODate('2013-04-14'), +new Date('2013-04-14')); + assert.equal(+dateFromISODate('2013-12-31'), +new Date('2013-12-31')); + }); + + it('should support BCE dates', () => { + assert.equal(+dateFromISODate('0001-01-01'), +new Date('0001-01-01')); + assert.equal(+dateFromISODate('0000-01-01'), +new Date('0000-01-01')); + assert.equal(+dateFromISODate('-0000-01-01'), +new Date('0000-01-01')); + assert.equal(+dateFromISODate('-0001-01-01'), +new Date('-000001-01-01')); + assert.equal(+dateFromISODate('-9999-01-01'), +new Date('-009999-01-01')); + }); +}); + +describe('decimalYearFromDate', () => { + it('should convert date objects to decimal years', () => { + assert.equal(decimalYearFromDate(new Date('2013-01-01')), 2013); + assert.equal(+decimalYearFromDate(new Date('2013-04-14')).toFixed(5), 2013.28219); + assert.equal(+decimalYearFromDate(new Date('2013-12-31')).toFixed(5), 2013.99726); + }); + + it('should support BCE dates', () => { + assert.equal(decimalYearFromDate(new Date('0001-01-01')), 1); + assert.equal(decimalYearFromDate(new Date('0000-01-01')), 0); + assert.equal(decimalYearFromDate(new Date('-000001-01-01')), -1); + assert.equal(decimalYearFromDate(new Date('-009999-01-01')), -9999); + }); +}); diff --git a/package.json b/package.json index 93028e5..a100ae0 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "esbuild index.js --bundle --outdir=dist", "clean": "shx rm -rf dist build", "start": "shx cp -r example dist && open 'http://127.0.0.1:8000/example/#map=15/29.9599/-90.0676&date=1900-01-01' && esbuild index.js --bundle --outdir=dist --servedir=dist --watch", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node --import ./test/setup.mjs --test 'index.spec.mjs'" }, "repository": { "type": "git", diff --git a/test/setup.mjs b/test/setup.mjs new file mode 100644 index 0000000..cf8822e --- /dev/null +++ b/test/setup.mjs @@ -0,0 +1,3 @@ +import { register } from 'node:module'; + +