test_chromeutils_shallowclone.js (1717B)
1 "use strict"; 2 3 add_task(function test_shallowclone() { 4 // Check that shallow cloning an object with regular properties, 5 // results into a new object with all properties from the source object. 6 const fullyCloneableObject = { 7 numProp: 123, 8 strProp: "str", 9 boolProp: true, 10 arrayProp: [{ item1: "1", item2: "2" }], 11 fnProp() { 12 return "fn result"; 13 }, 14 promise: Promise.resolve("promised-value"), 15 weakmap: new WeakMap(), 16 proxy: new Proxy({}, {}), 17 }; 18 19 let clonedObject = ChromeUtils.shallowClone(fullyCloneableObject); 20 21 Assert.deepEqual( 22 clonedObject, 23 fullyCloneableObject, 24 "Got the expected cloned object for an object with regular properties" 25 ); 26 27 // Check that shallow cloning an object with getters and setters properties, 28 // results into a new object without all the properties from the source object excluded 29 // its getters and setters. 30 const objectWithGetterAndSetter = { 31 get myGetter() { 32 return "getter result"; 33 }, 34 set mySetter(v) {}, 35 myFunction() { 36 return "myFunction result"; 37 }, 38 }; 39 40 clonedObject = ChromeUtils.shallowClone(objectWithGetterAndSetter); 41 42 Assert.deepEqual( 43 clonedObject, 44 { 45 myFunction: objectWithGetterAndSetter.myFunction, 46 }, 47 "Got the expected cloned object for an object with getters and setters" 48 ); 49 50 // Check that shallow cloning a proxy object raises the expected exception.. 51 const proxyObject = new Proxy(fullyCloneableObject, {}); 52 53 Assert.throws( 54 () => { 55 ChromeUtils.shallowClone(proxyObject); 56 }, 57 /Shallow cloning a proxy object is not allowed/, 58 "Got the expected error on ChromeUtils.shallowClone called on a proxy object" 59 ); 60 });