-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.ts
134 lines (123 loc) · 3.63 KB
/
index.test.ts
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
import gettextFactory from './index';
const multipleLanguages = {
'en-US': {
msg1: 'This is a American English message',
},
'fr-FR': {
msg1: 'This is a France French message',
},
'es-ES': {
msg1: 'This is a Spanish message',
},
'sv-SE': {
msg1: 'This is a Swedish message',
},
'nl-NL': {
msg1: 'This is a Dutch message',
},
'de-DE': {
msg1: 'This is a German message',
},
'it-IT': {
msg1: 'This is a Italian message',
},
'zh-CN': {
msg1: 'This is a Simplified Chinese message',
},
'zh-TW': {
msg1: 'This is a Traditional Chinese message',
},
'pt-BR': {
msg1: 'This is a Portugese (Brazillian) message',
},
'id-ID': {
msg1: 'This is a Indonesian (Bahasa) message',
},
'ro-RO': {
msg1: 'This is a Romanian message',
},
'ru-RU': {
msg1: 'This is a Russian message',
},
'pl-PL': {
msg1: 'This is a Polish message',
},
'cs-CZ': {
msg1: 'This is a Czech message',
},
'nb-NO': {
msg1: 'This is a Norwegian (Bokmål) message',
},
'hi-IN': {
msg1: 'This is a Indian (Hindi) message',
},
'da-DK': {
msg1: 'This is a Danish message',
},
};
const defaultLanguage = {
'en-US': {
msg1: 'This is a American English message',
},
};
describe('gettext', () => {
it('returns the message for the specified locale if it exists', () =>
expect(gettextFactory(multipleLanguages, 'fr-FR')('msg1')).toBe(
'This is a France French message',
));
it('returns a message for a matching language even if the locale differs', () =>
expect(gettextFactory(multipleLanguages, 'fr-CA')('msg1')).toBe(
'This is a France French message',
));
it('looks up the message in the fallback language if it cannot be found for the given language', () =>
expect(gettextFactory(defaultLanguage, 'fr-FR', 'en-US')('msg1')).toBe(
'This is a American English message',
));
it('returns msgid if the language table is empty', () => {
expect(gettextFactory({}, 'en-US')('msg1')).toBe('msg1');
});
it('returns msgid if the msgid does not exist in the specified or default language', () => {
expect(gettextFactory(multipleLanguages, 'is-DA', 'is-DA')('msg1')).toBe(
'msg1',
);
});
it('uses stringified msgid for lookups', () => {
expect(gettextFactory({ 'en-US': { '[object Object]': 'foo' } })({})).toBe(
'foo',
);
});
it('returns stringified msgid as the fallback', () => {
expect(gettextFactory({}, 'en-US')({})).toBe('[object Object]');
});
it('defaults to looking up strings in en-US', () => {
expect(gettextFactory(defaultLanguage)('msg1')).toBe(
'This is a American English message',
);
});
it.each([
['en-SE', 'sv-SE', 'Swedish'],
['en-NL', 'nl-NL', 'Dutch'],
['zh-Hant', 'zh-TW', 'Traditional Chinese'],
['zh-Hans', 'zh-CN', 'Simplified Chinese'],
['en-GB', 'en-US', 'American English'],
['fr-CA', 'fr-FR', 'France French'],
['es-PY', 'es-ES', 'Spanish'],
['de-GSW', 'de-DE', 'German'],
['it-SC', 'it-IT', 'Italian'],
['pt-PT', 'pt-BR', 'Portugese (Brazillian)'],
['pt-BR', 'pt-BR', 'Portugese (Brazillian)'],
['id-ID', 'id-ID', 'Indonesian (Bahasa)'],
['ro-RO', 'ro-RO', 'Romanian'],
['ru-BY', 'ru-RU', 'Russian'],
['ru-RU', 'ru-RU', 'Russian'],
['pl-PL', 'pl-PL', 'Polish'],
['cs-CZ', 'cs-CZ', 'Czech'],
['en-NO', 'nb-NO', 'Norwegian (Bokmål)'],
['en-IN', 'hi-IN', 'Indian (Hindi)'],
['da-DK', 'da-DK', 'Danish'],
])('maps %s to %s', (from, to, expectedMessage) =>
expect(gettextFactory(multipleLanguages, from)('msg1')).toBe(
`This is a ${expectedMessage} message`,
),
);
});