-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathlocale.test.js
More file actions
124 lines (109 loc) · 4.48 KB
/
locale.test.js
File metadata and controls
124 lines (109 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
// CAUTION: This test file relies on config values that may get changed in generated projects
import {determineTargetLocale, fetchTranslations, getTargetLocale} from './locale'
import {DEFAULT_LOCALE, SUPPORTED_LOCALES} from './test-utils'
jest.mock('cross-fetch', () => {
return async (url) => {
const matched = url.match(/translations\/compiled\/(.+)\.json/)
if (!matched) {
throw new Error('Not seeing the expected url for the translation file')
}
const locale = matched[1]
const json = await import(`../static/translations/compiled/${locale}.json`)
return {
ok: true,
json: () => Promise.resolve(json)
}
}
})
jest.mock('@salesforce/pwa-kit-react-sdk/utils/url', () => {
return {getAppOrigin: () => ''}
})
jest.mock('@salesforce/pwa-kit-react-sdk/ssr/universal/utils', () => {
return {getStaticAssetUrl: (url) => url}
})
const supportedLocales = SUPPORTED_LOCALES.map((locale) => locale.id)
const isMultiLocales = supportedLocales.length > 1
const nonSupportedLocale = 'nl-NL'
// Make sure this supported locale is not the default locale.
// Otherwise, our code would fall back to default and incorrectly pass the tests
const supportedLocale = isMultiLocales
? supportedLocales.find((locale) => locale !== DEFAULT_LOCALE)
: supportedLocales[0]
const testId1 = 'footer.link.privacy_policy'
const testId2 = 'account.accordion.button.my_account'
test('our assumptions before further testing', () => {
expect(supportedLocales).not.toContain(nonSupportedLocale)
if (isMultiLocales) {
// eslint-disable-next-line jest/no-conditional-expect
expect(DEFAULT_LOCALE).toBe('en-GB')
// eslint-disable-next-line jest/no-conditional-expect
expect(supportedLocale).not.toBe(DEFAULT_LOCALE)
}
})
describe('determineTargetLocale', () => {
test('default to fallback locale', () => {
const locale = determineTargetLocale([nonSupportedLocale], supportedLocales, DEFAULT_LOCALE)
expect(locale).toBe(DEFAULT_LOCALE)
})
test('matches one of the supported locales', () => {
const locale = determineTargetLocale([supportedLocale], supportedLocales, DEFAULT_LOCALE)
expect(locale).toBe(supportedLocale)
})
})
describe('fetchTranslations', () => {
// The following two tests expect the compiled translation files in app/static/translations/compiled to exist
test('loading the target locale', async () => {
const messages = await fetchTranslations(supportedLocale)
expect(messages[testId2]).toBeDefined()
})
})
describe('getTargetLocale', () => {
const originalEnv = {...process.env}
let windowSpy
const l10nConfig = {
defaultLocale: DEFAULT_LOCALE,
supportedLocales: SUPPORTED_LOCALES
}
beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get')
})
afterEach(() => {
// Reset
process.env = {...originalEnv}
windowSpy.mockRestore()
})
test('without getUserPreferredLocales parameter', () => {
const targetLocale = getTargetLocale({l10nConfig})
expect(targetLocale).toBe(DEFAULT_LOCALE)
})
test('with getUserPreferredLocales parameter', () => {
const locale = supportedLocale
if (isMultiLocales) {
// eslint-disable-next-line jest/no-conditional-expect
expect(locale).not.toBe(DEFAULT_LOCALE)
}
const targetLocale = getTargetLocale({
getUserPreferredLocales: () => [locale],
l10nConfig
})
expect(targetLocale).toBe(locale)
})
test('with pseudo locale', async () => {
process.env.USE_PSEUDOLOCALE = 'true'
// Simulate server side
windowSpy.mockImplementation(() => undefined)
const targetLocale = getTargetLocale({l10nConfig})
// We expect the compiled translation file in app/static/translations/compiled/en-XA to exist
const messages = await fetchTranslations(targetLocale)
// The app should still think its target locale is the default one
expect(targetLocale).toBe(DEFAULT_LOCALE)
// But the actual translation should be using the pseudo locale
expect(messages[testId1][1].value).toMatch(/Ƥřīṽȧȧƈẏ Ƥǿǿŀīƈẏ/)
})
})