]> BookStack Code Mirror - bookstack/blob - resources/js/services/__tests__/translations.test.ts
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / resources / js / services / __tests__ / translations.test.ts
1 import {Translator} from "../translations";
2
3
4 describe('Translations Service', () => {
5
6     let $trans: Translator;
7
8     beforeEach(() => {
9         $trans = new Translator();
10     });
11
12     describe('choice()', () => {
13
14         test('it pluralises as expected', () => {
15
16             const cases = [
17                 {
18                     translation: `cat`, count: 10000,
19                     expected: `cat`,
20                 },
21                 {
22                     translation: `cat|cats`, count: 1,
23                     expected: `cat`,
24                 },
25                 {
26                     translation: `cat|cats`, count: 0,
27                     expected: `cats`,
28                 },
29                 {
30                     translation: `cat|cats`, count: 2,
31                     expected: `cats`,
32                 },
33                 {
34                     translation: `{0} cat|[1,100] dog|[100,*] turtle`, count: 0,
35                     expected: `cat`,
36                 },
37                 {
38                     translation: `{0} cat|[1,100] dog|[100,*] turtle`, count: 40,
39                     expected: `dog`,
40                 },
41                 {
42                     translation: `{0} cat|[1,100] dog|[100,*] turtle`, count: 101,
43                     expected: `turtle`,
44                 },
45             ];
46
47             for (const testCase of cases) {
48                 const output = $trans.choice(testCase.translation, testCase.count, {});
49                 expect(output).toEqual(testCase.expected);
50             }
51         });
52
53         test('it replaces as expected', () => {
54             const caseA = $trans.choice(`{0} cat|[1,100] :count dog|[100,*] turtle`, 4, {count: '5'});
55             expect(caseA).toEqual('5 dog');
56
57             const caseB = $trans.choice(`an :a :b :c dinosaur|many`, 1, {a: 'orange', b: 'angry', c: 'big'});
58             expect(caseB).toEqual('an orange angry big dinosaur');
59         });
60
61         test('it provides count as a replacement by default', () => {
62             const caseA = $trans.choice(`:count cats|:count dogs`, 4);
63             expect(caseA).toEqual('4 dogs');
64         });
65
66         test('not provided replacements are left as-is', () => {
67             const caseA = $trans.choice(`An :a dog`, 5, {});
68             expect(caseA).toEqual('An :a dog');
69         });
70
71     });
72 });