-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoder.test.ts
279 lines (248 loc) · 8.48 KB
/
encoder.test.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {
decode,
decodePrimitive,
encode,
errorSym,
parseJSON,
} from './encoder';
import { parseSPObj } from '../parseSPObj';
import { type JSONCompatible } from '../utils';
describe('encoder', () => {
describe('string', () => {
it('simple', () => {
expect('').toStrictEqual(decode(encode('')));
expect(decode(encode('test'))).toStrictEqual('test');
});
it('special characters', () => {
const str = '"test #?\t=\n[]{}()%.';
expect(decode(encode(str))).toStrictEqual(str);
});
});
describe('number', () => {
it('integer and float', () => {
const num1 = 5;
const num2 = 5.55;
expect(num1).toStrictEqual(decode(encode(num1)));
expect(num2).toStrictEqual(decode(encode(num2)));
expect(encode(num1)).toStrictEqual('5');
expect(encode(num2)).toStrictEqual('5.55');
});
});
describe('date', () => {
it('instance', () => {
const d = new Date('2024-06-28T09:10:38.763Z');
expect(decode(encode(d))).toStrictEqual(d);
expect(encode(d)).toStrictEqual('⏲2024-06-28T09:10:38.763Z');
});
it('iso string', () => {
const d = '2024-06-28T09:10:38.763Z';
expect(encode(d)).toStrictEqual("'2024-06-28T09:10:38.763Z'");
expect(decode(encode(d))).toStrictEqual(d);
});
});
it('boolean', () => {
expect(decode(encode(true))).toStrictEqual(true);
expect(decode(encode(false))).toStrictEqual(false);
expect(encode(true)).toStrictEqual('true');
expect(encode(false)).toStrictEqual('false');
});
it('null', () => {
expect(decode(encode(null))).toStrictEqual(null);
expect(encode(null)).toStrictEqual('null');
});
it('undefined', () => {
expect(decode(encode(undefined))).toStrictEqual(undefined);
expect(encode(undefined)).toStrictEqual('∙undefined');
});
describe('object', () => {
it('simple', () => {
const obj = { num: 123, float: 1.55, bool1: false };
expect(decode(encode(obj))).toStrictEqual(obj);
expect(encode(obj)).toStrictEqual(
"{'num':123,'float':1.55,'bool1':false}",
);
});
it('nested', () => {
const obj = {
num: 123,
num2: 3.14,
b1: true,
b2: false,
str: 'test string',
n: null,
obj1: {
obj2: {
str: 'my_str',
n: 123,
n2: -12.3,
b: false,
b1: true,
// FIXME: can't test dates conversion in jsdom, have weird e2e tests with useEffect in Status-for-test
// date: new Date('2019-01-01T00:00:00.000Z'),
dateIso: '2020-01-01T00:00:00.000Z',
},
},
dateIso: '2022-01-01T00:00:00.000Z',
};
expect(decode(encode(obj))).toStrictEqual(obj);
expect(encode(obj)).toStrictEqual(
"{'num':123,'num2':3.14,'b1':true,'b2':false,'str':'test string','n':null,'obj1':{'obj2':{'str':'my_str','n':123,'n2':-12.3,'b':false,'b1':true,'dateIso':'2020-01-01T00:00:00.000Z'}},'dateIso':'2022-01-01T00:00:00.000Z'}",
);
expect(
decode(
"{'num':123,'num2':3.14,'b1':true,'b2':false,'str':'test string','n':null,'obj1':{'obj2':{'str':'my_str','n':123,'n2':-12.3,'b':false,'b1':true,'dateIso':'2020-01-01T00:00:00.000Z'}},'dateIso':'2022-01-01T00:00:00.000Z'}",
),
).toStrictEqual(obj);
});
});
describe('array', () => {
it('simple', () => {
const obj = [123, 1.55, false];
const expected = '[123,1.55,false]';
expect(decode(encode(obj))).toStrictEqual(obj);
expect(decode(expected)).toStrictEqual(obj);
expect(encode(obj)).toStrictEqual(expected);
});
it('nested', () => {
const obj = [123, [45, true, { arr: [1, 2, { test: true }, null] }]];
expect(decode(encode(obj))).toStrictEqual(obj);
expect(encode(obj)).toStrictEqual(
"[123,[45,true,{'arr':[1,2,{'test':true},null]}]]",
);
});
it('array with invalid value', () => {
const expected = [1, 'str'];
expect(decode(encode(expected))).toStrictEqual(expected);
expect(decode("[1,'str']", expected)).toStrictEqual(expected);
expect(decode("[1,'str']")).toStrictEqual(expected);
});
});
// no sense to handle this
it('function', () => {
expect(encode(() => {})).toStrictEqual('');
});
it('symbol', () => {
expect(encode(Symbol('a'))).toStrictEqual('');
});
});
describe('real life example', () => {
const bigObj = {
emails: {
sha1: [
'f37224933f5e906904ef1f9aeca3486941664555',
'8a3c1b25c63e3a402e5e3a8e981054ddfec20a18',
],
sha256: [
'9d67f9cab3b2775dbe38e9f6521c23d20037391c0879e4700fea9e9a7672a79e',
'65c4bbea793b0ac3d5adb86beac555294df2aac05c932374d1d2e382bfd3ea4c',
],
},
client: {
deviceType: 'Personal computer',
browserLanguage: 'en-GB',
isUserLoggedIn: true,
countryCode: 'ge',
isInternalRequest: false,
},
id: { dmp: 'AT56sgZZhNi6E_vvdRIAFf-HXftr&v=2' },
numb1: 100500,
numb2: 100.12345,
compliance: {
isCCPAOptIn: true,
isLinkedInEmployee: false,
isFunctionalOptIn: true,
isGeoOptIn: true,
isGDPROptIn: true,
isAdvertisingOptIn: true,
isAnalyticsAndResearchOptIn: true,
},
primaryEmail: {
sha1: 'f37224933f5e906904ef1f9aeca3486941664555',
sha256:
'9d67f9cab3b2775dbe38e9f6521c23d20037391c0879e4700fea9e9a7672a79e',
},
preference: { language: 'en-us' },
};
it('encode/decode', () => {
const a1 = performance.now();
const result = decode(encode(bigObj));
const a2 = performance.now();
console.log('encode time', a2 - a1 + ' milliseconds');
expect(result).toStrictEqual(bigObj);
});
});
describe('decodePrimitive', () => {
it('should encode with specia symbols', () => {
expect(decodePrimitive('∙undefined')).toStrictEqual(undefined);
const date = new Date('2024-06-28T09:10:38.763Z');
expect((decodePrimitive(`⏲${date}`) as Date).toString()).toStrictEqual(
date.toString(),
);
});
it('should return error for other primitive values', () => {
expect(decodePrimitive('null')).toStrictEqual(errorSym);
expect(decodePrimitive('false')).toStrictEqual(errorSym);
expect(decodePrimitive('true')).toStrictEqual(errorSym);
expect(decodePrimitive('3')).toStrictEqual(errorSym);
expect(decodePrimitive('3.14')).toStrictEqual(errorSym);
expect(decodePrimitive('test%20string')).toStrictEqual(errorSym);
})
it('invalid string', () => {
expect(decodePrimitive('')).toStrictEqual(errorSym);
expect(decodePrimitive('invalid')).toStrictEqual(errorSym);
const date = new Date('2024-06-28T09:10:38.763Z');
expect(decodePrimitive(` ⏲${date}`) as Date).toStrictEqual(errorSym);
expect(decodePrimitive(' null')).toStrictEqual(errorSym);
expect(decodePrimitive(' ∙undefined')).toStrictEqual(errorSym);
expect(decodePrimitive(' false')).toStrictEqual(errorSym);
expect(decodePrimitive(' true')).toStrictEqual(errorSym);
expect(decodePrimitive(' 3')).toStrictEqual(errorSym);
expect(decodePrimitive(' 3.14')).toStrictEqual(errorSym);
expect(decodePrimitive(' test%20string')).toStrictEqual(errorSym);
});
});
describe('parseJSON', () => {
const state = {
age: 30,
terms: true,
terms2: false,
n: null,
u: undefined,
arr: [],
obj: {},
arr1: [1, 2],
obj1: { t: 123 },
};
it('standard JSON without strings', () => {
// undefined stripped out in default JSON too, toStrictEqual preserves undefined
expect(parseJSON(JSON.stringify(state))).toEqual(state);
});
it('standard JSON', () => {
expect(parseJSON(JSON.stringify({ ...state, str: 'string' }))).toEqual({
...state,
str: 'string',
});
});
it('with encoded values', () => {
expect(parseJSON(encode(state).replace(/'/g, '"'))).toEqual(state);
});
it('should return fallback for invalid JSON', () => {
expect(parseJSON('invalid', '' as unknown as JSONCompatible)).toEqual('');
expect(parseJSON('invalid')).toEqual(undefined);
});
});
describe('parseSPObj', () => {
const stateShape = { perPage: 10 };
it('should parse params to object', () => {
expect(parseSPObj({}, stateShape)).toStrictEqual(stateShape);
expect(parseSPObj({ perPage: '20' }, stateShape)).toStrictEqual({
perPage: 20,
});
expect(
parseSPObj(undefined as unknown as object, stateShape),
).toStrictEqual(stateShape);
expect(parseSPObj(null as unknown as object, stateShape)).toStrictEqual(
stateShape,
);
});
});