Add unittests for dates tools

This commit is contained in:
Lucas Verney 2018-11-29 14:48:20 +01:00
parent 2c741cfb5c
commit 15321d3405
4 changed files with 202 additions and 2 deletions

View File

@ -4,7 +4,15 @@
"presets": [ "presets": [
"@babel/preset-env" "@babel/preset-env"
], ],
"plugins": ["@babel/plugin-transform-modules-commonjs"] "plugins": [
"@babel/plugin-transform-modules-commonjs",
["module-resolver", {
"root": ["./src"],
"alias": {
"@": "./src"
}
}]
]
} }
}, },
"presets": [ "presets": [

View File

@ -39,6 +39,7 @@
"app-manifest-webpack-plugin": "^1.1.3", "app-manifest-webpack-plugin": "^1.1.3",
"babel-eslint": "^10.0.1", "babel-eslint": "^10.0.1",
"babel-loader": "^8.0.4", "babel-loader": "^8.0.4",
"babel-plugin-module-resolver": "^3.1.1",
"copy-webpack-plugin": "^4.6.0", "copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1", "css-loader": "^1.0.1",
"eslint": "^5.9.0", "eslint": "^5.9.0",

160
src/tools/date.spec.js Normal file
View File

@ -0,0 +1,160 @@
var assert = require('assert');
var date = require('./date');
describe('Date tools', function() {
describe('addLeadingZeros', function () {
it('should add leading zeros', function () {
assert.equal(date.addLeadingZeros(10, 2), '10');
assert.equal(date.addLeadingZeros(10, 4), '0010');
});
});
describe('formatDate', function () {
it('should format date correctly', function () {
var unix = new Date(0);
assert.equal(date.formatDate(unix, 'MM'), '01');
assert.equal(date.formatDate(unix, 'DD'), '01');
assert.equal(date.formatDate(unix, 'YYYY'), '1970');
var timeZoneOffset = date.addLeadingZeros(unix.getTimezoneOffset() / 60, 2);
assert.equal(
date.formatDate(unix, 'HH'),
timeZoneOffset
);
assert.equal(date.formatDate(unix, 'mm'), '00');
assert.equal(date.formatDate(unix, 'ddd'), 'Thu');
assert.equal(
date.formatDate(unix, 'ddd DD/MM/YYYY HH:mm'),
'Thu 01/01/1970 ' + timeZoneOffset + ':00'
);
});
});
describe('diff', function () {
it('should output correct diff', function () {
var from = new Date(0);
var to = new Date(1543498512000);
assert.deepEqual(
date.diff(from, to),
{
day: 17865,
hour: 428750,
millisecond: 1543498512000,
minute: 25724975,
month: 587,
second: 1543498512,
week: 2552,
year: 49
}
);
});
});
describe('selectUnits', function () {
it('should output correct units', function () {
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(100))
),
'second'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(30 * 1000))
),
'second'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(40 * 60 * 1000))
),
'minute'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(20 * 60 * 60 * 1000))
),
'hour'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(20 * 24 * 60 * 60 * 1000))
),
'day'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(5 * 7 * 24 * 60 * 60 * 1000))
),
'month'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(5 * 30 * 24 * 60 * 60 * 1000))
),
'month'
)
assert.equal(
date.selectUnits(
date.diff(new Date(0), new Date(5 * 365 * 24 * 60 * 60 * 1000))
),
'year'
)
});
});
describe('distanceInWords', function () {
it('should output correct distance in words', function () {
var now = (new Date()).getTime();
assert.equal(
date.distanceInWordsToNow(
new Date(now - 100)
),
'0 seconds ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 30 * 1000)
),
'30 seconds ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 40 * 60 * 1000)
),
'40 minutes ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 20 * 60 * 60 * 1000)
),
'20 hours ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 20 * 24 * 60 * 60 * 1000)
),
'20 days ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 5 * 7 * 24 * 60 * 60 * 1000)
),
'one month ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 5 * 30 * 24 * 60 * 60 * 1000)
),
'5 months ago'
)
assert.equal(
date.distanceInWordsToNow(
new Date(now - 5 * 365 * 24 * 60 * 60 * 1000)
),
'5 years ago'
)
});
})
});

View File

@ -1190,6 +1190,17 @@ babel-loader@^8.0.4:
mkdirp "^0.5.1" mkdirp "^0.5.1"
util.promisify "^1.0.0" util.promisify "^1.0.0"
babel-plugin-module-resolver@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/babel-plugin-module-resolver/-/babel-plugin-module-resolver-3.1.1.tgz#881cf67e3d4b8400d5eaaefc1be44d2dc1fe404f"
integrity sha512-1Q77Al4ydp6nYApJ7sQ2fmgz30WuQgJZegIYuyOdbdpxenB/bSezQ3hDPsumIXGlUS4vUIv+EwFjzzXZNWtARw==
dependencies:
find-babel-config "^1.1.0"
glob "^7.1.2"
pkg-up "^2.0.0"
reselect "^3.0.1"
resolve "^1.4.0"
babel-runtime@6.18.0: babel-runtime@6.18.0:
version "6.18.0" version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.18.0.tgz#0f4177ffd98492ef13b9f823e9994a02584c9078" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.18.0.tgz#0f4177ffd98492ef13b9f823e9994a02584c9078"
@ -3746,6 +3757,14 @@ finalhandler@1.1.1:
statuses "~1.4.0" statuses "~1.4.0"
unpipe "~1.0.0" unpipe "~1.0.0"
find-babel-config@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/find-babel-config/-/find-babel-config-1.1.0.tgz#acc01043a6749fec34429be6b64f542ebb5d6355"
integrity sha1-rMAQQ6Z0n+w0Qpvmtk9ULrtdY1U=
dependencies:
json5 "^0.5.1"
path-exists "^3.0.0"
find-cache-dir@^0.1.1: find-cache-dir@^0.1.1:
version "0.1.1" version "0.1.1"
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-0.1.1.tgz#c8defae57c8a52a8a784f9e31c57c742e993a0b9" resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-0.1.1.tgz#c8defae57c8a52a8a784f9e31c57c742e993a0b9"
@ -5298,7 +5317,7 @@ json3@^3.3.2:
version "3.3.2" version "3.3.2"
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1" resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
json5@^0.5.0: json5@^0.5.0, json5@^0.5.1:
version "0.5.1" version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
@ -6814,6 +6833,13 @@ pkg-dir@^3.0.0:
dependencies: dependencies:
find-up "^3.0.0" find-up "^3.0.0"
pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-2.0.0.tgz#c819ac728059a461cab1c3889a2be3c49a004d7f"
integrity sha1-yBmscoBZpGHKscOImivjxJoATX8=
dependencies:
find-up "^2.1.0"
pluralize@^7.0.0: pluralize@^7.0.0:
version "7.0.0" version "7.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777" resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777"
@ -7980,6 +8006,11 @@ requires-port@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
reselect@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/reselect/-/reselect-3.0.1.tgz#efdaa98ea7451324d092b2b2163a6a1d7a9a2147"
integrity sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc=
resize-img@^1.1.0: resize-img@^1.1.0:
version "1.1.2" version "1.1.2"
resolved "https://registry.yarnpkg.com/resize-img/-/resize-img-1.1.2.tgz#fad650faf3ef2c53ea63112bc272d95e9d92550e" resolved "https://registry.yarnpkg.com/resize-img/-/resize-img-1.1.2.tgz#fad650faf3ef2c53ea63112bc272d95e9d92550e"