proxy-getOwnPropertyDescriptor.html (4253B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>getOwnPropertyDescriptor() is correct for Proxy with host object target</title> 6 <link rel="help" href="https://html.spec.whatwg.org/multipage/#window"> 7 <link rel="help" href="https://webidl.spec.whatwg.org/#Unforgeable"> 8 <script src="/resources/testharness.js"></script> 9 <script src="/resources/testharnessreport.js"></script> 10 </head> 11 <body> 12 <script> 13 'use strict'; 14 15 const assert_accessor_descriptor_equals = (actual, expected) => { 16 assert_equals(actual.get, expected.get, 'get'); 17 assert_equals(actual.set, expected.set, 'set'); 18 assert_equals(actual.enumerable, expected.enumerable, 'enumerable'); 19 assert_equals(actual.configurable, expected.configurable, 'configurable'); 20 }; 21 22 const assert_data_descriptor_equals = (actual, expected) => { 23 assert_equals(actual.value, expected.value, 'value'); 24 assert_equals(actual.writable, expected.writable, 'writable'); 25 assert_equals(actual.enumerable, expected.enumerable, 'enumerable'); 26 assert_equals(actual.configurable, expected.configurable, 'configurable'); 27 }; 28 29 test(() => { 30 const windowProxy = new Proxy(window, {}); 31 name = 'old_name'; 32 const descriptor = Object.getOwnPropertyDescriptor(windowProxy, 'name'); 33 34 assert_equals(descriptor.get.call(window), 'old_name'); 35 descriptor.set.call(window, 'new_name'); 36 assert_equals(name, 'new_name'); 37 assert_true(descriptor.enumerable); 38 assert_true(descriptor.configurable); 39 }, 'Window target, no trap, "name" attribute'); 40 41 test(() => { 42 let trapCalls = 0; 43 const windowProxy = new Proxy(window, { 44 getOwnPropertyDescriptor(...args) { 45 trapCalls++; 46 return Reflect.getOwnPropertyDescriptor(...args); 47 }, 48 }); 49 50 assert_accessor_descriptor_equals( 51 Object.getOwnPropertyDescriptor(windowProxy, 'document'), 52 Object.getOwnPropertyDescriptor(window, 'document') 53 ); 54 assert_equals(trapCalls, 1); 55 }, 'Window target, forwarding trap, [LegacyUnforgeable] "document" attribute'); 56 57 test(() => { 58 const trapResult = {get() {}, set(_val) {}, enumerable: false, configurable: true}; 59 const windowProxy = new Proxy(new Proxy(window, {}), { 60 getOwnPropertyDescriptor: () => trapResult, 61 }); 62 63 assert_accessor_descriptor_equals( 64 Object.getOwnPropertyDescriptor(windowProxy, 'onclick'), 65 trapResult 66 ); 67 }, 'Window proxy target, custom trap, "onclick" event handler attribute'); 68 69 test(() => { 70 let trapCalls = 0; 71 const documentProxy = new Proxy(document, { 72 getOwnPropertyDescriptor(...args) { 73 trapCalls++; 74 return Reflect.getOwnPropertyDescriptor(...args); 75 }, 76 }); 77 78 assert_accessor_descriptor_equals( 79 Object.getOwnPropertyDescriptor(documentProxy, 'location'), 80 Object.getOwnPropertyDescriptor(document, 'location') 81 ); 82 assert_equals(trapCalls, 1); 83 }, 'Document target, forwarding trap, [LegacyUnforgeable] "location" attribute'); 84 85 test(() => { 86 const trapResult = {value: 4, writable: false, enumerable: true, configurable: true}; 87 const documentProxy = new Proxy(new Proxy(document, {}), { 88 getOwnPropertyDescriptor: () => trapResult, 89 }); 90 91 assert_data_descriptor_equals( 92 Object.getOwnPropertyDescriptor(documentProxy, 'foo'), 93 trapResult 94 ); 95 }, 'Document proxy target, custom trap, non-existent value attribute'); 96 97 test(() => { 98 const locationProxy = new Proxy(location, {}); 99 location.hash = '#old'; 100 const descriptor = Object.getOwnPropertyDescriptor(locationProxy, 'hash'); 101 102 assert_equals(descriptor.get.call(location), '#old'); 103 descriptor.set.call(location, '#new'); 104 assert_equals(location.hash, '#new'); 105 assert_true(descriptor.enumerable); 106 assert_false(descriptor.configurable); 107 }, 'Location target, no trap, [LegacyUnforgeable] "hash" attribute'); 108 109 test(() => { 110 let trapCalls = 0; 111 const locationProxy = new Proxy(new Proxy(location, {}), { 112 getOwnPropertyDescriptor(...args) { 113 trapCalls++; 114 return Reflect.getOwnPropertyDescriptor(...args); 115 }, 116 }); 117 118 assert_data_descriptor_equals( 119 Object.getOwnPropertyDescriptor(locationProxy, 'reload'), 120 Object.getOwnPropertyDescriptor(location, 'reload') 121 ); 122 assert_equals(trapCalls, 1); 123 }, 'Location proxy target, forwarding trap, [LegacyUnforgeable] "reload" method'); 124 </script> 125 </body> 126 </html>