2021-02-24 17:33:07 +01:00
|
|
|
/*
|
|
|
|
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";
|
2022-02-10 19:52:45 +01:00
|
|
|
import { structuredClone } from "./structuredClone.js";
|
2021-02-24 17:33:07 +01:00
|
|
|
|
|
|
|
function checkClone(t: ExecutionContext, x: any): void {
|
|
|
|
t.deepEqual(structuredClone(x), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
test("structured clone", (t) => {
|
|
|
|
checkClone(t, "foo");
|
|
|
|
checkClone(t, [1, 2]);
|
2021-04-07 15:52:57 +02:00
|
|
|
checkClone(t, true);
|
|
|
|
checkClone(t, false);
|
2021-02-24 17:33:07 +01:00
|
|
|
checkClone(t, { x1: "foo" });
|
2021-04-07 15:52:57 +02:00
|
|
|
checkClone(t, { x1: true, x2: false });
|
2021-02-24 17:33:07 +01:00
|
|
|
checkClone(t, new Date());
|
|
|
|
checkClone(t, [new Date()]);
|
|
|
|
checkClone(t, undefined);
|
|
|
|
checkClone(t, [undefined]);
|
2021-02-25 16:34:55 +01:00
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
structuredClone({ foo: () => {} });
|
|
|
|
});
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
structuredClone(Promise);
|
|
|
|
});
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
structuredClone(Promise.resolve());
|
|
|
|
});
|
2021-02-24 17:33:07 +01:00
|
|
|
});
|
|
|
|
|
2021-12-15 02:37:03 +01:00
|
|
|
test("structured clone (array cycles)", (t) => {
|
2021-02-24 17:33:07 +01:00
|
|
|
const obj1: any[] = [1, 2];
|
|
|
|
obj1.push(obj1);
|
|
|
|
const obj1Clone = structuredClone(obj1);
|
|
|
|
t.is(obj1Clone, obj1Clone[2]);
|
|
|
|
});
|
2021-12-15 02:37:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|