33 */
44import { currentLocale , LOCALE_STORAGE_KEY , setLocale } from '@/i18n' ;
55import { followStoredLocale } from '../followStoredLocale' ;
6+ import * as catalog from '../messageCatalog.node' ;
67
78const storageEvent = ( key : string | null , newValue : string | null ) =>
89 new StorageEvent ( 'storage' , { key, newValue } ) ;
910
11+ const flush = ( ) => new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) ) ;
12+
1013describe ( 'followStoredLocale' , ( ) => {
1114 afterEach ( async ( ) => {
15+ jest . restoreAllMocks ( ) ;
1216 await setLocale ( 'en' ) ;
17+ localStorage . clear ( ) ;
1318 } ) ;
1419
1520 it ( 'switches to the locale written by another window' , async ( ) => {
@@ -35,4 +40,61 @@ describe('followStoredLocale', () => {
3540 await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
3641 expect ( currentLocale ( ) ) . toBe ( 'en' ) ;
3742 } ) ;
43+
44+ it ( 'does not write the followed locale back to storage' , async ( ) => {
45+ const stop = followStoredLocale ( ) ;
46+ const setItem = jest . spyOn ( Storage . prototype , 'setItem' ) ;
47+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'ru' ) ) ;
48+ await flush ( ) ;
49+ expect ( currentLocale ( ) ) . toBe ( 'ru' ) ;
50+ expect ( setItem ) . not . toHaveBeenCalled ( ) ;
51+ stop ( ) ;
52+ } ) ;
53+
54+ it ( 'normalizes regional tags and ignores unsupported or unchanged values' , async ( ) => {
55+ const stop = followStoredLocale ( ) ;
56+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'ru-RU' ) ) ;
57+ await flush ( ) ;
58+ expect ( currentLocale ( ) ) . toBe ( 'ru' ) ;
59+ const setItem = jest . spyOn ( Storage . prototype , 'setItem' ) ;
60+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'xx' ) ) ;
61+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'ru' ) ) ;
62+ await flush ( ) ;
63+ expect ( currentLocale ( ) ) . toBe ( 'ru' ) ;
64+ expect ( setItem ) . not . toHaveBeenCalled ( ) ;
65+ stop ( ) ;
66+ } ) ;
67+
68+ it ( 'keeps the newest locale when an older load resolves later' , async ( ) => {
69+ await setLocale ( 'ru' ) ;
70+ const stop = followStoredLocale ( ) ;
71+ let release : ( value : any ) => void = ( ) => undefined ;
72+ const realLoad = catalog . loadMessages ;
73+ jest
74+ . spyOn ( catalog , 'loadMessages' )
75+ . mockImplementationOnce (
76+ ( ) => new Promise ( ( resolve ) => ( release = resolve ) ) as any ,
77+ ) ;
78+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'ja' ) ) ;
79+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'en' ) ) ;
80+ await flush ( ) ;
81+ expect ( currentLocale ( ) ) . toBe ( 'en' ) ;
82+ release ( await realLoad ( 'ja' ) ) ;
83+ await flush ( ) ;
84+ expect ( currentLocale ( ) ) . toBe ( 'en' ) ;
85+ stop ( ) ;
86+ } ) ;
87+
88+ it ( 'swallows a failed catalog load' , async ( ) => {
89+ const stop = followStoredLocale ( ) ;
90+ const unhandled = jest . fn ( ) ;
91+ process . on ( 'unhandledRejection' , unhandled ) ;
92+ jest . spyOn ( catalog , 'loadMessages' ) . mockRejectedValueOnce ( new Error ( 'x' ) ) ;
93+ window . dispatchEvent ( storageEvent ( LOCALE_STORAGE_KEY , 'ko' ) ) ;
94+ await flush ( ) ;
95+ process . off ( 'unhandledRejection' , unhandled ) ;
96+ expect ( unhandled ) . not . toHaveBeenCalled ( ) ;
97+ expect ( currentLocale ( ) ) . toBe ( 'en' ) ;
98+ stop ( ) ;
99+ } ) ;
38100} ) ;
0 commit comments