/
buildMessageBlocks.spec.js
167 lines (142 loc) · 3.41 KB
/
buildMessageBlocks.spec.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
161
162
163
164
165
166
167
import {expect} from 'chai';
import {range} from 'ramda'
import {
buildMsgtr,
buildMsgtrs,
getNumPlurals,
formatMessageBlock,
} from '../src/utils/buildMessageBlocks';
import buildMessageBlocks from '../src/utils/buildMessageBlocks';
describe('buildMessageBlocks', () => {
it('builds a message block for a po file', () => {
let translations = [
{
text: 'hey',
path: '/here',
loc: {
line: 1,
column: 5
}
},
{
text: 'thank you',
path: '/here',
loc: {
line: 1,
column: 5
}
},
{
text: 'how are you',
path: '/here',
loc: {
line: 1,
column: 5
}
}
]
const expected = [
'#: /here 1:5\n',
'msgid "hey"\n',
'msgtr ""\n',
"\n",
'#: /here 1:5\n',
'msgid "thank you"\n',
'msgtr ""\n',
"\n",
'#: /here 1:5\n',
'msgid "how are you"\n',
'msgtr ""\n\n'
]
const formatted = buildMessageBlocks(translations);
expect(formatted).to.be.deep.equal(expected.join(''));
});
it('takes into account plural forms', () => {
let translations = [
{
text: 'hey',
path: '/here',
loc: {
line: 1,
column: 5
}
},
{
text: '%d views',
path: '/here',
loc: {
line: 1,
column: 5
}
}
]
const expected = [
'#: /here 1:5\n',
'msgid "hey"\n',
'msgtr ""\n',
"\n",
'#: /here 1:5\n',
'msgid "%d views"\n',
'msgtr[0] ""\n',
'msgtr[1] ""\n\n'
]
const formatted = buildMessageBlocks(translations);
expect(formatted).to.be.deep.equal(expected.join(''));
});
describe('buildMsgtr', () => {
it('builds a msgstr in a po file', () => {
let ouput = ['msgtr[0] ""\n', 'msgtr[1] ""\n'];
expect(buildMsgtr([0,1])).to.be.deep.equal(ouput);
});
});
describe('buildMsgtrs', () => {
it('builds an array of msgstr\'s given a range', () => {
let ouput = ['msgtr[0] ""\n', 'msgtr[1] ""\n', 'msgtr[2] ""\n', 'msgtr[3] ""\n'];
expect(buildMsgtrs(4)).to.be.deep.equal(ouput.join(''));
});
});
describe('getNumPlurals', () => {
it('determines the number of plural forms given a config', () => {
let config = 'nplurals=2; plural=(n != 1);'
expect(getNumPlurals(config)).to.be.equal(2);
});
});
describe('formatMessageBlock', () => {
it('takes an object, formats it, adds to and returns accumulator', () => {
let translation = {
text: 'hello',
path: '/here',
loc: {
line: 1,
column: 5
}
}
const expected = [
'#: /here 1:5',
'msgid "hello"',
'msgtr ""',
'\n'
]
const formatted = formatMessageBlock('', translation);
expect(formatted).to.be.equal(expected.join('\n'));
});
it('formats plural text properly', () => {
let translation = {
text: '%d views',
path: '/here',
loc: {
line: 1,
column: 5
}
}
const expected = [
'#: /here 1:5',
'msgid "%d views"',
'msgtr[0] ""',
'msgtr[1] ""\n\n'
]
const formatted = formatMessageBlock('', translation);
expect(formatted).to.be.equal(expected.join('\n'));
});
});
});