@@ -31,18 +31,32 @@ type NamespaceLocale = {
3131 languages : 'Languages'
3232}
3333
34+ const defaultSupportedLocales = [ 'en' , 'fr' , 'es' ]
35+
3436const wrapper =
3537 ( {
36- loadDateLocale = async ( locale : string ) =>
37- import ( `date-fns/locale/${ locale } /index` ) ,
38+ loadDateLocale = async ( locale : string ) => {
39+ if ( locale === 'en' ) {
40+ return ( await import ( 'date-fns/locale/en-GB' ) ) . enGB
41+ }
42+ if ( locale === 'fr' ) {
43+ return ( await import ( 'date-fns/locale/fr' ) ) . fr
44+ }
45+
46+ if ( locale === 'es' ) {
47+ return ( await import ( 'date-fns/locale/es' ) ) . es
48+ }
49+
50+ return ( await import ( `date-fns/locale/en-GB` ) ) . enGB
51+ } ,
3852 defaultLoad = async ( { locale } : { locale : string } ) =>
3953 import ( `./locales/${ locale } .json` ) ,
4054 defaultLocale = 'en' ,
4155 defaultTranslations = { } ,
4256 enableDebugKey = false ,
4357 enableDefaultLocale = false ,
4458 localeItemStorage = LOCALE_ITEM_STORAGE ,
45- supportedLocales = [ 'en' , 'fr' , 'es' ] ,
59+ supportedLocales = defaultSupportedLocales ,
4660 } = { } ) =>
4761 ( { children } : { children : ReactNode } ) => (
4862 < I18n
@@ -126,16 +140,16 @@ describe('i18n hook', () => {
126140 expect ( result . current . t ( 'title' ) ) . toEqual ( en . title )
127141 } )
128142
129- act ( ( ) => {
130- result . current . switchLocale ( 'fr' )
143+ await act ( async ( ) => {
144+ await result . current . switchLocale ( 'fr' )
131145 } )
132146
133147 await waitFor ( ( ) => {
134148 expect ( result . current . t ( 'title' ) ) . toEqual ( fr . title )
135149 } )
136150
137- act ( ( ) => {
138- result . current . switchLocale ( 'es' )
151+ await act ( async ( ) => {
152+ await result . current . switchLocale ( 'es' )
139153 } )
140154
141155 await waitFor ( ( ) => {
@@ -176,8 +190,8 @@ describe('i18n hook', () => {
176190 expect ( result . current . t ( 'lastName' ) ) . toEqual ( 'Last Name' )
177191 expect ( result . current . t ( 'languages' ) ) . toEqual ( 'Languages' )
178192
179- act ( ( ) => {
180- result . current . switchLocale ( 'fr' )
193+ await act ( async ( ) => {
194+ await result . current . switchLocale ( 'fr' )
181195 } )
182196
183197 await waitFor ( ( ) => {
@@ -221,8 +235,8 @@ describe('i18n hook', () => {
221235
222236 // current local will be 'en' based on navigator
223237 // await load of locales
224- act ( ( ) => {
225- result . current . switchLocale ( 'fr' )
238+ await act ( async ( ) => {
239+ await result . current . switchLocale ( 'fr' )
226240 } )
227241
228242 await waitFor ( ( ) => {
@@ -281,7 +295,36 @@ describe('i18n hook', () => {
281295
282296 await waitFor ( ( ) => {
283297 expect ( result . current . currentLocale ) . toEqual ( 'en' )
284- expect ( mockGetItem ) . toHaveBeenCalledTimes ( 2 )
298+ expect ( mockGetItem ) . toHaveBeenCalledTimes ( 1 )
299+ expect ( mockGetItem ) . toHaveBeenCalledWith ( LOCALE_ITEM_STORAGE )
300+ } )
301+ localStorageMock . mockRestore ( )
302+ } )
303+
304+ it ( 'should not set current locale from localStorage when this value is not supported' , async ( ) => {
305+ jest . spyOn ( global , 'navigator' , 'get' ) . mockReturnValueOnce ( {
306+ languages : [ 'bz' ] ,
307+ } as unknown as Navigator )
308+ const mockGetItem = jest . fn ( ) . mockImplementation ( ( ) => 're' )
309+ const mockSetItem = jest . fn ( )
310+ const localStorageMock = jest
311+ . spyOn ( global , 'localStorage' , 'get' )
312+ . mockReturnValue ( {
313+ getItem : mockGetItem ,
314+ setItem : mockSetItem ,
315+ clear : jest . fn ( ) ,
316+ } as unknown as Storage )
317+
318+ const { result } = renderHook ( ( ) => useI18n ( ) , {
319+ wrapper : wrapper ( {
320+ defaultLocale : 'en' ,
321+ supportedLocales : [ 'en' ] ,
322+ } ) ,
323+ } )
324+
325+ await waitFor ( ( ) => {
326+ expect ( result . current . currentLocale ) . toEqual ( 'en' )
327+ expect ( mockGetItem ) . toHaveBeenCalledTimes ( 1 )
285328 expect ( mockGetItem ) . toHaveBeenCalledWith ( LOCALE_ITEM_STORAGE )
286329 } )
287330 localStorageMock . mockRestore ( )
@@ -352,26 +395,26 @@ describe('i18n hook', () => {
352395 expect ( result . current . currentLocale ) . toEqual ( 'en' )
353396 expect ( localStorage . getItem ( LOCALE_ITEM_STORAGE ) ) . toBe ( null )
354397
355- act ( ( ) => {
356- result . current . switchLocale ( 'fr' )
398+ await act ( async ( ) => {
399+ await result . current . switchLocale ( 'fr' )
357400 } )
358401
359402 await waitFor ( ( ) => {
360403 expect ( result . current . currentLocale ) . toEqual ( 'fr' )
361404 } )
362405 expect ( localStorage . getItem ( LOCALE_ITEM_STORAGE ) ) . toBe ( 'fr' )
363406
364- act ( ( ) => {
365- result . current . switchLocale ( 'es' )
407+ await act ( async ( ) => {
408+ await result . current . switchLocale ( 'es' )
366409 } )
367410
368411 await waitFor ( ( ) => {
369412 expect ( result . current . currentLocale ) . toEqual ( 'es' )
370413 } )
371414 expect ( localStorage . getItem ( LOCALE_ITEM_STORAGE ) ) . toBe ( 'es' )
372415
373- act ( ( ) => {
374- result . current . switchLocale ( 'test' )
416+ await act ( async ( ) => {
417+ await result . current . switchLocale ( 'test' )
375418 } )
376419
377420 await waitFor ( ( ) => {
@@ -441,8 +484,8 @@ describe('i18n hook', () => {
441484 } ) ,
442485 ) . toEqual ( '$2.00' )
443486
444- act ( ( ) => {
445- result . current . switchLocale ( 'fr' )
487+ await act ( async ( ) => {
488+ await result . current . switchLocale ( 'fr' )
446489 } )
447490
448491 // https://stackoverflow.com/questions/58769806/identical-strings-not-matching-in-jest
@@ -491,8 +534,8 @@ describe('i18n hook', () => {
491534 } ) ,
492535 ) . toEqual ( 'Motorcycle Bus Car' )
493536
494- act ( ( ) => {
495- result . current . switchLocale ( 'fr' )
537+ await act ( async ( ) => {
538+ await result . current . switchLocale ( 'fr' )
496539 } )
497540
498541 await waitFor ( ( ) => {
@@ -565,8 +608,8 @@ describe('i18n hook', () => {
565608 } ) ,
566609 ) . toEqual ( '12/17/1995' )
567610
568- act ( ( ) => {
569- result . current . switchLocale ( 'fr' )
611+ await act ( async ( ) => {
612+ await result . current . switchLocale ( 'fr' )
570613 } )
571614
572615 await waitFor ( ( ) => {
@@ -602,11 +645,12 @@ describe('i18n hook', () => {
602645
603646 expect ( result . current . relativeTime ( date ) ) . toEqual ( 'over 20 years ago' )
604647
605- act ( ( ) => {
606- result . current . switchLocale ( 'fr' )
648+ await act ( async ( ) => {
649+ await result . current . switchLocale ( 'fr' )
607650 } )
608651
609652 await waitFor ( ( ) => {
653+ expect ( result . current . dateFnsLocale . code ) . toBe ( 'fr' )
610654 expect ( result . current . relativeTime ( date ) ) . toEqual ( 'il y a plus de 20 ans' )
611655 } )
612656 } )
@@ -621,8 +665,8 @@ describe('i18n hook', () => {
621665 const date = new Date ( 'September 13, 2011 15:15:00' )
622666
623667 expect ( result . current . relativeTimeStrict ( date ) ) . toEqual ( '3499 days ago' )
624- act ( ( ) => {
625- result . current . switchLocale ( 'fr' )
668+ await act ( async ( ) => {
669+ await result . current . switchLocale ( 'fr' )
626670 } )
627671
628672 await waitFor ( ( ) => {
@@ -642,8 +686,8 @@ describe('i18n hook', () => {
642686 expect (
643687 result . current . formatUnit ( 12 , { short : false , unit : 'byte' } ) ,
644688 ) . toEqual ( '12 bytes' )
645- act ( ( ) => {
646- result . current . switchLocale ( 'fr' )
689+ await act ( async ( ) => {
690+ await result . current . switchLocale ( 'fr' )
647691 } )
648692
649693 await waitFor ( ( ) => {
@@ -663,8 +707,8 @@ describe('i18n hook', () => {
663707 expect (
664708 result . current . formatDate ( new Date ( 2020 , 1 , 13 , 16 , 28 ) , 'numericHour' ) ,
665709 ) . toEqual ( '2020-02-13 4:28 PM' )
666- act ( ( ) => {
667- result . current . switchLocale ( 'fr' )
710+ await act ( async ( ) => {
711+ await result . current . switchLocale ( 'fr' )
668712 } )
669713
670714 await waitFor ( ( ) => {
@@ -681,10 +725,10 @@ describe('i18n hook', () => {
681725 supportedLocales : [ 'test' ] ,
682726 } ) ,
683727 } )
684- expect ( result . current . dateFnsLocale ) . toBe ( undefined )
728+ expect ( result . current . dateFnsLocale ) . toMatchObject ( { code : 'en-GB' } )
685729
686730 await waitFor ( ( ) => {
687- expect ( result . current . dateFnsLocale ? .code ) . toEqual ( 'en-GB' )
731+ expect ( result . current . dateFnsLocale . code ) . toEqual ( 'en-GB' )
688732 } )
689733 } )
690734
0 commit comments