-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathobject.test.ts
275 lines (234 loc) · 7.85 KB
/
object.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
import { clone, deserialize, fill, serialize, urlEncode } from '../src/object';
const MATRIX = [
{ name: 'boolean', object: true, serialized: 'true' },
{ name: 'number', object: 42, serialized: '42' },
{ name: 'string', object: 'test', serialized: '"test"' },
{ name: 'array', object: [1, 'test'], serialized: '[1,"test"]' },
{ name: 'object', object: { a: 'test' }, serialized: '{"a":"test"}' },
{ name: 'nan', object: { a: NaN }, serialized: '{"a":"[NaN]"}' },
{ name: 'undefined', object: { a: undefined }, serialized: '{"a":"[undefined]"}' },
];
describe('clone()', () => {
for (const entry of MATRIX) {
test(`clones a ${entry.name}`, () => {
expect(clone(entry.object)).toEqual(entry.object);
});
}
});
describe('serialize()', () => {
function jsonify(obj: object): string {
return JSON.stringify(obj);
}
for (const entry of MATRIX) {
test(`serializes a ${entry.name}`, () => {
expect(serialize(entry.object)).toEqual(entry.serialized);
});
}
describe('cyclical structures', () => {
it('must stringify circular objects', () => {
const obj = { name: 'Alice' };
// @ts-ignore
obj.self = obj;
const json = serialize(obj);
expect(json).toEqual(jsonify({ name: 'Alice', self: '[Circular ~]' }));
});
it('must stringify circular objects with intermediaries', () => {
const obj = { name: 'Alice' };
// @ts-ignore
obj.identity = { self: obj };
const json = serialize(obj);
expect(json).toEqual(jsonify({ name: 'Alice', identity: { self: '[Circular ~]' } }));
});
it('must stringify circular objects deeper', () => {
const obj = { name: 'Alice', child: { name: 'Bob' } };
// @ts-ignore
obj.child.self = obj.child;
expect(serialize(obj)).toEqual(
jsonify({
name: 'Alice',
child: { name: 'Bob', self: '[Circular ~.child]' },
}),
);
});
it('must stringify circular objects deeper with intermediaries', () => {
const obj = { name: 'Alice', child: { name: 'Bob' } };
// @ts-ignore
obj.child.identity = { self: obj.child };
expect(serialize(obj)).toEqual(
jsonify({
name: 'Alice',
child: { name: 'Bob', identity: { self: '[Circular ~.child]' } },
}),
);
});
it('must stringify circular objects in an array', () => {
const obj = { name: 'Alice' };
// @ts-ignore
obj.self = [obj, obj];
expect(serialize(obj)).toEqual(
jsonify({
name: 'Alice',
self: ['[Circular ~]', '[Circular ~]'],
}),
);
});
it('must stringify circular objects deeper in an array', () => {
const obj = {
name: 'Alice',
children: [{ name: 'Bob' }, { name: 'Eve' }],
};
// @ts-ignore
obj.children[0].self = obj.children[0];
// @ts-ignore
obj.children[1].self = obj.children[1];
expect(serialize(obj)).toEqual(
jsonify({
name: 'Alice',
children: [
{ name: 'Bob', self: '[Circular ~.children.0]' },
{ name: 'Eve', self: '[Circular ~.children.1]' },
],
}),
);
});
it('must stringify circular arrays', () => {
const obj: object[] = [];
obj.push(obj);
obj.push(obj);
const json = serialize(obj);
expect(json).toEqual(jsonify(['[Circular ~]', '[Circular ~]']));
});
it('must stringify circular arrays with intermediaries', () => {
const obj: object[] = [];
obj.push({ name: 'Alice', self: obj });
obj.push({ name: 'Bob', self: obj });
expect(serialize(obj)).toEqual(
jsonify([{ name: 'Alice', self: '[Circular ~]' }, { name: 'Bob', self: '[Circular ~]' }]),
);
});
it('must stringify repeated objects in objects', () => {
const obj = {};
const alice = { name: 'Alice' };
// @ts-ignore
obj.alice1 = alice;
// @ts-ignore
obj.alice2 = alice;
expect(serialize(obj)).toEqual(
jsonify({
alice1: { name: 'Alice' },
alice2: { name: 'Alice' },
}),
);
});
it('must stringify repeated objects in arrays', () => {
const alice = { name: 'Alice' };
const obj = [alice, alice];
const json = serialize(obj);
expect(json).toEqual(jsonify([{ name: 'Alice' }, { name: 'Alice' }]));
});
it('must stringify error objects, including extra properties', () => {
const obj = new Error('Wubba Lubba Dub Dub');
// @ts-ignore
obj.reason = new TypeError("I'm pickle Riiick!");
// @ts-ignore
obj.extra = 'some extra prop';
// Stack is inconsistent across browsers, so override it and just make sure its stringified
obj.stack = 'x';
// @ts-ignore
obj.reason.stack = 'x';
// IE 10/11
// @ts-ignore
delete obj.description;
// @ts-ignore
delete obj.reason.description;
// Safari doesn't allow deleting those properties from error object, yet only it provides them
const result = serialize(obj)
.replace(/ +"(line|column|sourceURL)": .+,?\n/g, '')
.replace(/,\n( +)}/g, '\n$1}'); // make sure to strip trailing commas as well
expect(result).toEqual(
jsonify({
message: 'Wubba Lubba Dub Dub',
name: 'Error',
stack: 'x',
reason: {
message: "I'm pickle Riiick!",
name: 'TypeError',
stack: 'x',
},
extra: 'some extra prop',
}),
);
});
});
it('must stringify error objects with circular references', () => {
const obj = new Error('Wubba Lubba Dub Dub');
// @ts-ignore
obj.reason = obj;
// Stack is inconsistent across browsers, so override it and just make sure its stringified
obj.stack = 'x';
// @ts-ignore
obj.reason.stack = 'x';
// IE 10/11
// @ts-ignore
delete obj.description;
// Safari doesn't allow deleting those properties from error object, yet only it provides them
const result = serialize(obj)
.replace(/ +"(line|column|sourceURL)": .+,?\n/g, '')
.replace(/,\n( +)}/g, '\n$1}'); // make sure to strip trailing commas as well
expect(result).toEqual(
jsonify({
message: 'Wubba Lubba Dub Dub',
name: 'Error',
stack: 'x',
reason: '[Circular ~]',
}),
);
});
});
describe('deserialize()', () => {
for (const entry of MATRIX) {
test(`deserializes a ${entry.name}`, () => {
// tslint:disable-next-line:no-inferred-empty-object-type
expect(deserialize(entry.serialized)).toEqual(entry.object);
});
}
});
describe('fill()', () => {
test('wraps a method by calling a replacement function on it', () => {
const source = {
foo(): number {
return 42;
},
};
const name = 'foo';
const replacement = jest.fn().mockImplementationOnce(cb => cb);
fill(source, name, replacement);
expect(source.foo()).toEqual(42);
expect(replacement).toBeCalled();
});
test('can do anything inside replacement function', () => {
const source = {
foo: (): number => 42,
};
const name = 'foo';
const replacement = jest.fn().mockImplementationOnce(cb => {
expect(cb).toBe(source.foo);
return () => 1337;
});
fill(source, name, replacement);
expect(source.foo()).toEqual(1337);
expect(replacement).toBeCalled();
expect.assertions(3);
});
});
describe('urlEncode()', () => {
test('returns empty string for empty object input', () => {
expect(urlEncode({})).toEqual('');
});
test('returns single key/value pair joined with = sign', () => {
expect(urlEncode({ foo: 'bar' })).toEqual('foo=bar');
});
test('returns multiple key/value pairs joined together with & sign', () => {
expect(urlEncode({ foo: 'bar', pickle: 'rick', morty: '4 2' })).toEqual('foo=bar&pickle=rick&morty=4%202');
});
});