55 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66 */
77
8- import { whichLocaleToLoad , loadLocaleData , getLocaleConfig , getPreferredCurrency } from './locale'
8+ import {
9+ whichLocaleToLoad ,
10+ loadLocaleData ,
11+ getLocaleConfig ,
12+ getPreferredCurrency ,
13+ getSupportedLocalesIds
14+ } from './locale'
915
1016import { SUPPORTED_LOCALES , DEFAULT_LOCALE } from '../constants'
1117
18+ const supportedLocales = getSupportedLocalesIds ( )
1219const nonSupportedLocale = 'nl-NL'
13- const supportedLocale = SUPPORTED_LOCALES [ 0 ]
14- const testMessageId = 'login-redirect.message.welcome'
20+ // Make sure this supported locale is not the default locale.
21+ // Otherwise, our code would fall back to default and incorrectly pass the tests
22+ const supportedLocale = supportedLocales [ 1 ]
23+
24+ const testId1 = 'login-redirect.message.welcome'
25+ const testId2 = 'homepage.message.welcome'
1526
1627test ( 'our assumptions before further testing' , ( ) => {
17- expect ( SUPPORTED_LOCALES . includes ( nonSupportedLocale ) ) . toBe ( false )
28+ expect ( supportedLocales . includes ( nonSupportedLocale ) ) . toBe ( false )
1829 expect ( DEFAULT_LOCALE ) . toBe ( 'en-GB' )
30+ expect ( supportedLocale ) . not . toBe ( DEFAULT_LOCALE )
1931} )
2032
2133describe ( 'whichLocaleToLoad' , ( ) => {
2234 test ( 'default to fallback locale' , ( ) => {
23- const locale = whichLocaleToLoad ( [ nonSupportedLocale ] , SUPPORTED_LOCALES , DEFAULT_LOCALE )
35+ const locale = whichLocaleToLoad ( [ nonSupportedLocale ] , supportedLocales , DEFAULT_LOCALE )
2436 expect ( locale ) . toBe ( DEFAULT_LOCALE )
2537 } )
2638 test ( 'matches one of the supported locales' , ( ) => {
27- const locale = whichLocaleToLoad ( [ supportedLocale ] , SUPPORTED_LOCALES , DEFAULT_LOCALE )
28- expect ( locale ) . toBe ( supportedLocale . id )
29- } )
30- test ( 'case-insensitivity' , ( ) => {
31- const locale = whichLocaleToLoad (
32- [ supportedLocale . id . toUpperCase ( ) ] ,
33- SUPPORTED_LOCALES ,
34- DEFAULT_LOCALE
35- )
36- expect ( locale ) . toBe ( supportedLocale . id )
39+ const locale = whichLocaleToLoad ( [ supportedLocale ] , supportedLocales , DEFAULT_LOCALE )
40+ expect ( locale ) . toBe ( supportedLocale )
3741 } )
3842} )
3943
4044describe ( 'loadLocaleData' , ( ) => {
4145 test ( 'default to English as the fallback locale' , async ( ) => {
4246 const messages = await loadLocaleData ( nonSupportedLocale )
43- expect ( messages [ testMessageId ] [ 0 ] . value ) . toMatch ( / l o g i n r e d i r e c t / i)
47+ expect ( messages [ testId1 ] [ 0 ] . value ) . toMatch ( / l o g i n r e d i r e c t / i)
4448 } )
4549 test ( 'loading one of the supported locales' , async ( ) => {
4650 const messages = await loadLocaleData ( supportedLocale )
47- expect ( messages [ testMessageId ] ) . toBeDefined ( )
51+ expect ( messages [ testId2 ] ) . toBeDefined ( )
4852 } )
4953 test ( 'loading the pseudo locale' , async ( ) => {
5054 const messages = await loadLocaleData ( 'en-XB' )
51- expect ( messages [ testMessageId ] [ 0 ] . value ) . toMatch ( / ^ \[ ! ! Ļ ŏ ĝ ĝ ĝ í ń Ŕ è ḋ ḋ ḋ í ŕ è è è ć ṭ ! ! ] $ / )
55+ expect ( messages [ testId1 ] [ 0 ] . value ) . toMatch ( / ^ \[ ! ! Ļ ŏ ĝ ĝ ĝ í ń Ŕ è ḋ ḋ ḋ í ŕ è è è ć ṭ ! ! ] $ / )
5256 } )
5357 test ( 'handling a not-found translation file' , async ( ) => {
54- expect ( SUPPORTED_LOCALES [ 1 ] . id ) . not . toBe ( DEFAULT_LOCALE )
58+ expect ( supportedLocale ) . not . toBe ( DEFAULT_LOCALE )
5559
56- jest . mock ( `../translations/compiled/${ SUPPORTED_LOCALES [ 1 ] . id } .json` , ( ) => {
60+ jest . mock ( `../translations/compiled/${ supportedLocale } .json` , ( ) => {
5761 throw new Error ( )
5862 } )
5963
@@ -62,11 +66,11 @@ describe('loadLocaleData', () => {
6266 importDefaultLocale = true
6367 } )
6468
65- await loadLocaleData ( SUPPORTED_LOCALES [ 1 ] . id )
69+ await loadLocaleData ( supportedLocale )
6670 expect ( importDefaultLocale ) . toBe ( true )
6771
6872 // Reset
69- jest . unmock ( `../translations/compiled/${ SUPPORTED_LOCALES [ 1 ] . id } .json` )
73+ jest . unmock ( `../translations/compiled/${ supportedLocale } .json` )
7074 jest . unmock ( `../translations/compiled/${ DEFAULT_LOCALE } .json` )
7175 } )
7276} )
@@ -89,7 +93,7 @@ describe('getLocaleConfig', () => {
8993 expect ( config . app . targetLocale ) . toBe ( DEFAULT_LOCALE )
9094 } )
9195 test ( 'with getUserPreferredLocales parameter' , async ( ) => {
92- const locale = SUPPORTED_LOCALES [ 1 ] . id
96+ const locale = supportedLocale
9397 expect ( locale ) . not . toBe ( DEFAULT_LOCALE )
9498
9599 const config = await getLocaleConfig ( {
@@ -107,13 +111,13 @@ describe('getLocaleConfig', () => {
107111 // The app should still think its target locale is the default one
108112 expect ( config . app . targetLocale ) . toBe ( DEFAULT_LOCALE )
109113 // But the actual translation should be using the pseudo locale
110- expect ( config . messages [ testMessageId ] [ 0 ] . value ) . toMatch ( / ^ \[ ! ! Ļ ŏ ĝ ĝ ĝ í ń Ŕ è ḋ ḋ ḋ í ŕ è è è ć ṭ ! ! ] $ / )
114+ expect ( config . messages [ testId1 ] [ 0 ] . value ) . toMatch ( / ^ \[ ! ! Ļ ŏ ĝ ĝ ĝ í ń Ŕ è ḋ ḋ ḋ í ŕ è è è ć ṭ ! ! ] $ / )
111115 } )
112116} )
113117
114118describe ( 'getCurrency' , ( ) => {
115119 test ( 'returns the preferred currency for a supported locale' , ( ) => {
116- const currency = getPreferredCurrency ( supportedLocale . id )
120+ const currency = getPreferredCurrency ( SUPPORTED_LOCALES [ 0 ] . id )
117121 expect ( currency ) . toBe ( SUPPORTED_LOCALES [ 0 ] . preferredCurrency )
118122 } )
119123
0 commit comments