summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_chromeutils_shallowclone.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/base/test/unit/test_chromeutils_shallowclone.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/base/test/unit/test_chromeutils_shallowclone.js')
-rw-r--r--dom/base/test/unit/test_chromeutils_shallowclone.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/dom/base/test/unit/test_chromeutils_shallowclone.js b/dom/base/test/unit/test_chromeutils_shallowclone.js
new file mode 100644
index 0000000000..03e0d443c9
--- /dev/null
+++ b/dom/base/test/unit/test_chromeutils_shallowclone.js
@@ -0,0 +1,60 @@
+"use strict";
+
+add_task(function test_shallowclone() {
+ // Check that shallow cloning an object with regular properties,
+ // results into a new object with all properties from the source object.
+ const fullyCloneableObject = {
+ numProp: 123,
+ strProp: "str",
+ boolProp: true,
+ arrayProp: [{ item1: "1", item2: "2" }],
+ fnProp() {
+ return "fn result";
+ },
+ promise: Promise.resolve("promised-value"),
+ weakmap: new WeakMap(),
+ proxy: new Proxy({}, {}),
+ };
+
+ let clonedObject = ChromeUtils.shallowClone(fullyCloneableObject);
+
+ Assert.deepEqual(
+ clonedObject,
+ fullyCloneableObject,
+ "Got the expected cloned object for an object with regular properties"
+ );
+
+ // Check that shallow cloning an object with getters and setters properties,
+ // results into a new object without all the properties from the source object excluded
+ // its getters and setters.
+ const objectWithGetterAndSetter = {
+ get myGetter() {
+ return "getter result";
+ },
+ set mySetter(v) {},
+ myFunction() {
+ return "myFunction result";
+ },
+ };
+
+ clonedObject = ChromeUtils.shallowClone(objectWithGetterAndSetter);
+
+ Assert.deepEqual(
+ clonedObject,
+ {
+ myFunction: objectWithGetterAndSetter.myFunction,
+ },
+ "Got the expected cloned object for an object with getters and setters"
+ );
+
+ // Check that shallow cloning a proxy object raises the expected exception..
+ const proxyObject = new Proxy(fullyCloneableObject, {});
+
+ Assert.throws(
+ () => {
+ ChromeUtils.shallowClone(proxyObject);
+ },
+ /Shallow cloning a proxy object is not allowed/,
+ "Got the expected error on ChromeUtils.shallowClone called on a proxy object"
+ );
+});