Tofu/tests/dates/getWeekId.test.js

37 lines
1.3 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { getWeekId } from '#tofu/tools/dates/getWeekId.js'
test('returns the ISO week of a plain week', () => {
assert.equal(getWeekId(new Date(2026, 8, 21)), '2026-W39')
})
test('gives the same identifier for every day of the week', () => {
assert.equal(getWeekId(new Date(2026, 8, 23, 18)), '2026-W39')
assert.equal(getWeekId(new Date(2026, 8, 27, 23)), '2026-W39')
})
test('pads the week number to two digits', () => {
assert.equal(getWeekId(new Date(2026, 0, 1)), '2026-W01')
})
test('keeps a January day in the previous week-year when its Thursday is there', () => {
// Sunday 2027-01-03 belongs to the week of Monday 2026-12-28.
assert.equal(getWeekId(new Date(2027, 0, 3)), '2026-W53')
})
test('moves a December day to the next week-year when its Thursday is there', () => {
// Monday 2024-12-30 belongs to the first ISO week of 2025.
assert.equal(getWeekId(new Date(2024, 11, 30)), '2025-W01')
})
test('numbers the 53rd week of a long ISO year', () => {
assert.equal(getWeekId(new Date(2020, 11, 31)), '2020-W53')
})
test('does not mutate the received date', () => {
const date = new Date(2026, 0, 1, 9, 45)
const before = date.getTime()
getWeekId(date)
assert.equal(date.getTime(), before)
})