|
3 | 3 | import assert from 'assert' |
4 | 4 | import roundToNearestMinutes, { RoundToNearestMinutesOptions } from './index' |
5 | 5 |
|
6 | | -function makeDate(minutes: number, seconds: number = 0) { |
| 6 | +function makeDate( |
| 7 | + minutes: number, |
| 8 | + seconds: number = 0, |
| 9 | + milliseconds: number = 0 |
| 10 | +) { |
7 | 11 | // helper to make tests more readable since we mostly care about minutes and seconds |
8 | | - return new Date(2014, 6 /* Jul */, 10, 12, minutes, seconds, 0) |
| 12 | + return new Date(2014, 6 /* Jul */, 10, 12, minutes, seconds, milliseconds) |
9 | 13 | } |
10 | 14 |
|
11 | 15 | describe('roundToNearestMinutes', () => { |
@@ -51,13 +55,6 @@ describe('roundToNearestMinutes', () => { |
51 | 55 | ) |
52 | 56 | }) |
53 | 57 |
|
54 | | - it('rounds up to the next day', () => { |
55 | | - assert.deepStrictEqual( |
56 | | - roundToNearestMinutes(new Date(2014, 6, 10, 23, 59, 59)), |
57 | | - new Date(2014, 6, 11) |
58 | | - ) |
59 | | - }) |
60 | | - |
61 | 58 | describe('roundingMethod', () => { |
62 | 59 | it('trunc, nearestTo === 1 (default)', () => { |
63 | 60 | const options: RoundToNearestMinutesOptions = { roundingMethod: 'trunc' } |
@@ -248,6 +245,27 @@ describe('roundToNearestMinutes', () => { |
248 | 245 | }) |
249 | 246 | }) |
250 | 247 |
|
| 248 | + describe('edge cases', () => { |
| 249 | + it('rounds up to the next day', () => { |
| 250 | + assert.deepStrictEqual( |
| 251 | + roundToNearestMinutes(new Date(2014, 6, 10, 23, 59, 59)), |
| 252 | + new Date(2014, 6, 11) |
| 253 | + ) |
| 254 | + }) |
| 255 | + |
| 256 | + it('ceils correctly with 0 seconds and 1 millisecond', () => { |
| 257 | + assert.deepStrictEqual( |
| 258 | + roundToNearestMinutes(makeDate(15, 0, 0), { roundingMethod: 'ceil' }), |
| 259 | + makeDate(15) |
| 260 | + ) |
| 261 | + |
| 262 | + assert.deepStrictEqual( |
| 263 | + roundToNearestMinutes(makeDate(15, 0, 1), { roundingMethod: 'ceil' }), |
| 264 | + makeDate(16) |
| 265 | + ) |
| 266 | + }) |
| 267 | + }) |
| 268 | + |
251 | 269 | describe('examples', () => { |
252 | 270 | it('example 1', () => { |
253 | 271 | const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34)) |
|
0 commit comments