diff --git a/src/roundToNearestHours/test.ts b/src/roundToNearestHours/test.ts index b7c4e560e3..4623a23075 100644 --- a/src/roundToNearestHours/test.ts +++ b/src/roundToNearestHours/test.ts @@ -43,7 +43,58 @@ describe('roundToNearestHours', function () { it('does not mutate the original date', function () { const date = new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99) roundToNearestHours(date) - assert.deepStrictEqual(date, new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99)) + assert.deepStrictEqual( + date, + new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99) + ) + }) + + it('rounds according to the passed mode - floor', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 12, 30, 0, 5), + { roundingMethod: 'floor' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 13, 0, 0)) + }) + + it('rounds according to the passed mode - floor - when nearestTo is provided', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 15, 10, 30, 5), + { nearestTo: 4, roundingMethod: 'floor' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 16, 0, 0)) + }) + + it('rounds according to the passed mode - ceil', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 12, 10, 30, 5), + { roundingMethod: 'ceil' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 13, 0, 0)) + }) + + it('rounds according to the passed mode - ceil - when nearestTo is provided', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 12, 10, 30, 5), + { nearestTo: 4, roundingMethod: 'ceil' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 16, 0, 0)) + }) + + it('rounds according to the passed mode - round - when nearestTo is provided', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 12, 10, 30, 5), + { nearestTo: 4, roundingMethod: 'round' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 0, 0)) + }) + + it('rounds according to the passed mode - trunc - when nearestTo is provided', () => { + const result = roundToNearestHours( + new Date(2014, 6 /* Jul */, 10, 12, 10, 30, 5), + { nearestTo: 4, roundingMethod: 'trunc' } + ) + assert.deepStrictEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 0, 0)) }) it('returns `Invalid Date` if the given date is invalid', function () {