summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_chromeutils_shallowclone.js
blob: 03e0d443c9a692eab9279ec184f7f0a8514f880b (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
"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"
  );
});