Initial commit
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
formatDateForDisplay,
|
||||
formatDateForInput,
|
||||
parseInputDate,
|
||||
isValidDate,
|
||||
getDayName,
|
||||
getMonthName,
|
||||
} from '@/utils/dates'
|
||||
|
||||
describe('dates.ts utilities', () => {
|
||||
describe('formatDateForDisplay', () => {
|
||||
it('formats ISO string to Swedish display format', () => {
|
||||
const result = formatDateForDisplay('2026-06-15')
|
||||
expect(result).toBe('15 juni 2026')
|
||||
})
|
||||
|
||||
it('formats Date object to Swedish display format', () => {
|
||||
const date = new Date('2026-06-15T00:00:00Z')
|
||||
const result = formatDateForDisplay(date)
|
||||
expect(result).toBe('15 juni 2026')
|
||||
})
|
||||
|
||||
it('handles January correctly', () => {
|
||||
const result = formatDateForDisplay('2026-01-01')
|
||||
expect(result).toBe('1 januari 2026')
|
||||
})
|
||||
|
||||
it('handles December correctly', () => {
|
||||
const result = formatDateForDisplay('2026-12-25')
|
||||
expect(result).toBe('25 december 2026')
|
||||
})
|
||||
|
||||
it('returns empty string for invalid date', () => {
|
||||
const result = formatDateForDisplay('invalid')
|
||||
expect(result).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatDateForInput', () => {
|
||||
it('formats ISO string to input format', () => {
|
||||
const result = formatDateForInput('2026-06-15')
|
||||
expect(result).toBe('2026-06-15')
|
||||
})
|
||||
|
||||
it('formats Date object to input format', () => {
|
||||
const date = new Date('2026-06-15T00:00:00Z')
|
||||
const result = formatDateForInput(date)
|
||||
expect(result).toBe('2026-06-15')
|
||||
})
|
||||
|
||||
it('pads month and day with zeros', () => {
|
||||
const result = formatDateForInput('2026-01-05')
|
||||
expect(result).toBe('2026-01-05')
|
||||
})
|
||||
|
||||
it('returns empty string for invalid date', () => {
|
||||
const result = formatDateForInput('invalid')
|
||||
expect(result).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('parseInputDate', () => {
|
||||
it('parses ISO date string to Date object', () => {
|
||||
const result = parseInputDate('2026-06-15')
|
||||
expect(result).toBeInstanceOf(Date)
|
||||
expect(result.toISOString()).toContain('2026-06-15')
|
||||
})
|
||||
|
||||
it('creates valid Date for valid input', () => {
|
||||
const result = parseInputDate('2026-06-15')
|
||||
expect(isValidDate(result)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isValidDate', () => {
|
||||
it('returns true for valid Date object', () => {
|
||||
const date = new Date('2026-06-15')
|
||||
expect(isValidDate(date)).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for invalid Date', () => {
|
||||
const date = new Date('invalid')
|
||||
expect(isValidDate(date)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for non-Date values', () => {
|
||||
expect(isValidDate('2026-06-15')).toBe(false)
|
||||
expect(isValidDate(20260615)).toBe(false)
|
||||
expect(isValidDate(null)).toBe(false)
|
||||
expect(isValidDate(undefined)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getDayName', () => {
|
||||
it('returns Swedish day name', () => {
|
||||
const date = new Date('2026-06-15T00:00:00Z') // Monday
|
||||
const result = getDayName(date)
|
||||
expect(result.toLowerCase()).toBe('måndag')
|
||||
})
|
||||
|
||||
it('returns empty string for invalid date', () => {
|
||||
const result = getDayName(new Date('invalid'))
|
||||
expect(result).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getMonthName', () => {
|
||||
it('returns Swedish month name', () => {
|
||||
const date = new Date('2026-06-15T00:00:00Z')
|
||||
const result = getMonthName(date)
|
||||
expect(result).toBe('juni')
|
||||
})
|
||||
|
||||
it('returns correct month names for all months', () => {
|
||||
const monthNames = [
|
||||
'januari',
|
||||
'februari',
|
||||
'mars',
|
||||
'april',
|
||||
'maj',
|
||||
'juni',
|
||||
'juli',
|
||||
'augusti',
|
||||
'september',
|
||||
'oktober',
|
||||
'november',
|
||||
'december',
|
||||
]
|
||||
|
||||
monthNames.forEach((monthName, index) => {
|
||||
const date = new Date(`2026-${String(index + 1).padStart(2, '0')}-15T00:00:00Z`)
|
||||
const result = getMonthName(date)
|
||||
expect(result).toBe(monthName)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns empty string for invalid date', () => {
|
||||
const result = getMonthName(new Date('invalid'))
|
||||
expect(result).toBe('')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { formatDayCount, formatPercentage } from '@/utils/formatting'
|
||||
|
||||
describe('formatting.ts utilities', () => {
|
||||
describe('formatDayCount', () => {
|
||||
it('returns singular form for 1 day', () => {
|
||||
expect(formatDayCount(1)).toBe('1 dag')
|
||||
})
|
||||
|
||||
it('returns plural form for 0 days', () => {
|
||||
expect(formatDayCount(0)).toBe('0 dagar')
|
||||
})
|
||||
|
||||
it('returns plural form for 2 days', () => {
|
||||
expect(formatDayCount(2)).toBe('2 dagar')
|
||||
})
|
||||
|
||||
it('returns plural form for many days', () => {
|
||||
expect(formatDayCount(25)).toBe('25 dagar')
|
||||
expect(formatDayCount(365)).toBe('365 dagar')
|
||||
})
|
||||
|
||||
it('handles large numbers', () => {
|
||||
expect(formatDayCount(1000)).toBe('1000 dagar')
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatPercentage', () => {
|
||||
it('formats simple percentage', () => {
|
||||
expect(formatPercentage(1, 4)).toBe('25%')
|
||||
})
|
||||
|
||||
it('rounds to nearest integer', () => {
|
||||
expect(formatPercentage(1, 3)).toBe('33%') // 33.333...%
|
||||
expect(formatPercentage(2, 3)).toBe('67%') // 66.666...%
|
||||
})
|
||||
|
||||
it('handles 0% correctly', () => {
|
||||
expect(formatPercentage(0, 10)).toBe('0%')
|
||||
})
|
||||
|
||||
it('handles 100% correctly', () => {
|
||||
expect(formatPercentage(10, 10)).toBe('100%')
|
||||
})
|
||||
|
||||
it('returns 0% when denominator is 0', () => {
|
||||
expect(formatPercentage(5, 0)).toBe('0%')
|
||||
})
|
||||
|
||||
it('handles fractional results with proper rounding', () => {
|
||||
expect(formatPercentage(1, 2)).toBe('50%')
|
||||
expect(formatPercentage(3, 4)).toBe('75%')
|
||||
})
|
||||
|
||||
it('works with large numbers', () => {
|
||||
expect(formatPercentage(500, 1000)).toBe('50%')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user