-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathutil.test.ts
More file actions
200 lines (178 loc) · 6.25 KB
/
util.test.ts
File metadata and controls
200 lines (178 loc) · 6.25 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { UserTraits } from '../types';
import { chunk, allSettled, deepCompare, getURL } from '../util';
describe('#chunk', () => {
it('handles empty array', () => {
expect(chunk([], 5)).toEqual([]);
});
it('handles chunk to 1', () => {
expect(chunk([1, 2, 3, 4], 1)).toEqual([[1], [2], [3], [4]]);
});
it('handles chunk to 2', () => {
expect(chunk([1, 2, 3, 4], 2)).toEqual([
[1, 2],
[3, 4],
]);
});
it('handles remainders', () => {
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
});
it('handles max kb', () => {
const almost1KBString =
'heisxxohwswnetphtpnwyhluoeariblrgbksynoricexcoylnpmkymrgxmtuslveckeoulxqfwmemyxqvgyjnnclrmzcsacxkpdwzdthtudexkphsshjhlplvgryuqbqecgpbyussbgwuearvcmmrnkghcmcmynzxcswkghgrkhumeswvmhnhvymohluqooxmlbqdtxmeyikacyxvdkntrlwggoyrldmeaezowghoyukeeuaulxohknbbpyxpotonrtwfhoevysrfonevjtpjfywpptklmshzobtyklrahpkqnjifqtnxbeleeqxjacnphcomsdplxqmkcvnpxwbbmnmmzqvxmvpdrqaytlybmyghuckxnqlkgzptowgycanrenlgsgtwwbyudctiqmlszpzczfoglfoeuvdkublajngfmgstufgaroktarkeydqpmrgspuhbsqxqebzhycttsxkfcbgdqgctjgkrahoubqrvscdowafwfbysqerxqmzmakqibepyrjxjgcsnabjmovsgothxcawjsqzprxdnolyyhjfujgcxvniupmffqkhzknkzglazjflnnloazwslomxpegluqywyuvalysliziukrunfprejkojyihjjfcqdgsundoinipuzoxkqthslqgminkwcqglrjuhbqcirmuowdvizfsyubnllxxtjxqrctlzqmbfunxwzzbovogpvgrzeunvucaniqmqhwzcwqwzqvjaaeuajxgdhgitsekkowqejuouhaguudfkbyyivkayzsyoiymvavbabvtrgwmoadkvxxvgenttbohzngbzqiguficxkqvbxlrvkpycotawgalfdpmnmcfligffhivuzjtuxrwjjmzqcvbaocxfdmhnrcwhdmnpccikjvrskojxwnoiskacbivzfmbwyqfrhzjtdsjomzifswvaqdlorszbasmskosoullyyzpzjjzzkuvgoqwrzasqiroyaomrrxyqgzdigcwde';
expect(
chunk([almost1KBString, almost1KBString, almost1KBString], 5, 1)
).toEqual([[almost1KBString], [almost1KBString], [almost1KBString]]);
});
it('handles max kb and count at the same time', () => {
const about500bString =
'jfywukefmafvjroiisrqxudmmkqibiwaknaywfberhhtiymwrzcyoitzemqhwqamkccbgccsinagjddhhnijndpqyejfaztdbmcunmdaewraamfzssfwyzddttjkdpwehphowxrbpohntfohxvmufgoyovfguxlghvoisbmtufpyxqgqylufzhvavjylkhavobvmzyqbojniyjhkgssmnujyicucskiaenpeutaqbxsnzuinhnvfqbkmmoxzxhangjxhnuhskmldksucmridbyacvhycxpdgkkibypdphhwrkpmwivtgbvnfkilxmwvxbxuxklqstjltilksgaaxqmhdtmublkwzobrcoofiyygahosrmbmgftybdmdpyptsrntukfyczdusqlfgyexyojcfuloyyuepgkyhivmiqruqxbvixfotvwzjbamrqopsjiyftiuwmhbcfsvkrmjpmwthpwwszgjarargfpmxxkuwaofahreb';
expect(
chunk([about500bString, about500bString, about500bString], 2, 1)
).toEqual([[about500bString, about500bString], [about500bString]]);
});
});
describe('allSettled', () => {
it('handles all resolved and rejected promises properly', async () => {
const promises: (Promise<number> | number)[] = [];
promises.push(Promise.resolve(1));
promises.push(2);
promises.push(Promise.reject(3));
const results = await allSettled(promises);
expect(results).toEqual([
{
status: 'fulfilled',
value: 1,
},
{
status: 'fulfilled',
value: 2,
},
{
status: 'rejected',
reason: 3,
},
]);
});
});
describe('deepCompare', () => {
const base: UserTraits = {
age: 30,
address: {
city: 'San Francisco',
country: 'USA',
},
company: {
name: 'Twilio',
},
deepNested: {
level1: {
level2: {
level3: true,
},
},
},
};
it('shallow compare, same object should return true', () => {
const a: UserTraits = { ...base };
const b: UserTraits = a;
expect(deepCompare(a, b)).toBe(true);
});
it('deep compare, object copy should return true', () => {
const a: UserTraits = { ...base };
const b: UserTraits = { ...base };
expect(deepCompare(a, b)).toBe(true);
});
it('deep compare, different key objects should return false', () => {
const a: UserTraits = { ...base };
const b: UserTraits = {
age: 20,
deepNested: {
level1: {
level2: {
level3: false,
},
},
},
};
expect(deepCompare(a, b)).toBe(false);
});
it('deep compare, modified objects should return false', () => {
const a: UserTraits = { ...base };
const b: UserTraits = {
...base,
age: 20,
deepNested: {
level1: {
level2: {
level3: false,
},
},
},
};
expect(deepCompare(a, b)).toBe(false);
});
it('deep compare, different nested objects should return false', () => {
const a: UserTraits = { ...base };
const b: UserTraits = {
...base,
age: 20,
deepNested: {
level1: {
level2: {
level3: true,
},
anotherLevel2: {
level3: true,
},
},
},
};
expect(deepCompare(a, b)).toBe(false);
});
it('deep compare, mistmatching nested object type should return false', () => {
const a: UserTraits = { ...base };
const b: UserTraits = {
...base,
age: 20,
deepNested: {
level1: {
level2: 1,
},
},
};
expect(deepCompare(a, b)).toBe(false);
});
});
describe('getURL function', () => {
// Positive Test Cases
it('should return correct URL for valid host and path', () => {
expect(getURL('www.example.com', '/home')).toBe(
'https://www.example.com/home'
);
expect(getURL('blog.example.com', '/posts')).toBe(
'https://blog.example.com/posts'
);
});
it('should return the root URL when the path is empty', () => {
expect(getURL('www.example.com', '')).toBe('https://www.example.com');
});
it('should handle query parameters correctly in the URL path', () => {
expect(getURL('www.example.com', '/search?q=test')).toBe(
'https://www.example.com/search?q=test'
);
});
it('should handle special characters in the URL path', () => {
expect(getURL('www.example.com', '/about#section1')).toBe(
'https://www.example.com/about#section1'
);
});
// Negative Test Cases
it('should throw an error for empty host', () => {
expect(() => getURL('', '/home')).toThrow('Invalid URL has been passed');
});
it('should throw an error for invalid characters in the host', () => {
expect(() => getURL('invalid host.com', '/path')).toThrow(
'Invalid URL has been passed'
);
});
});