Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Aug 4, 2024
1 parent 6858301 commit d583b2d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
38 changes: 38 additions & 0 deletions index.spec.mjs
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { register } from 'node:module';


0 comments on commit d583b2d

Please sign in to comment.