-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalizeText.test.js
More file actions
138 lines (123 loc) · 4.9 KB
/
localizeText.test.js
File metadata and controls
138 lines (123 loc) · 4.9 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { assert } from './test-utils/deps-node.js';
import { resetPreferredLocales, getPreferredLocales, getSupportedLocales, localizeText, setPreferredLocales, validateLocalizableText } from '../ts/localizeText.ts';
describe('[LOCX] Localization', () => {
afterEach(() => {
// make sure locales are set back to default after each test
resetPreferredLocales();
});
describe('[LOVX] validateLocalizableText', () => {
it('[LOVA] should validate correct localizable text', () => {
const text = { en: 'Hello', fr: 'Bonjour' };
const result = validateLocalizableText('testKey', text);
assert.deepEqual(result, text);
});
it('[LOVB] should throw error if en is missing', () => {
try {
validateLocalizableText('testKey', { fr: 'Bonjour' });
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'Missing or invalid localizable text for testKey');
}
});
it('[LOVC] should throw error if en is not a string', () => {
try {
validateLocalizableText('testKey', { en: 123 });
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'Missing or invalid localizable text for testKey');
}
});
it('[LOVD] should throw error if optional language is not a string', () => {
try {
validateLocalizableText('testKey', { en: 'Hello', fr: 123 });
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'Missing or invalid localizable text for testKey languagecode: fr');
}
});
it('[LOVE] should throw error if es is not a string', () => {
try {
validateLocalizableText('testKey', { en: 'Hello', es: { nested: 'object' } });
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'Missing or invalid localizable text for testKey languagecode: es');
}
});
});
describe('[LOSX] Localization settings', () => {
it('[LOSG] getSupportedLocales', () => {
const defaultLocales = getSupportedLocales();
assert.deepEqual(defaultLocales, ['en', 'fr', 'es']);
});
it('[LOSS] setPreferredLocales, resetPrefferedLocales', () => {
const defaultLocales = getSupportedLocales();
const text = {
en: 'Hello',
fr: 'Bonjour'
};
setPreferredLocales(['en']);
assert.equal(localizeText(text), text.en);
setPreferredLocales(['fr', 'es']);
assert.equal(localizeText(text), text.fr);
const prefferedLocales = getPreferredLocales();
assert.deepEqual(prefferedLocales, ['fr', 'es', 'en']);
resetPreferredLocales();
assert.deepEqual(getPreferredLocales(), defaultLocales);
});
it('[LOSE] setPreferredLocales throws error if language code unsuported', () => {
try {
setPreferredLocales(['ex', 'en', 'fr', 'ut']);
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'locales "ex, ut" are not supported');
}
});
it('[LOSA] setPreferredLocales throws error if not array', () => {
try {
setPreferredLocales('en');
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'setPreferredLocales takes an array of language codes');
}
});
});
// --- item localization
describe('[LOLX] item localization', () => {
it('[LOLN] localizable null items return null', () => {
const nullRes = localizeText(null);
assert.equal(nullRes, null);
});
it('[LOLE] localizable should return english translation if none other found', () => {
setPreferredLocales(['fr', 'es']);
const text = {
en: 'Hello'
};
const res = localizeText(text);
assert.equal(res, 'Hello');
});
it('[LOLT] localizable items must have an english translation, even if default language is not english', () => {
try {
const text = {
es: 'Ola',
fr: 'Bonjour'
};
setPreferredLocales(['fr']);
localizeText(text);
throw new Error('Should throw error');
} catch (e) {
assert.equal(e.message, 'textItems must have an english translation');
}
});
it('[LOLY] empty string `en` is a valid translation (author chose "no text")', () => {
// Regression for S-2026-05-26-E: previously `!textItem.en` swallowed
// empty strings and threw "textItems must have an english translation",
// breaking invite peeks against Chat presets that wrote `consent: { en: "" }`.
assert.equal(localizeText({ en: '' }), '');
// Less-preferred locale set to `""` should also fall through to en.
setPreferredLocales(['fr']);
assert.equal(localizeText({ en: 'Hello', fr: '' }), '');
assert.equal(localizeText({ en: 'Hello', fr: null }), 'Hello');
assert.equal(localizeText({ en: 'Hello', fr: undefined }), 'Hello');
});
});
});