UNPKG

5.67 kBJavaScriptView Raw
1// @flow
2import {
3 serialize,
4 deserialize,
5 registerSerializableClass,
6 unregisterSerializableClass,
7} from '../src/serializer';
8import assert from 'assert';
9
10describe('serializer', () => {
11 it('should serialize a basic object', () => {
12 let serialized = serialize({foo: 2, bar: 3});
13 assert(Buffer.isBuffer(serialized));
14 let deserialized = deserialize(serialized);
15 assert.equal(typeof deserialized, 'object');
16 assert.deepEqual(deserialized, {foo: 2, bar: 3});
17 });
18
19 it('should serialize an object with multiple references', () => {
20 let a = {foo: 2};
21 let b = {bar: a, baz: a};
22 let res = deserialize(serialize(b));
23 assert.deepEqual(res, b);
24 assert.equal(res.bar, res.baz);
25 });
26
27 it('should serialize a cyclic object', () => {
28 let a = {foo: 2, bar: {}};
29 a.bar = a;
30 let res = deserialize(serialize(a));
31 assert.deepEqual(res, a);
32 assert.equal(res.bar, res);
33 assert.equal(a.bar, a);
34 });
35
36 it('should serialize a Map', () => {
37 let a = new Map([[2, 3]]);
38 let res = deserialize(serialize(a));
39 assert(res instanceof Map);
40 assert.equal(res.get(2), 3);
41 });
42
43 it('should serialize a Set', () => {
44 let a = new Set([2, 3]);
45 let res = deserialize(serialize(a));
46 assert(res instanceof Set);
47 assert(res.has(2));
48 assert(res.has(3));
49 });
50
51 it('should serialize a class', () => {
52 class Test {
53 x: number;
54 constructor(x: number) {
55 this.x = x;
56 }
57 }
58
59 registerSerializableClass('Test', Test);
60
61 let x = new Test(2);
62 let res = deserialize(serialize(x));
63 assert(res instanceof Test);
64 assert.equal(res.x, x.x);
65
66 unregisterSerializableClass('Test', Test);
67 });
68
69 it('should serialize a class with a custom serialize method', () => {
70 class Test {
71 x: number;
72 constructor(x: number) {
73 this.x = x;
74 }
75
76 serialize() {
77 return {
78 x: this.x,
79 serialized: true,
80 };
81 }
82 }
83
84 registerSerializableClass('Test', Test);
85
86 let x = new Test(2);
87 let res = deserialize(serialize(x));
88 assert(res instanceof Test);
89 assert.equal(res.x, x.x);
90 assert.equal(res.serialized, true);
91
92 unregisterSerializableClass('Test', Test);
93 });
94
95 it('should serialize a class with a custom deserialize method', () => {
96 class Test {
97 x: number;
98 constructor(x: number) {
99 this.x = x;
100 }
101
102 static deserialize(x: any) {
103 return {
104 deserialized: true,
105 value: x,
106 };
107 }
108 }
109
110 registerSerializableClass('Test', Test);
111
112 let x = new Test(2);
113 let res = deserialize(serialize(x));
114 assert(!(res instanceof Test));
115 assert.equal(res.value.x, x.x);
116 assert.equal(res.deserialized, true);
117
118 unregisterSerializableClass('Test', Test);
119 });
120
121 it('should serialize a class recursively', () => {
122 class Foo {
123 x: number;
124 constructor(x: number) {
125 this.x = x;
126 }
127 }
128
129 class Bar {
130 foo: Foo;
131 constructor(foo: Foo) {
132 this.foo = foo;
133 }
134 }
135
136 registerSerializableClass('Foo', Foo);
137 registerSerializableClass('Bar', Bar);
138
139 let x = new Bar(new Foo(2));
140 let res = deserialize(serialize(x));
141 assert(res instanceof Bar);
142 assert(res.foo instanceof Foo);
143 assert.equal(res.foo.x, 2);
144
145 unregisterSerializableClass('Foo', Foo);
146 unregisterSerializableClass('Bar', Bar);
147 });
148
149 it('should serialize a cyclic class', () => {
150 class Foo {
151 x: ?Foo;
152 constructor(x: ?Foo) {
153 this.x = x;
154 }
155 }
156
157 registerSerializableClass('Foo', Foo);
158
159 let x = new Foo();
160 x.x = x;
161
162 let res = deserialize(serialize(x));
163 assert(res instanceof Foo);
164 assert(res.x instanceof Foo);
165 assert.equal(res.x, res);
166
167 assert.equal(x.x, x);
168
169 unregisterSerializableClass('Foo', Foo);
170 });
171
172 it('should copy on write', () => {
173 class Foo {
174 x: number;
175 constructor(x: number) {
176 this.x = x;
177 }
178 }
179
180 registerSerializableClass('Foo', Foo);
181
182 let x = {y: {foo: new Foo(2)}};
183
184 let res = deserialize(serialize(x));
185 assert(res.y.foo instanceof Foo);
186 assert(x.y.foo instanceof Foo);
187
188 unregisterSerializableClass('Foo', Foo);
189 });
190
191 it('should serialize a cyclic class and copy on write', () => {
192 class Foo {
193 x: ?Foo;
194 constructor(x: ?Foo) {
195 this.x = x;
196 }
197 }
198
199 registerSerializableClass('Foo', Foo);
200
201 let x = new Foo();
202 x.x = x;
203 let y = {x: {y: x}};
204
205 let res = deserialize(serialize(y));
206 assert(res.x.y instanceof Foo);
207 assert(res.x.y.x instanceof Foo);
208 assert(y.x.y instanceof Foo);
209 assert(y.x.y.x instanceof Foo);
210 assert.equal(res.x.y.x, res.x.y);
211
212 assert.equal(x.x, x);
213
214 unregisterSerializableClass('Foo', Foo);
215 });
216
217 it('should serialize a class inside a Map', () => {
218 class Test {
219 x: number;
220 constructor(x: number) {
221 this.x = x;
222 }
223 }
224
225 registerSerializableClass('Test', Test);
226
227 let x = new Map([[2, new Test(2)]]);
228 let res = deserialize(serialize(x));
229 assert(res instanceof Map);
230 assert(res.get(2) instanceof Test);
231
232 unregisterSerializableClass('Test', Test);
233 });
234
235 it('should serialize a class inside a Set', () => {
236 class Test {
237 x: number;
238 constructor(x: number) {
239 this.x = x;
240 }
241 }
242
243 registerSerializableClass('Test', Test);
244
245 let x = new Set([new Test(2)]);
246 let res = deserialize(serialize(x));
247 assert(res instanceof Set);
248 assert(res.values().next().value instanceof Test);
249
250 unregisterSerializableClass('Test', Test);
251 });
252});