|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + resolvePlural, |
| 4 | + formatDateSpec, |
| 5 | + formatNumberSpec, |
| 6 | + applyLocaleConfig, |
| 7 | + type SpecPluralRule, |
| 8 | +} from '../utils/spec-formatters'; |
| 9 | + |
| 10 | +// ============================================================================ |
| 11 | +// PluralRuleSchema Consumer Tests |
| 12 | +// ============================================================================ |
| 13 | + |
| 14 | +describe('resolvePlural', () => { |
| 15 | + const rule: SpecPluralRule = { |
| 16 | + key: 'items.count', |
| 17 | + zero: 'No items', |
| 18 | + one: '{count} item', |
| 19 | + other: '{count} items', |
| 20 | + }; |
| 21 | + |
| 22 | + it('should resolve zero form', () => { |
| 23 | + // English 'other' is used for 0 by Intl.PluralRules, but 'zero' is explicitly checked |
| 24 | + // In English, 0 maps to "other" category, but we fall back to rule.other |
| 25 | + const result = resolvePlural(rule, 0, 'en'); |
| 26 | + expect(result).toBe('0 items'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should resolve one form', () => { |
| 30 | + expect(resolvePlural(rule, 1, 'en')).toBe('1 item'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should resolve other form for plural', () => { |
| 34 | + expect(resolvePlural(rule, 5, 'en')).toBe('5 items'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should fallback to other when form is not defined', () => { |
| 38 | + const simpleRule: SpecPluralRule = { |
| 39 | + key: 'items', |
| 40 | + other: '{count} things', |
| 41 | + }; |
| 42 | + expect(resolvePlural(simpleRule, 3, 'en')).toBe('3 things'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should work with different locales', () => { |
| 46 | + const ruleWithTwo: SpecPluralRule = { |
| 47 | + key: 'items', |
| 48 | + one: '{count} element', |
| 49 | + two: '{count} éléments (dual)', |
| 50 | + other: '{count} éléments', |
| 51 | + }; |
| 52 | + // Arabic has a 'two' plural category |
| 53 | + expect(resolvePlural(ruleWithTwo, 2, 'ar')).toBe('2 éléments (dual)'); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +// ============================================================================ |
| 58 | +// DateFormatSchema Consumer Tests |
| 59 | +// ============================================================================ |
| 60 | + |
| 61 | +describe('formatDateSpec', () => { |
| 62 | + it('should format date with dateStyle', () => { |
| 63 | + const date = new Date('2026-02-11T12:00:00Z'); |
| 64 | + const result = formatDateSpec(date, { dateStyle: 'short' }, 'en-US'); |
| 65 | + expect(result).toBeTruthy(); |
| 66 | + expect(typeof result).toBe('string'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return string for invalid date', () => { |
| 70 | + expect(formatDateSpec('invalid-date', { dateStyle: 'medium' })).toBe('invalid-date'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle timeZone', () => { |
| 74 | + const date = new Date('2026-02-11T12:00:00Z'); |
| 75 | + const result = formatDateSpec(date, { |
| 76 | + dateStyle: 'medium', |
| 77 | + timeZone: 'America/New_York', |
| 78 | + }, 'en-US'); |
| 79 | + expect(result).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle hour12 option', () => { |
| 83 | + const date = new Date('2026-02-11T15:30:00Z'); |
| 84 | + const result12 = formatDateSpec(date, { timeStyle: 'short', hour12: true }, 'en-US'); |
| 85 | + const result24 = formatDateSpec(date, { timeStyle: 'short', hour12: false }, 'en-US'); |
| 86 | + expect(result12).toBeTruthy(); |
| 87 | + expect(result24).toBeTruthy(); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +// ============================================================================ |
| 92 | +// NumberFormatSchema Consumer Tests |
| 93 | +// ============================================================================ |
| 94 | + |
| 95 | +describe('formatNumberSpec', () => { |
| 96 | + it('should format decimal numbers', () => { |
| 97 | + const result = formatNumberSpec(1234.56, { style: 'decimal' }, 'en-US'); |
| 98 | + expect(result).toContain('1,234'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should format currency', () => { |
| 102 | + const result = formatNumberSpec(42.99, { |
| 103 | + style: 'currency', |
| 104 | + currency: 'USD', |
| 105 | + }, 'en-US'); |
| 106 | + expect(result).toContain('$'); |
| 107 | + expect(result).toContain('42.99'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should format percentage', () => { |
| 111 | + const result = formatNumberSpec(0.75, { style: 'percent' }, 'en-US'); |
| 112 | + expect(result).toContain('75'); |
| 113 | + expect(result).toContain('%'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should respect fraction digits', () => { |
| 117 | + const result = formatNumberSpec(3.14159, { |
| 118 | + style: 'decimal', |
| 119 | + maximumFractionDigits: 2, |
| 120 | + }, 'en-US'); |
| 121 | + expect(result).toBe('3.14'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should respect useGrouping', () => { |
| 125 | + const withGrouping = formatNumberSpec(1234567, { |
| 126 | + style: 'decimal', |
| 127 | + useGrouping: true, |
| 128 | + }, 'en-US'); |
| 129 | + const withoutGrouping = formatNumberSpec(1234567, { |
| 130 | + style: 'decimal', |
| 131 | + useGrouping: false, |
| 132 | + }, 'en-US'); |
| 133 | + expect(withGrouping).toContain(','); |
| 134 | + expect(withoutGrouping).not.toContain(','); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +// ============================================================================ |
| 139 | +// LocaleConfigSchema Consumer Tests |
| 140 | +// ============================================================================ |
| 141 | + |
| 142 | +describe('applyLocaleConfig', () => { |
| 143 | + it('should create bound formatting functions', () => { |
| 144 | + const locale = applyLocaleConfig({ |
| 145 | + code: 'en-US', |
| 146 | + direction: 'ltr', |
| 147 | + numberFormat: { style: 'decimal', useGrouping: true }, |
| 148 | + dateFormat: { dateStyle: 'medium' }, |
| 149 | + }); |
| 150 | + |
| 151 | + expect(locale.code).toBe('en-US'); |
| 152 | + expect(locale.direction).toBe('ltr'); |
| 153 | + expect(locale.fallbackChain).toEqual([]); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should format dates using configured locale', () => { |
| 157 | + const locale = applyLocaleConfig({ |
| 158 | + code: 'en-US', |
| 159 | + dateFormat: { dateStyle: 'short' }, |
| 160 | + }); |
| 161 | + |
| 162 | + const result = locale.formatDate(new Date('2026-02-11')); |
| 163 | + expect(result).toBeTruthy(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should format numbers using configured locale', () => { |
| 167 | + const locale = applyLocaleConfig({ |
| 168 | + code: 'en-US', |
| 169 | + numberFormat: { style: 'decimal', maximumFractionDigits: 2 }, |
| 170 | + }); |
| 171 | + |
| 172 | + const result = locale.formatNumber(1234.5678); |
| 173 | + expect(result).toBeTruthy(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should allow overrides in formatting calls', () => { |
| 177 | + const locale = applyLocaleConfig({ |
| 178 | + code: 'en-US', |
| 179 | + numberFormat: { style: 'decimal' }, |
| 180 | + }); |
| 181 | + |
| 182 | + const result = locale.formatNumber(42.99, { |
| 183 | + style: 'currency', |
| 184 | + currency: 'EUR', |
| 185 | + }); |
| 186 | + expect(result).toContain('€'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should resolve plurals with configured locale', () => { |
| 190 | + const locale = applyLocaleConfig({ |
| 191 | + code: 'en', |
| 192 | + }); |
| 193 | + |
| 194 | + const rule: SpecPluralRule = { |
| 195 | + key: 'items', |
| 196 | + one: '{count} item', |
| 197 | + other: '{count} items', |
| 198 | + }; |
| 199 | + |
| 200 | + expect(locale.resolvePlural(rule, 1)).toBe('1 item'); |
| 201 | + expect(locale.resolvePlural(rule, 5)).toBe('5 items'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should default direction to ltr', () => { |
| 205 | + const locale = applyLocaleConfig({ code: 'en' }); |
| 206 | + expect(locale.direction).toBe('ltr'); |
| 207 | + }); |
| 208 | +}); |
0 commit comments