/
UtilTest.js
160 lines (146 loc) · 4.29 KB
/
UtilTest.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
jest.disableAutomock()
import {
parseNPlurals,
prepareDocs,
prepareLocales,
prepareStats
} from '../../app/editor/utils/Util'
describe('parseNPluralsTest', () => {
it('can parse valid Plural-Forms string', () => {
// Valid plural forms from
// https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
expect(parseNPlurals('nplurals=2; plural=n == 1 ? 0 : 1;')).toEqual(2)
expect(parseNPlurals('nplurals=1; plural=0;')).toEqual(1)
expect(parseNPlurals('nplurals=2; plural=n != 1;')).toEqual(2)
expect(parseNPlurals('nplurals=2; plural=n>1;')).toEqual(2)
expect(parseNPlurals(
'nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2;'))
.toEqual(3)
expect(parseNPlurals(
'nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 ' +
': n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;')).toEqual(6)
})
it('returns undefined when plural string is null or empty', () => {
expect(parseNPlurals(undefined)).toBeUndefined()
expect(parseNPlurals(null)).toBeUndefined()
expect(parseNPlurals('')).toBeUndefined()
})
it('returns undefined for strings without valid nplurals', () => {
expect(parseNPlurals('nplurals=x; plural=y;')).toBeUndefined()
expect(parseNPlurals('not even a plural forms string')).toBeUndefined()
expect(parseNPlurals('mplurals=7; the mumber of plurals')).toBeUndefined()
})
})
describe('prepareLocalesTest', () => {
it('Can transform locales to the expected form', () => {
// Values taken from API response.
const unpreparedLocales =
[
{
'localeId': 'de',
'displayName': 'German',
'nativeName': 'Deutsch',
'enabled': true,
'enabledByDefault': true,
'pluralForms': 'nplurals=2; plural=(n != 1)'
}, {
'localeId': 'ja',
'displayName': 'Japanese',
'nativeName': '日本語',
'enabled': true,
'enabledByDefault': true,
'pluralForms': 'nplurals=1; plural=0'
}
]
const preparedLocales = {
de: {
id: 'de',
name: 'German',
nplurals: 2
},
ja: {
id: 'ja',
name: 'Japanese',
nplurals: 1
}
}
expect(prepareLocales(unpreparedLocales)).toEqual(preparedLocales)
})
})
describe('prepareStatsTest', () => {
it('Can can translate statistics to the expected form.', () => {
// Values taken from API response.
const unpreparedStats = [
{
'total': 4592,
'untranslated': 4592,
'needReview': 0,
'translated': 0,
'approved': 0,
'rejected': 0,
'fuzzy': 0,
'unit': 'WORD',
'locale': 'de',
'lastTranslated': null,
'translatedOnly': 0
}, {
'total': 495,
'untranslated': 460,
'needReview': 0,
'translated': 15,
'approved': 5,
'rejected': 0,
'fuzzy': 20,
'unit': 'MESSAGE',
'locale': 'de',
'lastTranslated': null,
'translatedOnly': 10
}
]
// just the MESSAGE stats
const preparedStats = {
'total': 495,
'untranslated': 460,
'approved': 5,
'rejected': 0,
'needswork': 20,
'translated': 10
}
expect(prepareStats(unpreparedStats)).toEqual(preparedStats)
})
})
describe('prepareDocsTest', () => {
it('Can handle a null document list', () => {
expect(prepareDocs([])).toEqual([])
})
it('Can transform a list of documents to the expected structure.', () => {
// Values taken from API response.
const unpreparedDocs = [
{
'name': 'template20161102.pot',
'contentType': 'application/x-gettext',
'lang': 'en-US',
'type': 'FILE',
'revision': 1
}, {
'name': 'flags/template20161102.pot',
'contentType': 'application/x-gettext',
'lang': 'en-US',
'type': 'FILE',
'revision': 1
}, {
'name': 'msgctxt/template20161102.pot',
'contentType': 'application/x-gettext',
'lang': 'en-US',
'type': 'FILE',
'revision': 1
}
]
const preparedDocs = [
'template20161102.pot',
'flags/template20161102.pot',
'msgctxt/template20161102.pot'
]
expect(prepareDocs(unpreparedDocs)).toEqual(preparedDocs)
})
})