|
1 | | -import { beforeEach, describe, expect, test, vi } from 'vitest' |
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { mount } from '@vue/test-utils' |
2 | 4 |
|
3 | 5 | import { |
4 | 6 | formatDate, |
5 | 7 | formatDateTime, |
6 | 8 | formatTime, |
7 | 9 | } from '../../../src/common/formatters/date' |
8 | 10 |
|
9 | | -import { App, ComponentInternalInstance, getCurrentInstance } from 'vue' |
10 | 11 | import { createI18n } from 'vue-i18n' |
| 12 | +import { h, nextTick } from 'vue' |
11 | 13 |
|
| 14 | +describe('common/formatters/date', () => { |
| 15 | + describe('formatDate', () => { |
| 16 | + test('falls back to en-GB', () => { |
| 17 | + expect(formatDate(new Date('2024-01-01T12:30:00'))).toMatch(/01\/01\/2024/) |
| 18 | + }) |
12 | 19 |
|
13 | | -vi.mock('vue', async () => { |
14 | | - const actual = await vi.importActual<typeof import('vue')>('vue') |
15 | | - return { |
16 | | - ...actual, |
17 | | - getCurrentInstance: vi.fn(), |
18 | | - } |
19 | | -}) |
| 20 | + test('handles string dates', () => { |
| 21 | + expect(formatDate('2024-01-01')).toMatch(/01\/01\/2024/) |
| 22 | + }) |
20 | 23 |
|
21 | | -const mockVue3Instance = (locale: string | { value: string }) => ({ |
22 | | - proxy: { |
23 | | - $i18n: { locale }, |
24 | | - }, |
25 | | -}) |
| 24 | + test('should detect i18n locale from Vue instance', async () => { |
| 25 | + const i18n = createI18n({ |
| 26 | + legacy: false, |
| 27 | + locale: 'en-GB', |
| 28 | + fallbackLocale: 'en-GB', |
| 29 | + }) |
26 | 30 |
|
27 | | -describe('common/formatters/date', () => { |
28 | | - const mockDate = new Date('2024-01-01T12:30:00') |
| 31 | + const wrapper = mount({ |
| 32 | + setup: () => () => h('div', [formatDate(new Date('2024-01-01T12:30:00'))]), |
| 33 | + }, { |
| 34 | + global: { plugins: [i18n] }, |
| 35 | + }) |
29 | 36 |
|
30 | | - beforeEach(() => { |
31 | | - vi.clearAllMocks() |
32 | | - }) |
| 37 | + expect(wrapper.text()).toMatch('01/01/2024') |
33 | 38 |
|
| 39 | + i18n.global.locale.value = 'ru-RU' |
34 | 40 |
|
35 | | - test('should detect i18n locale from Vue instance', () => { |
36 | | - // Create proper i18n instance |
37 | | - const i18n = createI18n({ |
38 | | - legacy: false, |
39 | | - locale: 'en-GB', |
40 | | - fallbackLocale: 'ru-RU', |
41 | | - }) |
| 41 | + await nextTick() |
42 | 42 |
|
43 | | - // Create proper component instance mock |
44 | | - const mockInstance = { |
45 | | - appContext: { |
46 | | - app: { |
47 | | - config: { |
48 | | - globalProperties: { |
49 | | - $i18n: i18n.global, |
50 | | - }, |
51 | | - }, |
52 | | - } as unknown as App, |
53 | | - }, |
54 | | - proxy: { |
55 | | - $i18n: i18n.global, |
56 | | - }, |
57 | | - } as unknown as ComponentInternalInstance |
58 | | - |
59 | | - vi.mocked(getCurrentInstance).mockReturnValue(mockInstance as ComponentInternalInstance) |
60 | | - |
61 | | - i18n.global.locale.value = 'en-GB' |
62 | | - expect(formatDateTime(mockDate)).toMatch('01/01/2024, 12:30') |
63 | | - |
64 | | - i18n.global.locale.value = 'ru-RU' |
65 | | - expect(formatDateTime(mockDate)).toMatch('01.01.2024, 12:30') |
66 | | - |
67 | | - i18n.global.locale.value = 'es-ES' |
68 | | - expect(formatDateTime(mockDate)).toMatch('01/01/2024, 12:30') |
69 | | - }) |
| 43 | + expect(wrapper.text()).toMatch('01.01.2024') |
70 | 44 |
|
71 | | - test('should fallback to en-GB when no i18n instance exists', () => { |
72 | | - vi.mocked(getCurrentInstance).mockReturnValue(null) |
73 | | - |
74 | | - const result = formatDateTime(mockDate) |
75 | | - expect(result).toMatch('01/01/2024, 12:30') |
76 | | - }) |
| 45 | + i18n.global.locale.value = 'es-ES' |
77 | 46 |
|
78 | | - test('formatTime uses explicit locale', () => { |
79 | | - expect(formatTime(new Date('2024-01-01T12:30:00'))).toBe('12:30') |
| 47 | + await nextTick() |
| 48 | + |
| 49 | + expect(wrapper.text()).toMatch('01/01/2024') |
| 50 | + }) |
80 | 51 | }) |
81 | 52 |
|
82 | | - test('formatDate falls back to en-GB', () => { |
83 | | - expect(formatDate(new Date('2024-01-01T12:30:00'))).toMatch(/01\/01\/2024/) |
| 53 | + describe('formatTime', () => { |
| 54 | + test('uses explicit locale', () => { |
| 55 | + expect(formatTime(new Date('2024-01-01T12:30:00'))).toBe('12:30') |
| 56 | + }) |
84 | 57 | }) |
85 | 58 |
|
| 59 | + describe('formatDateTime', () => { |
| 60 | + test('falls back to en-GB when no i18n instance exists', () => { |
| 61 | + expect(formatDateTime(new Date('2024-01-01T12:30:00'))).toMatch('01/01/2024, 12:30') |
| 62 | + }) |
86 | 63 |
|
87 | | - test('formatDateTime combines date and time', () => { |
88 | | - mockVue3Instance('es-ES') |
89 | | - const result = formatDateTime(new Date('2024-01-01T12:30:00')) |
90 | | - expect(result).toMatch(/01\/01\/2024/) |
91 | | - expect(result).toMatch(/12:30/) |
92 | | - }) |
| 64 | + test('combines date and time', () => { |
| 65 | + const result = formatDateTime(new Date('2024-01-01T12:30:00')) |
| 66 | + |
| 67 | + expect(result).toMatch(/01\/01\/2024/) |
| 68 | + expect(result).toMatch(/12:30/) |
| 69 | + }) |
| 70 | + |
| 71 | + test('should detect i18n locale from Vue instance', async () => { |
| 72 | + const i18n = createI18n({ |
| 73 | + legacy: false, |
| 74 | + locale: 'en-GB', |
| 75 | + fallbackLocale: 'en-GB', |
| 76 | + }) |
| 77 | + |
| 78 | + const wrapper = mount({ |
| 79 | + setup: () => () => h('div', [formatDateTime(new Date('2024-01-01T12:30:00'))]), |
| 80 | + }, { |
| 81 | + global: { plugins: [i18n] }, |
| 82 | + }) |
| 83 | + |
| 84 | + expect(wrapper.text()).toMatch('01/01/2024, 12:30') |
93 | 85 |
|
94 | | - test('handles string dates', () => { |
95 | | - expect(formatDate('2024-01-01')).toMatch(/01\/01\/2024/) |
| 86 | + i18n.global.locale.value = 'ru-RU' |
| 87 | + |
| 88 | + await nextTick() |
| 89 | + |
| 90 | + expect(wrapper.text()).toMatch('01.01.2024, 12:30') |
| 91 | + |
| 92 | + i18n.global.locale.value = 'es-ES' |
| 93 | + |
| 94 | + await nextTick() |
| 95 | + |
| 96 | + expect(wrapper.text()).toMatch('01/01/2024, 12:30') |
| 97 | + }) |
96 | 98 | }) |
97 | 99 | }) |
0 commit comments