summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/util/structuredClone.test.ts
blob: e13d4117f2c71f256097252cd5b354e6deaa8925 (plain)
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
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/

import test, { ExecutionContext } from "ava";
import {
  structuredClone,
  structuredEncapsulate,
  structuredRevive,
} from "./structuredClone.js";

function checkClone(t: ExecutionContext, x: any): void {
  t.deepEqual(structuredClone(x), x);
}

test("structured clone", (t) => {
  checkClone(t, "foo");
  checkClone(t, [1, 2]);
  checkClone(t, true);
  checkClone(t, false);
  checkClone(t, { x1: "foo" });
  checkClone(t, { x1: true, x2: false });
  checkClone(t, new Date());
  checkClone(t, [new Date()]);
  checkClone(t, undefined);
  checkClone(t, [undefined]);

  t.throws(() => {
    structuredClone({ foo: () => {} });
  });

  t.throws(() => {
    structuredClone(Promise);
  });

  t.throws(() => {
    structuredClone(Promise.resolve());
  });
});

test("structured clone (array cycles)", (t) => {
  const obj1: any[] = [1, 2];
  obj1.push(obj1);
  const obj1Clone = structuredClone(obj1);
  t.is(obj1Clone, obj1Clone[2]);
});

test("structured clone (object cycles)", (t) => {
  const obj1: any = { a: 1, b: 2 };
  obj1.c = obj1;
  const obj1Clone = structuredClone(obj1);
  t.is(obj1Clone, obj1Clone.c);
});

test("encapsulate", (t) => {
  t.deepEqual(structuredEncapsulate(42), 42);
  t.deepEqual(structuredEncapsulate(true), true);
  t.deepEqual(structuredEncapsulate(false), false);
  t.deepEqual(structuredEncapsulate(null), null);

  t.deepEqual(structuredEncapsulate(undefined), { $: "undef" });
  t.deepEqual(structuredEncapsulate(42n), { $: "bigint", val: "42" });

  t.deepEqual(structuredEncapsulate(new Date(42)), { $: "date", val: 42 });

  t.deepEqual(structuredEncapsulate({ x: 42 }), { x: 42 });

  t.deepEqual(structuredEncapsulate({ $: "bla", x: 42 }), {
    $: "obj",
    val: { $: "bla", x: 42 },
  });

  const x = { foo: 42, bar: {} } as any;
  x.bar.baz = x;

  t.deepEqual(structuredEncapsulate(x), {
    foo: 42,
    bar: {
      baz: { $: "ref", d: 2, p: [] },
    },
  });
});

test("revive", (t) => {
  t.deepEqual(structuredRevive(42), 42);
  t.deepEqual(structuredRevive([1, 2, 3]), [1, 2, 3]);
  t.deepEqual(structuredRevive(true), true);
  t.deepEqual(structuredRevive(false), false);
  t.deepEqual(structuredRevive(null), null);
  t.deepEqual(structuredRevive({ $: "undef" }), undefined);
  t.deepEqual(structuredRevive({ x: { $: "undef" } }), { x: undefined });

  t.deepEqual(structuredRevive({ $: "date", val: 42}), new Date(42));

  {
    const x = { foo: 42, bar: {} } as any;
    x.bar.baz = x;

    const r = {
      foo: 42,
      bar: {
        baz: { $: "ref", d: 2, p: [] },
      },
    };

    t.deepEqual(structuredRevive(r), x);
  }
});