Tofu/tests/dates/shiftWeeks.test.js

42 lines
1.3 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { shiftWeeks } from '#tofu/tools/dates/shiftWeeks.js'
test('moves forward by whole weeks', () => {
const shifted = shiftWeeks(new Date(2026, 8, 21), 1)
assert.equal(shifted.getMonth(), 8)
assert.equal(shifted.getDate(), 28)
})
test('moves backwards on a negative count', () => {
const shifted = shiftWeeks(new Date(2026, 8, 21), -1)
assert.equal(shifted.getMonth(), 8)
assert.equal(shifted.getDate(), 14)
})
test('crosses the year boundary', () => {
const shifted = shiftWeeks(new Date(2026, 11, 28), 1)
assert.equal(shifted.getFullYear(), 2027)
assert.equal(shifted.getMonth(), 0)
assert.equal(shifted.getDate(), 4)
})
test('returns an equal but distinct date on a zero count', () => {
const date = new Date(2026, 8, 21)
const shifted = shiftWeeks(date, 0)
assert.equal(shifted.getTime(), date.getTime())
assert.notEqual(shifted, date)
})
test('keeps the local time of day', () => {
const shifted = shiftWeeks(new Date(2026, 8, 21, 7, 30), 3)
assert.equal(shifted.getHours(), 7)
assert.equal(shifted.getMinutes(), 30)
})
test('does not mutate the received date', () => {
const date = new Date(2026, 8, 21, 7, 30)
const before = date.getTime()
shiftWeeks(date, 5)
assert.equal(date.getTime(), before)
})