@i18n-micro/core 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,464 +0,0 @@
1
- import type { PluralFunc, Translations } from '@i18n-micro/types'
2
- import { BaseI18n, type BaseI18nOptions } from '../src/base'
3
-
4
- // Test implementation of BaseI18n
5
- class TestI18n extends BaseI18n {
6
- private _locale: string
7
- private _fallbackLocale: string
8
- private _route: string
9
-
10
- constructor(locale: string, fallbackLocale: string, route: string, options?: BaseI18nOptions) {
11
- super(options)
12
- this._locale = locale
13
- this._fallbackLocale = fallbackLocale
14
- this._route = route
15
- }
16
-
17
- public getLocale(): string {
18
- return this._locale
19
- }
20
-
21
- public getFallbackLocale(): string {
22
- return this._fallbackLocale
23
- }
24
-
25
- public getRoute(): string {
26
- return this._route
27
- }
28
-
29
- public setLocale(locale: string): void {
30
- this._locale = locale
31
- }
32
-
33
- public setRoute(route: string): void {
34
- this._route = route
35
- }
36
- }
37
-
38
- describe('BaseI18n', () => {
39
- describe('Constructor', () => {
40
- test('should initialize with default options', () => {
41
- const i18n = new TestI18n('en', 'en', 'index')
42
- expect(i18n.getLocale()).toBe('en')
43
- expect(i18n.getFallbackLocale()).toBe('en')
44
- expect(i18n.getRoute()).toBe('index')
45
- })
46
-
47
- test('should initialize with custom storage', () => {
48
- const storage = { translations: new Map<string, Translations>() }
49
- const i18n = new TestI18n('en', 'en', 'index', { storage })
50
- expect(i18n).toBeDefined()
51
- })
52
-
53
- test('should initialize with custom plural function', () => {
54
- const customPlural: PluralFunc = () => 'custom'
55
- const i18n = new TestI18n('en', 'en', 'index', { plural: customPlural })
56
- expect(i18n).toBeDefined()
57
- })
58
-
59
- test('should initialize with missingWarn option', () => {
60
- const i18n = new TestI18n('en', 'en', 'index', { missingWarn: false })
61
- expect(i18n).toBeDefined()
62
- })
63
-
64
- test('should initialize with missingHandler', () => {
65
- const handler = jest.fn()
66
- const i18n = new TestI18n('en', 'en', 'index', { missingHandler: handler })
67
- expect(i18n).toBeDefined()
68
- })
69
- })
70
-
71
- describe('t() method', () => {
72
- test('should return empty string for empty key', () => {
73
- const i18n = new TestI18n('en', 'en', 'index')
74
- expect(i18n.t('')).toBe('')
75
- })
76
-
77
- test('should return translation for existing key', async () => {
78
- const i18n = new TestI18n('en', 'en', 'index')
79
- const translations: Translations = { greeting: 'Hello' }
80
- i18n['helper'].loadTranslations('en', translations)
81
-
82
- expect(i18n.t('greeting')).toBe('Hello')
83
- })
84
-
85
- test('should interpolate params in translation', async () => {
86
- const storage = { translations: new Map<string, Translations>() }
87
- const i18n = new TestI18n('en', 'en', 'index', { storage })
88
- const translations: Translations = { greeting: 'Hello, {name}!' }
89
- await i18n['helper'].loadTranslations('en', translations)
90
-
91
- expect(i18n.t('greeting', { name: 'John' })).toBe('Hello, John!')
92
- })
93
-
94
- test('should use defaultValue when translation is missing', () => {
95
- const i18n = new TestI18n('en', 'en', 'index')
96
- expect(i18n.t('missing.key', undefined, 'Default value')).toBe('Default value')
97
- })
98
-
99
- test('should return key when translation is missing and no defaultValue', () => {
100
- const i18n = new TestI18n('en', 'en', 'index')
101
- expect(i18n.t('missing.key')).toBe('missing.key')
102
- })
103
-
104
- test('should fallback to fallbackLocale when translation is missing', async () => {
105
- const storage = { translations: new Map<string, Translations>() }
106
- const i18n = new TestI18n('en', 'fr', 'index', { storage })
107
- const translations: Translations = { greeting: 'Bonjour' }
108
- await i18n['helper'].loadTranslations('fr', translations)
109
-
110
- expect(i18n.t('greeting')).toBe('Bonjour')
111
- })
112
-
113
- test('should use route-specific translation when routeName is provided', async () => {
114
- const i18n = new TestI18n('en', 'en', 'index')
115
- const routeTranslations: Translations = { title: 'Route Title' }
116
- await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
117
-
118
- expect(i18n.t('title', undefined, undefined, 'about')).toBe('Route Title')
119
- })
120
-
121
- test('should call missingHandler when translation is missing', () => {
122
- const handler = jest.fn()
123
- const i18n = new TestI18n('en', 'en', 'index', { missingHandler: handler })
124
-
125
- i18n.t('missing.key')
126
-
127
- expect(handler).toHaveBeenCalledWith('en', 'missing.key', 'index')
128
- })
129
-
130
- test('should call customMissingHandler when set (Nuxt runtime)', () => {
131
- const customHandler = jest.fn()
132
- const i18n = new TestI18n('en', 'en', 'index', {
133
- getCustomMissingHandler: () => customHandler,
134
- })
135
-
136
- i18n.t('missing.key')
137
-
138
- expect(customHandler).toHaveBeenCalledWith('en', 'missing.key', 'index')
139
- })
140
-
141
- test('should not warn when missingWarn is false', () => {
142
- const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
143
- const i18n = new TestI18n('en', 'en', 'index', { missingWarn: false })
144
-
145
- i18n.t('missing.key')
146
-
147
- expect(consoleSpy).not.toHaveBeenCalled()
148
- consoleSpy.mockRestore()
149
- })
150
- })
151
-
152
- describe('ts() method', () => {
153
- test('should return translation as string', async () => {
154
- const i18n = new TestI18n('en', 'en', 'index')
155
- const translations: Translations = { greeting: 'Hello' }
156
- i18n['helper'].loadTranslations('en', translations)
157
-
158
- expect(i18n.ts('greeting')).toBe('Hello')
159
- })
160
-
161
- test('should return defaultValue when translation is missing', () => {
162
- const i18n = new TestI18n('en', 'en', 'index')
163
- expect(i18n.ts('missing.key', undefined, 'Default')).toBe('Default')
164
- })
165
-
166
- test('should return key when translation is missing and no defaultValue', () => {
167
- const i18n = new TestI18n('en', 'en', 'index')
168
- expect(i18n.ts('missing.key')).toBe('missing.key')
169
- })
170
-
171
- test('should convert non-string values to string', async () => {
172
- const i18n = new TestI18n('en', 'en', 'index')
173
- const translations: Translations = { count: 42 }
174
- i18n['helper'].loadTranslations('en', translations)
175
-
176
- expect(i18n.ts('count')).toBe('42')
177
- })
178
- })
179
-
180
- describe('tc() method', () => {
181
- test('should return defaultValue when count is undefined', () => {
182
- const i18n = new TestI18n('en', 'en', 'index')
183
- expect(i18n.tc('apples', { other: 'params' }, 'No count')).toBe('No count')
184
- })
185
-
186
- test('should use plural function with count', async () => {
187
- const i18n = new TestI18n('en', 'en', 'index')
188
- const translations: Translations = { apples: 'apple|apples' }
189
- await i18n['helper'].loadTranslations('en', translations)
190
-
191
- // defaultPlural selects form by index: forms[count] or last form if count >= forms.length
192
- // For 'apple|apples': forms[0]='apple', forms[1]='apples'
193
- expect(i18n.tc('apples', 0)).toBe('apple')
194
- expect(i18n.tc('apples', 1)).toBe('apples') // forms[1]
195
- expect(i18n.tc('apples', 5)).toBe('apples') // last form
196
- })
197
-
198
- test('should handle count as number', async () => {
199
- const i18n = new TestI18n('en', 'en', 'index')
200
- const translations: Translations = { apples: 'apple|apples' }
201
- i18n['helper'].loadTranslations('en', translations)
202
-
203
- expect(i18n.tc('apples', 2)).toBe('apples')
204
- })
205
-
206
- test('should handle count as Params object', async () => {
207
- const i18n = new TestI18n('en', 'en', 'index')
208
- const translations: Translations = { apples: 'apple|apples' }
209
- i18n['helper'].loadTranslations('en', translations)
210
-
211
- expect(i18n.tc('apples', { count: 2, name: 'John' })).toBe('apples')
212
- })
213
-
214
- test('should return defaultValue when plural function returns null', () => {
215
- const i18n = new TestI18n('en', 'en', 'index')
216
- // When translation is missing, t() returns key, which is passed to pluralFunc
217
- // pluralFunc tries to process 'missing.key' as translation, but since it doesn't contain '|',
218
- // it returns the key itself. So tc returns the key, not defaultValue.
219
- // To test defaultValue, we need a case where pluralFunc actually returns null.
220
- // This happens when translation exists but is empty or invalid.
221
- expect(i18n.tc('missing.key', 1, 'Default')).toBe('missing.key')
222
- })
223
- })
224
-
225
- describe('tn() method', () => {
226
- test('should format number with default locale', () => {
227
- const i18n = new TestI18n('en', 'en', 'index')
228
- const result = i18n.tn(1234.56)
229
- expect(result).toMatch(/1[,.]234[.,]56/)
230
- })
231
-
232
- test('should format number with custom options', () => {
233
- const i18n = new TestI18n('en', 'en', 'index')
234
- const result = i18n.tn(1234.56, { style: 'currency', currency: 'USD' })
235
- expect(result).toContain('1,234.56')
236
- })
237
-
238
- test('should use current locale for formatting', () => {
239
- const i18n = new TestI18n('ru', 'en', 'index')
240
- const result = i18n.tn(1234.56)
241
- // Russian locale uses different number formatting
242
- expect(result).toBeDefined()
243
- })
244
- })
245
-
246
- describe('td() method', () => {
247
- test('should format date with default locale', () => {
248
- const i18n = new TestI18n('en', 'en', 'index')
249
- const date = new Date('2024-01-15')
250
- const result = i18n.td(date)
251
- expect(result).toBeDefined()
252
- expect(result).not.toBe('Invalid Date')
253
- })
254
-
255
- test('should format date with custom options', () => {
256
- const i18n = new TestI18n('en', 'en', 'index')
257
- const date = new Date('2024-01-15')
258
- const result = i18n.td(date, { year: 'numeric', month: 'long', day: 'numeric' })
259
- expect(result).toContain('2024')
260
- expect(result).toContain('January')
261
- })
262
-
263
- test('should handle date as number (timestamp)', () => {
264
- const i18n = new TestI18n('en', 'en', 'index')
265
- const timestamp = new Date('2024-01-15').getTime()
266
- const result = i18n.td(timestamp)
267
- expect(result).toBeDefined()
268
- expect(result).not.toBe('Invalid Date')
269
- })
270
-
271
- test('should handle date as string', () => {
272
- const i18n = new TestI18n('en', 'en', 'index')
273
- const result = i18n.td('2024-01-15')
274
- expect(result).toBeDefined()
275
- expect(result).not.toBe('Invalid Date')
276
- })
277
- })
278
-
279
- describe('tdr() method', () => {
280
- test('should format relative time', () => {
281
- const i18n = new TestI18n('en', 'en', 'index')
282
- const yesterday = new Date(Date.now() - 86400000)
283
- const result = i18n.tdr(yesterday)
284
- expect(result).toBeDefined()
285
- expect(result).toMatch(/day|ago/i)
286
- })
287
-
288
- test('should format relative time with custom options', () => {
289
- const i18n = new TestI18n('en', 'en', 'index')
290
- const yesterday = new Date(Date.now() - 86400000)
291
- const result = i18n.tdr(yesterday, { numeric: 'always' })
292
- expect(result).toBeDefined()
293
- })
294
-
295
- test('should handle invalid date gracefully', () => {
296
- const i18n = new TestI18n('en', 'en', 'index')
297
- const invalidDate = new Date('invalid')
298
- const result = i18n.tdr(invalidDate)
299
- expect(result).toBeDefined()
300
- })
301
- })
302
-
303
- describe('has() method', () => {
304
- test('should return true when translation exists', async () => {
305
- const i18n = new TestI18n('en', 'en', 'index')
306
- const translations: Translations = { greeting: 'Hello' }
307
- i18n['helper'].loadTranslations('en', translations)
308
-
309
- expect(i18n.has('greeting')).toBe(true)
310
- })
311
-
312
- test('should return false when translation does not exist', () => {
313
- const i18n = new TestI18n('en', 'en', 'index')
314
- expect(i18n.has('missing.key')).toBe(false)
315
- })
316
-
317
- test('should check route-specific translation when routeName is provided', async () => {
318
- const i18n = new TestI18n('en', 'en', 'index')
319
- const routeTranslations: Translations = { title: 'Route Title' }
320
- await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
321
-
322
- expect(i18n.has('title', 'about')).toBe(true)
323
- expect(i18n.has('title', 'index')).toBe(false)
324
- })
325
-
326
- test('should use current route when routeName is not provided', async () => {
327
- const i18n = new TestI18n('en', 'en', 'about')
328
- const routeTranslations: Translations = { title: 'Route Title' }
329
- await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
330
-
331
- expect(i18n.has('title')).toBe(true)
332
- })
333
- })
334
-
335
- describe('clearCache() method', () => {
336
- test('should clear all translations from cache', async () => {
337
- const i18n = new TestI18n('en', 'en', 'index')
338
- const translations: Translations = { greeting: 'Hello' }
339
- i18n['helper'].loadTranslations('en', translations)
340
-
341
- expect(i18n.has('greeting')).toBe(true)
342
-
343
- i18n.clearCache()
344
-
345
- expect(i18n.has('greeting')).toBe(false)
346
- })
347
- })
348
-
349
- describe('loadTranslationsCore() method', () => {
350
- test('should load translations when merge is false', async () => {
351
- const i18n = new TestI18n('en', 'en', 'index')
352
- const translations: Translations = { greeting: 'Hello' }
353
-
354
- i18n['loadTranslationsCore']('en', translations, false)
355
- // Wait for async operation to complete
356
- await new Promise((resolve) => setTimeout(resolve, 0))
357
-
358
- expect(i18n.has('greeting')).toBe(true)
359
- })
360
-
361
- test('should merge translations when merge is true', async () => {
362
- const i18n = new TestI18n('en', 'en', 'index')
363
- const initial: Translations = { greeting: 'Hello' }
364
- const additional: Translations = { farewell: 'Goodbye' }
365
-
366
- await i18n['helper'].loadTranslations('en', initial)
367
- i18n['loadTranslationsCore']('en', additional, true)
368
- // Wait for async operation to complete
369
- await new Promise((resolve) => setTimeout(resolve, 0))
370
-
371
- expect(i18n.has('greeting')).toBe(true)
372
- expect(i18n.has('farewell')).toBe(true)
373
- })
374
- })
375
-
376
- describe('loadRouteTranslationsCore() method', () => {
377
- test('should load route translations when merge is false', async () => {
378
- const i18n = new TestI18n('en', 'en', 'index')
379
- const translations: Translations = { title: 'Page Title' }
380
-
381
- i18n['loadRouteTranslationsCore']('en', 'about', translations, false)
382
- // Wait for async operation to complete
383
- await new Promise((resolve) => setTimeout(resolve, 0))
384
-
385
- expect(i18n.has('title', 'about')).toBe(true)
386
- })
387
-
388
- test('should merge route translations when merge is true', async () => {
389
- const i18n = new TestI18n('en', 'en', 'index')
390
- const initial: Translations = { title: 'Page Title' }
391
- const additional: Translations = { description: 'Page Description' }
392
-
393
- await i18n['helper'].loadPageTranslations('en', 'about', initial)
394
- i18n['loadRouteTranslationsCore']('en', 'about', additional, true)
395
- // Wait for async operation to complete
396
- await new Promise((resolve) => setTimeout(resolve, 0))
397
-
398
- expect(i18n.has('title', 'about')).toBe(true)
399
- expect(i18n.has('description', 'about')).toBe(true)
400
- })
401
- })
402
-
403
- describe('Edge cases', () => {
404
- test('should handle nested translation keys', async () => {
405
- const i18n = new TestI18n('en', 'en', 'index')
406
- const translations: Translations = {
407
- nested: {
408
- deep: {
409
- key: 'Nested value',
410
- },
411
- },
412
- }
413
- i18n['helper'].loadTranslations('en', translations)
414
-
415
- expect(i18n.t('nested.deep.key')).toBe('Nested value')
416
- })
417
-
418
- test('should handle multiple params in interpolation', async () => {
419
- const i18n = new TestI18n('en', 'en', 'index')
420
- const translations: Translations = {
421
- message: 'Hello, {name}! You are {age} years old.',
422
- }
423
- i18n['helper'].loadTranslations('en', translations)
424
-
425
- expect(i18n.t('message', { name: 'John', age: 30 })).toBe('Hello, John! You are 30 years old.')
426
- })
427
-
428
- test('should handle null defaultValue', () => {
429
- const i18n = new TestI18n('en', 'en', 'index')
430
- expect(i18n.t('missing.key', undefined, null)).toBe('missing.key')
431
- })
432
-
433
- test('should handle empty string defaultValue', () => {
434
- const i18n = new TestI18n('en', 'en', 'index')
435
- // Empty string is falsy, so it will fall back to key (as per defaultValue || key logic)
436
- expect(i18n.t('missing.key', undefined, '')).toBe('missing.key')
437
- })
438
-
439
- test('should handle route change', async () => {
440
- const storage = { translations: new Map<string, Translations>() }
441
- const i18n = new TestI18n('en', 'en', 'index', { storage })
442
- const rootTranslations: Translations = { greeting: 'Hello' }
443
- // Packages (vue, node, etc.) merge root into pages automatically.
444
- // Core does not — so we simulate pre-merged data here.
445
- const routeTranslations: Translations = { greeting: 'Hello', title: 'About Page' }
446
-
447
- await i18n['helper'].loadTranslations('en', rootTranslations)
448
- await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
449
-
450
- // Verify translations are loaded
451
- expect(i18n.has('greeting')).toBe(true)
452
- expect(i18n.has('title', 'about')).toBe(true)
453
-
454
- // Check route-specific translation with explicit routeName
455
- const titleValue = i18n.t('title', undefined, undefined, 'about')
456
- expect(titleValue).toBe('About Page')
457
-
458
- // Change route and check
459
- i18n.setRoute('about')
460
- expect(i18n.t('title')).toBe('About Page')
461
- expect(i18n.t('greeting')).toBe('Hello')
462
- })
463
- })
464
- })
@@ -1,81 +0,0 @@
1
- import { interpolate, useTranslationHelper } from '../src'
2
-
3
- describe('Translation Helper', () => {
4
- const translations = {
5
- en: {
6
- greeting: 'Hello, {name}!',
7
- nested: {
8
- message: 'This is a nested message.',
9
- },
10
- },
11
- fr: {
12
- greeting: 'Bonjour, {name}!',
13
- },
14
- }
15
-
16
- test('interpolate function replaces placeholders correctly', () => {
17
- const result = interpolate('Hello, {name}!', { name: 'John' })
18
- expect(result).toBe('Hello, John!')
19
- })
20
-
21
- test('interpolate function handles missing placeholders gracefully', () => {
22
- const result = interpolate('Hello, {name}!', { age: 30 })
23
- expect(result).toBe('Hello, {name}!')
24
- })
25
-
26
- test('getTranslation fetches correct translation', () => {
27
- const helper = useTranslationHelper()
28
- helper.loadTranslations('en', translations.en)
29
-
30
- const translation = helper.getTranslation('en', 'index', 'greeting')
31
- expect(translation).toBe('Hello, {name}!')
32
- })
33
-
34
- test('getTranslation supports nested keys', () => {
35
- const helper = useTranslationHelper()
36
- helper.loadTranslations('en', translations.en)
37
-
38
- const translation = helper.getTranslation('en', 'index', 'nested.message')
39
- expect(translation).toBe('This is a nested message.')
40
- })
41
-
42
- test('getTranslation falls back when translation is missing', () => {
43
- const helper = useTranslationHelper()
44
- helper.loadTranslations('en', translations.en)
45
-
46
- const translation = helper.getTranslation('en', 'index', 'nonexistent.key')
47
- expect(translation).toBeNull()
48
- })
49
-
50
- test('loadPageTranslations correctly caches translations', async () => {
51
- const helper = useTranslationHelper()
52
- await helper.loadPageTranslations('fr', 'home', translations.fr)
53
-
54
- expect(helper.hasPageTranslation('fr', 'home')).toBe(true)
55
- expect(helper.getTranslation('fr', 'home', 'greeting')).toBe('Bonjour, {name}!')
56
- })
57
-
58
- test('mergeTranslation updates route translations', () => {
59
- const helper = useTranslationHelper()
60
- helper.loadPageTranslations('en', 'home', translations.en)
61
-
62
- helper.mergeTranslation('en', 'home', { newKey: 'New value' })
63
- expect(helper.getTranslation('en', 'home', 'newKey')).toBe('New value')
64
- })
65
-
66
- test('mergeTranslation with index routeName updates index translations', () => {
67
- const helper = useTranslationHelper()
68
- helper.loadTranslations('en', translations.en)
69
-
70
- helper.mergeTranslation('en', 'index', { newKey: 'New value' })
71
- expect(helper.getTranslation('en', 'index', 'newKey')).toBe('New value')
72
- })
73
-
74
- test('deepClone creates a deep copy of objects', () => {
75
- const original = { nested: { key: 'value' } }
76
- const cloned = JSON.parse(JSON.stringify(original))
77
-
78
- expect(cloned).toEqual(original)
79
- expect(cloned).not.toBe(original)
80
- })
81
- })
@@ -1,101 +0,0 @@
1
- import { FormatService } from '../src'
2
-
3
- describe('FormatService', () => {
4
- let formatService: FormatService
5
-
6
- beforeEach(() => {
7
- formatService = new FormatService()
8
- })
9
-
10
- describe('formatNumber', () => {
11
- test('should format number with default options', () => {
12
- const result = formatService.formatNumber(123456.789, 'en-US')
13
- expect(result).toBe('123,456.789')
14
- })
15
-
16
- test('should format number with custom options', () => {
17
- const result = formatService.formatNumber(123456.789, 'de-DE', {
18
- style: 'currency',
19
- currency: 'EUR',
20
- })
21
- expect(result).toBe('123.456,79 €')
22
- })
23
-
24
- test('should handle invalid locale by falling back to default formatting', () => {
25
- const result = formatService.formatNumber(123456.789, 'invalid-locale')
26
- expect(result).toBe('123,456.789') // Fallback to default formatting
27
- })
28
- })
29
-
30
- describe('formatDate', () => {
31
- test('should format date with default options', () => {
32
- const date = new Date('2023-10-05T12:34:56Z')
33
- const result = formatService.formatDate(date, 'en-US')
34
- expect(result).toBe('10/5/2023')
35
- })
36
-
37
- test('should format date with custom options', () => {
38
- const date = new Date('2023-10-05T12:34:56Z')
39
- const result = formatService.formatDate(date, 'de-DE', {
40
- year: 'numeric',
41
- month: 'long',
42
- day: 'numeric',
43
- })
44
- expect(result).toBe('5. Oktober 2023')
45
- })
46
-
47
- test('should handle invalid date by returning "Invalid Date"', () => {
48
- const result = formatService.formatDate('invalid-date', 'en-US')
49
- expect(result).toBe('Invalid Date')
50
- })
51
- })
52
-
53
- describe('formatRelativeTime', () => {
54
- test('should format relative time for seconds', () => {
55
- const now = new Date()
56
- const past = new Date(now.getTime() - 30 * 1000) // 30 seconds ago
57
- const result = formatService.formatRelativeTime(past, 'en-US')
58
- expect(result).toBe('30 seconds ago')
59
- })
60
-
61
- test('should format relative time for minutes', () => {
62
- const now = new Date()
63
- const past = new Date(now.getTime() - 5 * 60 * 1000) // 5 minutes ago
64
- const result = formatService.formatRelativeTime(past, 'en-US')
65
- expect(result).toBe('5 minutes ago')
66
- })
67
-
68
- test('should format relative time for hours', () => {
69
- const now = new Date()
70
- const past = new Date(now.getTime() - 2 * 60 * 60 * 1000) // 2 hours ago
71
- const result = formatService.formatRelativeTime(past, 'en-US')
72
- expect(result).toBe('2 hours ago')
73
- })
74
-
75
- test('should format relative time for days', () => {
76
- const now = new Date()
77
- const past = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000) // 3 days ago
78
- const result = formatService.formatRelativeTime(past, 'en-US')
79
- expect(result).toBe('3 days ago')
80
- })
81
-
82
- test('should format relative time for months', () => {
83
- const now = new Date()
84
- const past = new Date(now.getTime() - 60 * 24 * 60 * 60 * 1000) // ~2 months ago
85
- const result = formatService.formatRelativeTime(past, 'en-US')
86
- expect(result).toBe('2 months ago')
87
- })
88
-
89
- test('should format relative time for years', () => {
90
- const now = new Date()
91
- const past = new Date(now.getTime() - 365 * 24 * 60 * 60 * 1000) // 1 year ago
92
- const result = formatService.formatRelativeTime(past, 'en-US')
93
- expect(result).toBe('1 year ago')
94
- })
95
-
96
- test('should handle invalid date by returning "0 seconds ago"', () => {
97
- const result = formatService.formatRelativeTime('invalid-date', 'en-US')
98
- expect(result).toBe('in 0 seconds')
99
- })
100
- })
101
- })