Compare commits

...

2 Commits

Author SHA1 Message Date
Christian Byrne
119c9764d2 Merge branch 'main' into test-coverage-slack-e2e 2026-04-15 11:16:34 -07:00
bymyself
93fddf2343 test: add unit tests for numberUtil and dateTimeUtil
Cover clampPercentInt, formatPercent0, dateKey, isToday, isYesterday,
formatShortMonthDay, and formatClockTime utilities.
2026-04-14 21:25:28 -07:00
2 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
dateKey,
formatClockTime,
formatShortMonthDay,
isToday,
isYesterday
} from './dateTimeUtil'
describe('dateKey', () => {
it('returns YYYY-MM-DD for a given timestamp', () => {
// 2024-03-15 in UTC
const ts = new Date(2024, 2, 15, 10, 30).getTime()
expect(dateKey(ts)).toBe('2024-03-15')
})
it('zero-pads single-digit months and days', () => {
const ts = new Date(2024, 0, 5).getTime()
expect(dateKey(ts)).toBe('2024-01-05')
})
})
describe('isToday', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date(2024, 5, 15, 14, 0, 0))
})
afterEach(() => {
vi.useRealTimers()
})
it('returns true for a timestamp on the same day', () => {
const ts = new Date(2024, 5, 15, 8, 0, 0).getTime()
expect(isToday(ts)).toBe(true)
})
it('returns false for yesterday', () => {
const ts = new Date(2024, 5, 14, 23, 59, 59).getTime()
expect(isToday(ts)).toBe(false)
})
it('returns false for tomorrow', () => {
const ts = new Date(2024, 5, 16, 0, 0, 0).getTime()
expect(isToday(ts)).toBe(false)
})
})
describe('isYesterday', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date(2024, 5, 15, 14, 0, 0))
})
afterEach(() => {
vi.useRealTimers()
})
it('returns true for a timestamp yesterday', () => {
const ts = new Date(2024, 5, 14, 10, 0, 0).getTime()
expect(isYesterday(ts)).toBe(true)
})
it('returns false for today', () => {
const ts = new Date(2024, 5, 15, 10, 0, 0).getTime()
expect(isYesterday(ts)).toBe(false)
})
it('returns false for two days ago', () => {
const ts = new Date(2024, 5, 13, 10, 0, 0).getTime()
expect(isYesterday(ts)).toBe(false)
})
})
describe('formatShortMonthDay', () => {
it('formats a date as short month + day', () => {
const ts = new Date(2024, 0, 2, 12, 0, 0).getTime()
const result = formatShortMonthDay(ts, 'en-US')
expect(result).toBe('Jan 2')
})
})
describe('formatClockTime', () => {
it('formats time with hours, minutes, and seconds', () => {
const ts = new Date(2024, 5, 15, 14, 5, 6).getTime()
const result = formatClockTime(ts, 'en-GB')
// en-GB uses 24-hour format
expect(result).toBe('14:05:06')
})
})

View File

@@ -0,0 +1,55 @@
import { describe, expect, it } from 'vitest'
import { clampPercentInt, formatPercent0 } from './numberUtil'
describe('clampPercentInt', () => {
it('clamps undefined to 0', () => {
expect(clampPercentInt()).toBe(0)
expect(clampPercentInt(undefined)).toBe(0)
})
it('rounds to nearest integer', () => {
expect(clampPercentInt(42.3)).toBe(42)
expect(clampPercentInt(42.7)).toBe(43)
expect(clampPercentInt(0.5)).toBe(1)
})
it('clamps below 0 to 0', () => {
expect(clampPercentInt(-10)).toBe(0)
expect(clampPercentInt(-0.1)).toBe(0)
})
it('clamps above 100 to 100', () => {
expect(clampPercentInt(150)).toBe(100)
expect(clampPercentInt(100.4)).toBe(100)
})
it('returns boundary values as-is', () => {
expect(clampPercentInt(0)).toBe(0)
expect(clampPercentInt(100)).toBe(100)
})
})
describe('formatPercent0', () => {
it('formats a percentage in en-US locale', () => {
expect(formatPercent0('en-US', 42)).toBe('42%')
})
it('formats 0%', () => {
expect(formatPercent0('en-US', 0)).toBe('0%')
})
it('formats 100%', () => {
expect(formatPercent0('en-US', 100)).toBe('100%')
})
it('rounds fractional values before formatting', () => {
expect(formatPercent0('en-US', 42.7)).toBe('43%')
expect(formatPercent0('en-US', 42.3)).toBe('42%')
})
it('clamps out-of-range values', () => {
expect(formatPercent0('en-US', 150)).toBe('100%')
expect(formatPercent0('en-US', -10)).toBe('0%')
})
})