1.8 KB74 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import {deserialize, serialize} from '../serialize';
9
10describe('serialize', () => {
11 it('map', () => {
12 const map = new Map([
13 [1, 'a'],
14 [2, 'b'],
15 ]);
16 expect(deserialize(serialize(map))).toEqual(map);
17 });
18 it('set', () => {
19 const set = new Set([1, 2, 3, 1]);
20 expect(deserialize(serialize(set))).toEqual(set);
21 });
22
23 it('nesting Maps and Sets', () => {
24 const complex = new Map([
25 ['a', new Set([1, 2, 3, 1])],
26 ['b', new Set([10, 20, 10])],
27 ]);
28 expect(deserialize(serialize(complex))).toEqual(complex);
29 });
30
31 it('Dates', () => {
32 const date = new Date('2020-12-01');
33 expect(deserialize(serialize(date))?.valueOf()).toBe(date.valueOf());
34 });
35
36 it('nested objects', () => {
37 const nested = {
38 a: new Date('2020-12-01'),
39 b: [new Set([1, 2, 3, 1]), new Set([2, 3, 4, 5])],
40 c: {
41 d: new Map([
42 ['1', 1],
43 ['2', 2],
44 ]),
45 e: 'just a regular primitive',
46 f: 2,
47 g: [
48 {
49 a: new Set([1, 2, 3, 4]),
50 },
51 {a: new Set([1, 2, 3, 4])},
52 ],
53 },
54 };
55 expect(deserialize(serialize(nested))).toEqual(nested);
56 });
57
58 it('undefined is preserved', () => {
59 expect(deserialize(serialize(undefined))).toEqual(undefined);
60 });
61
62 it('objects without a prototype', () => {
63 const o = Object.create(null);
64 o.a = 123;
65 expect(deserialize(serialize(o))).toEqual({a: 123});
66 });
67
68 it('errors', () => {
69 expect(deserialize(serialize(new Error('this is my error\nwow')))).toEqual(
70 new Error('this is my error\nwow'),
71 );
72 });
73});
74