Files
malio-layer-ui/app/components/malio/date/composables/dateWeek.test.ts

75 lines
2.4 KiB
TypeScript

import {describe, expect, it} from 'vitest'
import {
formatWeekDisplay,
isValidIsoWeek,
isoWeekToMonday,
mondayOf,
sundayOf,
toIsoWeek,
} from './dateWeek'
describe('dateWeek', () => {
describe('mondayOf / sundayOf', () => {
it('returns Monday and Sunday of a midweek date', () => {
expect(mondayOf('2026-05-20')).toBe('2026-05-18') // mercredi
expect(sundayOf('2026-05-20')).toBe('2026-05-24')
})
it('keeps Monday on a Monday', () => {
expect(mondayOf('2026-05-18')).toBe('2026-05-18')
})
it('returns the preceding Monday for a Sunday', () => {
expect(mondayOf('2026-05-24')).toBe('2026-05-18')
})
})
describe('toIsoWeek', () => {
it('returns the ISO week of a date', () => {
expect(toIsoWeek('2026-05-20')).toBe('2026-W21')
})
it('handles year boundaries', () => {
expect(toIsoWeek('2026-01-01')).toBe('2026-W01')
expect(toIsoWeek('2025-12-31')).toBe('2026-W01')
expect(toIsoWeek('2027-01-01')).toBe('2026-W53')
})
})
describe('isoWeekToMonday', () => {
it('returns the Monday of a week string', () => {
expect(isoWeekToMonday('2026-W21')).toBe('2026-05-18')
})
it('round-trips with toIsoWeek', () => {
for (const w of ['2026-W01', '2026-W21', '2026-W53', '2024-W09']) {
const monday = isoWeekToMonday(w)
expect(monday).not.toBeNull()
expect(toIsoWeek(monday as string)).toBe(w)
}
})
it('returns null for invalid input', () => {
expect(isoWeekToMonday('2026-21')).toBeNull()
expect(isoWeekToMonday('2026-W00')).toBeNull()
expect(isoWeekToMonday('2026-W54')).toBeNull()
expect(isoWeekToMonday('2025-W53')).toBeNull() // 2025 n'a que 52 semaines ISO
})
})
describe('isValidIsoWeek', () => {
it('accepts a real ISO week', () => {
expect(isValidIsoWeek('2026-W21')).toBe(true)
})
it('rejects malformed or impossible weeks', () => {
expect(isValidIsoWeek('2026-21')).toBe(false)
expect(isValidIsoWeek('2026-W00')).toBe(false)
expect(isValidIsoWeek('2026-W54')).toBe(false)
})
})
describe('formatWeekDisplay', () => {
it('formats a week as a human label', () => {
expect(formatWeekDisplay('2026-W21')).toBe('Semaine 21 (18/05 → 24/05/2026)')
})
it('returns empty string for invalid input', () => {
expect(formatWeekDisplay('2026-W54')).toBe('')
})
})
})