UNPKG

2.32 kBPlain TextView Raw
1import {TranslateDefaultParser, TranslateParser} from '../public-api';
2
3describe('Parser', () => {
4 let parser: TranslateParser;
5
6 beforeEach(() => {
7 parser = new TranslateDefaultParser();
8 });
9
10 it('is defined', () => {
11 expect(TranslateParser).toBeDefined();
12
13 expect(parser instanceof TranslateParser).toBeTruthy();
14 });
15
16 it('should interpolate strings', () => {
17 expect(parser.interpolate("This is a {{ key }}", {key: "value"})).toEqual("This is a value");
18 });
19
20 it('should interpolate strings with falsy values', () => {
21 expect(parser.interpolate("This is a {{ key }}", {key: ""})).toEqual("This is a ");
22 expect(parser.interpolate("This is a {{ key }}", {key: 0})).toEqual("This is a 0");
23 });
24
25 it('should interpolate strings with object properties', () => {
26 expect(parser.interpolate("This is a {{ key1.key2 }}", {key1: {key2: "value2"}})).toEqual("This is a value2");
27 expect(parser.interpolate("This is a {{ key1.key2.key3 }}", {key1: {key2: {key3: "value3"}}})).toEqual("This is a value3");
28 });
29
30 it('should support interpolation functions', () => {
31 expect(parser.interpolate((v: string) => v.toUpperCase() + ' YOU!', 'bless')).toBe('BLESS YOU!');
32 });
33
34 it('should get the addressed value', () => {
35 expect(parser.getValue({key1: {key2: "value2"}}, 'key1.key2')).toEqual("value2");
36 expect(parser.getValue({key1: {key2: "value"}}, 'keyWrong.key2')).not.toBeDefined();
37 expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.key2.key3')).toEqual("value3");
38 expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.keyWrong.key3')).not.toBeDefined();
39 expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.key2.keyWrong')).not.toBeDefined();
40
41
42 expect(parser.getValue({'key1.key2': {key3: "value3"}}, 'key1.key2.key3')).toEqual("value3");
43 expect(parser.getValue({key1: {'key2.key3': "value3"}}, 'key1.key2.key3')).toEqual("value3");
44 expect(parser.getValue({'key1.key2.key3': "value3"}, 'key1.key2.key3')).toEqual("value3");
45 expect(parser.getValue({'key1.key2': {key3: "value3"}}, 'key1.key2.keyWrong')).not.toBeDefined();
46 expect(parser.getValue({
47 'key1': "value1",
48 'key1.key2': "value2"
49 }, 'key1.key2')).toEqual("value2");
50
51 });
52});