-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
index.spec.ts
69 lines (58 loc) · 2.64 KB
/
index.spec.ts
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
import { getFullUrl, setByDotNotation, truncateText } from '.';
describe('utils', () => {
it('should correctly set value by dot notation', () => {
const obj: any = {};
// set to empty path
setByDotNotation(obj, 'a.b', 3);
expect(obj).toEqual({ a: { b: 3 }});
// set to existing object in path
setByDotNotation(obj, 'a.c', 4);
expect(obj).toEqual({ a: { b: 3, c: 4 }});
// set to new array
setByDotNotation(obj, 'a.d.1', 5);
expect(obj).toEqual({ a: { b: 3, c: 4, d: [undefined, 5] }})
// set to existing array
setByDotNotation(obj, 'a.d.0', 6);
expect(obj).toEqual({ a: { b: 3, c: 4, d: [6, 5] }})
// set existing value
setByDotNotation(obj, 'a.c', 7);
expect(obj).toEqual({ a: { b: 3, c: 7, d: [6, 5] }})
// set new object inside array
setByDotNotation(obj, 'a.d.2.x', 8);
expect(obj).toEqual({ a: { b: 3, c: 7, d: [6, 5, { x: 8 }] }})
})
describe('.truncateText', () => {
it('should truncate long texts and append ellipsis', () => {
const longText = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrnged.`;
const result = truncateText(longText);
expect(result).toBe('Lorem Ipsum is simply dummy text of the printing and typesetting indus...');
});
it('should truncate texts longer than provided max length', () => {
const longText = `Lorem Ipsum is simply dummy text.`;
const result = truncateText(longText, 10);
expect(result).toBe('Lorem Ipsu...');
});
it('should keep short texts unchanged', () => {
const longText = `Lorem Ipsum is simply dummy text.`;
const result = truncateText(longText, 70);
expect(result).toBe('Lorem Ipsum is simply dummy text.');
});
});
describe('.getFullUrl', () => {
it('should not change the value when including env variable', () => {
const envVariable = '{{endpointUrl}}';
const result = getFullUrl(envVariable);
expect(result).toBe(envVariable);
});
it('should not change the value for a valid absolute url', () => {
const url = 'https://test.com/graphql';
const result = getFullUrl(url);
expect(result).toBe(url);
});
it('should prepend text with origin location', () => {
const endpoint = 'graphql';
const result = getFullUrl(endpoint);
expect(result).toBe(location.origin + "/" + endpoint);
});
})
})