diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/compareAsc/test.ts | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-master.tar.gz node-vendor-master.tar.bz2 node-vendor-master.zip |
Diffstat (limited to 'date-fns/src/compareAsc/test.ts')
-rw-r--r-- | date-fns/src/compareAsc/test.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/date-fns/src/compareAsc/test.ts b/date-fns/src/compareAsc/test.ts new file mode 100644 index 0000000..c12c560 --- /dev/null +++ b/date-fns/src/compareAsc/test.ts @@ -0,0 +1,81 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import compareAsc from '.' + +describe('compareAsc', function() { + it('returns 0 if the given dates are equal', function() { + const result = compareAsc( + new Date(1989, 6 /* Jul */, 10), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === 0) + }) + + it('returns -1 if the first date is before the second one', function() { + const result = compareAsc( + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === -1) + }) + + it('returns 1 if the first date is after the second one', function() { + const result = compareAsc( + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ) + assert(result === 1) + }) + + it('sorts the dates array in the chronological order when function is passed as the argument to Array.prototype.sort()', function() { + const unsortedArray = [ + new Date(1995, 6 /* Jul */, 2), + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10) + ] + + const sortedArray = [ + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10), + new Date(1995, 6 /* Jul */, 2) + ] + + + unsortedArray.sort(compareAsc) + const result = unsortedArray + + assert.deepStrictEqual(result, sortedArray) + }) + + it('accepts timestamps', function() { + const result = compareAsc( + new Date(1987, 1 /* Feb */, 11).getTime(), + new Date(1989, 6 /* Jul */, 10).getTime() + ) + assert(result === -1) + }) + + it('returns NaN if the first date is `Invalid Date`', function() { + const result = compareAsc(new Date(NaN), new Date(1989, 6 /* Jul */, 10)) + assert(isNaN(result)) + }) + + it('returns NaN if the second date is `Invalid Date`', function() { + const result = compareAsc(new Date(1989, 6 /* Jul */, 10), new Date(NaN)) + assert(isNaN(result)) + }) + + it('returns NaN if the both dates are `Invalid Date`', function() { + const result = compareAsc(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + //@ts-expect-error + assert.throws(compareAsc.bind(null), TypeError) + //@ts-expect-error + assert.throws(compareAsc.bind(null, 1), TypeError) + }) +}) |