-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
101 lines (88 loc) · 2.31 KB
/
test.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
'use strict'
var test = require('tape')
var Translate = require('./')
var translate = Translate({
id: 'en_US',
values: {
PLAIN_STRING: 'hello',
PLURALIZED_SIMPLE: [
'Here are {{ count }}',
'Here is {{ count }}',
'And larger: {{ count }}'
],
TEMPLATED: '{{foo}} {{ bar}} baz',
APPLE: [
'apples',
'apple',
'apples'
],
COMPLEX: [
'No {{item}} left.',
'One {{item}} left.',
'{{count}} {{item}} left.'
]
}
})
test('basic', t => {
t.equal(translate.id, 'en_US')
t.equal(translate('PLAIN_STRING'), 'hello')
t.end()
})
test('not found', t => {
t.throws(() => translate('NOT_FOUND'), /"NOT_FOUND" not found/)
t.end()
})
test('pluralized simple', t => {
t.throws(() => translate('PLURALIZED_SIMPLE', {}), /pluralized/)
t.throws(() => translate('PLURALIZED_SIMPLE', {count: 'not a number'}), /pluralized/)
t.equal(translate('PLURALIZED_SIMPLE', {count: 0}), 'Here are 0')
t.equal(translate('PLURALIZED_SIMPLE', {count: 1}), 'Here is 1')
t.equal(translate('PLURALIZED_SIMPLE', {count: 2}), 'And larger: 2')
t.equal(translate('PLURALIZED_SIMPLE', {count: 3}), 'And larger: 3')
t.equal(translate('PLURALIZED_SIMPLE', {count: 999}), 'And larger: 999')
t.equal(translate('PLURALIZED_SIMPLE', {count: 0}), 'Here are 0')
t.equal(translate('PLURALIZED_SIMPLE', {count: -1}), 'Here is -1')
t.equal(translate('PLURALIZED_SIMPLE', {count: -2}), 'And larger: -2')
t.equal(translate('PLURALIZED_SIMPLE', {count: -3}), 'And larger: -3')
t.end()
})
test('templates', t => {
t.equal(translate('TEMPLATED', {foo: 1, bar: 2}), '1 2 baz')
t.equal(translate('TEMPLATED', {bar: 2}), '{{foo}} 2 baz')
t.end()
})
test('complex', t => {
t.equal(
translate('COMPLEX', {
item: translate('APPLE', {count: 0}),
count: 0
}),
'No apples left.'
)
t.equal(
translate('COMPLEX', {
item: translate('APPLE', {count: 1}),
count: 1
}),
'One apple left.'
)
t.equal(
translate('COMPLEX', {
item: translate('APPLE', {count: 999}),
count: 999
}),
'999 apples left.'
)
t.end()
})
test('separate Translate.run function', t => {
t.equal(
Translate.run({
values: {
HELLO: 'hello {{world}}'
}
}, 'HELLO', {world: 'planet'}),
'hello planet'
)
t.end()
})