xref: /expo/apps/test-suite/tests/Localization.js (revision 190df5fb)
1import { Localization } from 'expo';
2import { Localization as DangerZoneLocalization } from 'expo/DangerZone';
3import i18n from 'i18n-js';
4
5const en = {
6  good: 'good',
7  morning: 'morning',
8};
9
10const fr = {
11  good: 'bien',
12  morning: 'matin',
13};
14
15const pl = {
16  good: 'dobry',
17  morning: 'rano',
18};
19
20export const name = 'Localization';
21
22export function test(t) {
23  t.describe(`Localization methods`, () => {
24    t.it('expect async to return locale', async () => {
25      function validateString(result) {
26        t.expect(result).toBeDefined();
27        t.expect(typeof result).toBe('string');
28        t.expect(result.length > 0).toBe(true);
29      }
30
31      function validateStringArray(result) {
32        t.expect(result).toBeDefined();
33        t.expect(Array.isArray(result)).toBe(true);
34        t.expect(result.length > 0).toBe(true);
35      }
36
37      const {
38        locale,
39        locales,
40        timezone,
41        isoCurrencyCodes,
42        country,
43        isRTL,
44      } = await Localization.getLocalizationAsync();
45
46      validateString(locale);
47      validateString(timezone);
48      validateString(country);
49
50      validateStringArray(isoCurrencyCodes);
51      validateStringArray(locales);
52      t.expect(locales[0]).toBe(Localization.locale);
53      t.expect(typeof isRTL).toBe('boolean');
54    });
55  });
56
57  t.describe(`Localization defines constants`, () => {
58    t.it('Gets the current device country', async () => {
59      const result = Localization.country;
60
61      t.expect(result).toBeDefined();
62      t.expect(typeof result).toBe('string');
63      t.expect(result.length > 0).toBe(true);
64    });
65    t.it('Gets the current locale', async () => {
66      const result = Localization.locale;
67
68      t.expect(result).toBeDefined();
69      t.expect(typeof result).toBe('string');
70      t.expect(result.length > 0).toBe(true);
71    });
72    t.it('Gets the preferred locales', async () => {
73      const result = Localization.locales;
74
75      t.expect(result).toBeDefined();
76      t.expect(Array.isArray(result)).toBe(true);
77      t.expect(result.length > 0).toBe(true);
78      t.expect(result[0]).toBe(Localization.locale);
79    });
80    t.it('Gets ISO currency codes', async () => {
81      const result = Localization.isoCurrencyCodes;
82      t.expect(result).toBeDefined();
83      t.expect(Array.isArray(result)).toBe(true);
84      t.expect(result.length > 0).toBe(true);
85      for (let iso of result) {
86        t.expect(typeof iso).toBe('string');
87        t.expect(iso.length > 0).toBe(true);
88      }
89    });
90    t.it('Gets the current timzezone', async () => {
91      const result = Localization.timezone;
92      t.expect(result).toBeDefined();
93      t.expect(typeof result).toBe('string');
94      t.expect(result.length > 0).toBe(true);
95      // Format: expect something like America/Los_Angeles or America/Chihuahua
96      t.expect(result.split('/').length > 1).toBe(true);
97    });
98
99    t.it('Gets the current layout direction (ltr only)', async () => {
100      const result = Localization.isRTL;
101      t.expect(result).toBeDefined();
102      t.expect(result).toBe(false);
103    });
104  });
105
106  t.describe(`Localization works with i18n-js`, () => {
107    i18n.locale = Localization.locale;
108    i18n.translations = { en, fr, pl };
109    i18n.missingTranslationPrefix = 'EE: ';
110    i18n.fallbacks = true;
111
112    t.it('expect language to match strings (en, pl, fr supported)', async () => {
113      const target = 'good';
114
115      i18n.locale = Localization.locale;
116
117      const expoPredictedLangTag = Localization.locale.split('-')[0];
118      const translation = i18n.translations[expoPredictedLangTag];
119
120      t.expect(translation[target]).toBe(i18n.t(target));
121    });
122  });
123
124  t.describe(`DangerZone.Localization`, () => {
125    const currentWarn = console.warn;
126
127    t.beforeEach(() => {
128      console.warn = currentWarn;
129    });
130
131    [
132      ['getCurrentDeviceCountryAsync', 'country'],
133      ['getCurrentLocaleAsync', 'locale'],
134      ['getCurrentTimeZoneAsync', 'timezone'],
135      ['getPreferredLocalesAsync', 'locales'],
136      ['getISOCurrencyCodesAsync', 'isoCurrencyCodes'],
137    ].forEach(obj => {
138      const [deprecated, replacement] = obj;
139
140      t.it(`${deprecated} is deprecated`, async () => {
141        const target = `Expo.DangerZone.Localization.${deprecated}() is deprecated. Use \`Expo.Localization.${replacement}\` instead.`;
142
143        let warning = null;
144        console.warn = (...props) => {
145          warning = props[0];
146          console.warn = currentWarn;
147        };
148
149        const value = await DangerZoneLocalization[deprecated]();
150
151        t.expect(warning).toBe(target);
152        t.expect(`${value}`.replace('_', '-')).toBe(`${Localization[replacement]}`);
153      });
154    });
155  });
156}
157