summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/util/structuredClone.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/idb-bridge/src/util/structuredClone.test.ts')
-rw-r--r--packages/idb-bridge/src/util/structuredClone.test.ts70
1 files changed, 68 insertions, 2 deletions
diff --git a/packages/idb-bridge/src/util/structuredClone.test.ts b/packages/idb-bridge/src/util/structuredClone.test.ts
index 352c2c30b..e13d4117f 100644
--- a/packages/idb-bridge/src/util/structuredClone.test.ts
+++ b/packages/idb-bridge/src/util/structuredClone.test.ts
@@ -15,7 +15,11 @@
*/
import test, { ExecutionContext } from "ava";
-import { structuredClone } from "./structuredClone";
+import {
+ structuredClone,
+ structuredEncapsulate,
+ structuredRevive,
+} from "./structuredClone.js";
function checkClone(t: ExecutionContext, x: any): void {
t.deepEqual(structuredClone(x), x);
@@ -46,9 +50,71 @@ test("structured clone", (t) => {
});
});
-test("structured clone (cycles)", (t) => {
+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);
+ }
+});