diff options
author | Özgür Kesim <oec-taler@kesim.org> | 2023-08-25 13:24:08 +0200 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2023-08-25 13:24:08 +0200 |
commit | 5ab3070b3a63c2e8fed0e413dea06cf03fb48f1e (patch) | |
tree | 121425d99c9465f2daf8ac91c6dc2254ebac5906 /packages/idb-bridge/src/util/structuredClone.test.ts | |
parent | 70fca92e781696a057089bc8bc48adebdf6e017e (diff) | |
parent | 2051aded501cddac1a4c869fb1f9731ac4523a1e (diff) |
Merge branch 'master' into age-withdraw
Diffstat (limited to 'packages/idb-bridge/src/util/structuredClone.test.ts')
-rw-r--r-- | packages/idb-bridge/src/util/structuredClone.test.ts | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/packages/idb-bridge/src/util/structuredClone.test.ts b/packages/idb-bridge/src/util/structuredClone.test.ts index 0c613e6cc..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.js"; +import { + structuredClone, + structuredEncapsulate, + structuredRevive, +} from "./structuredClone.js"; function checkClone(t: ExecutionContext, x: any): void { t.deepEqual(structuredClone(x), x); @@ -59,3 +63,58 @@ test("structured clone (object cycles)", (t) => { 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); + } +}); |