@@ -2,63 +2,77 @@ import { beforeEach, describe, expect, test, vi } from 'vitest'
22
33import {
44 formatDate ,
5- formatDateLat ,
6- formatDateRu ,
75 formatDateTime ,
86 formatTime ,
97} from '../../../src/common/formatters/date'
108
11- import { ComponentInternalInstance , getCurrentInstance } from 'vue'
12- import { createI18n , I18n } from 'vue-i18n'
9+ import { App , ComponentInternalInstance , getCurrentInstance } from 'vue'
10+ import { createI18n } from 'vue-i18n'
1311
1412
15- vi . mock ( 'vue' , ( ) => ( {
16- getCurrentInstance : vi . fn ( ) ,
17- } ) )
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+ } )
1820
19- // Mock Vue instance and window for different scenarios
2021const mockVue3Instance = ( locale : string | { value : string } ) => ( {
2122 proxy : {
2223 $i18n : { locale } ,
2324 } ,
2425} )
2526
2627describe ( 'common/formatters/date' , ( ) => {
28+ const mockDate = new Date ( '2024-01-01T12:30:00' )
29+
2730 beforeEach ( ( ) => {
28- vi . mocked ( getCurrentInstance ) . mockReturnValue ( null )
31+ vi . clearAllMocks ( )
2932 } )
3033
3134
32- test ( 'formatDateTime with vue-i18n instance and locale detection' , ( ) => {
33- const i18n : I18n = createI18n ( {
35+ test ( 'should detect i18n locale from Vue instance' , ( ) => {
36+ // Create proper i18n instance
37+ const i18n = createI18n ( {
38+ legacy : false ,
3439 locale : 'en-GB' ,
3540 fallbackLocale : 'ru-RU' ,
36- messages : { } ,
3741 } )
3842
43+ // Create proper component instance mock
3944 const mockInstance = {
45+ appContext : {
46+ app : {
47+ config : {
48+ globalProperties : {
49+ $i18n : i18n . global ,
50+ } ,
51+ } ,
52+ } as unknown as App ,
53+ } ,
4054 proxy : {
4155 $i18n : i18n . global ,
4256 } ,
43- }
57+ } as unknown as ComponentInternalInstance
4458
4559 vi . mocked ( getCurrentInstance ) . mockReturnValue ( mockInstance as ComponentInternalInstance )
4660
47- const result = formatDateTime ( new Date ( '2024-01-01T12:30:00' ) )
48- expect ( result ) . toMatch ( / 0 1 \/ 0 1 \/ 2 0 2 4 / ) // en-GB format
49- expect ( result ) . toMatch ( / 1 2 : 3 0 / )
61+ i18n . global . locale . value = 'en-GB'
62+ expect ( formatDateTime ( mockDate ) ) . toMatch ( '01/01/2024, 12:30' )
5063
51- // Verify vue-i18n is detected
52- expect ( mockInstance . proxy . $i18n . locale ) . toBe ( 'en-GB' )
53- expect ( mockInstance . proxy . $i18n . fallbackLocale ) . toBe ( 'ru-RU' )
54- } )
64+ i18n . global . locale . value = 'ru-RU'
65+ expect ( formatDateTime ( mockDate ) ) . toMatch ( '01.01.2024, 12:30' )
5566
56- test ( 'formatDateLat formats with en-GB style' , ( ) => {
57- expect ( formatDateLat ( new Date ( '2024-01-01T12:30:00' ) ) ) . toBe ( '01/01/2024' )
67+ i18n . global . locale . value = 'es-ES'
68+ expect ( formatDateTime ( mockDate ) ) . toMatch ( '01/01/2024, 12:30 ' )
5869 } )
5970
60- test ( 'formatDateRu formats with ru-RU style' , ( ) => {
61- expect ( formatDateRu ( new Date ( '2024-01-01T12:30:00' ) ) ) . toBe ( '01.01.2024' )
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' )
6276 } )
6377
6478 test ( 'formatTime uses explicit locale' , ( ) => {
@@ -69,9 +83,6 @@ describe('common/formatters/date', () => {
6983 expect ( formatDate ( new Date ( '2024-01-01T12:30:00' ) ) ) . toMatch ( / 0 1 \/ 0 1 \/ 2 0 2 4 / )
7084 } )
7185
72- test ( 'formatDate respects explicit locale' , ( ) => {
73- expect ( formatDate ( new Date ( '2024-01-01T12:30:00' ) , 'ru-RU' ) ) . toBe ( '01.01.2024' )
74- } )
7586
7687 test ( 'formatDateTime combines date and time' , ( ) => {
7788 mockVue3Instance ( 'es-ES' )
0 commit comments