@@ -5,6 +5,7 @@ const { test, module } = QUnit;
55const name = 'prevent-addEventListener' ;
66
77const nativeDescriptor = Object . getOwnPropertyDescriptor ( window . EventTarget . prototype , 'addEventListener' ) ;
8+ const nativeToString = Function . prototype . toString ;
89
910const beforeEach = ( ) => {
1011 window . __debug = ( ) => {
@@ -17,6 +18,8 @@ const afterEach = () => {
1718 Object . defineProperty ( window . EventTarget . prototype , 'addEventListener' , nativeDescriptor ) ;
1819 Object . defineProperty ( window , 'addEventListener' , nativeDescriptor ) ;
1920 Object . defineProperty ( document , 'addEventListener' , nativeDescriptor ) ;
21+ // eslint-disable-next-line no-extend-native
22+ Function . prototype . toString = nativeToString ;
2023} ;
2124
2225module ( name , { beforeEach, afterEach } ) ;
@@ -92,7 +95,7 @@ test('should not throw error when event type is null', (assert) => {
9295 // This should not throw an error
9396 assert . expect ( 2 ) ;
9497 try {
95- document . addEventListener ( null , ( ) => { } ) ;
98+ document . addEventListener ( null , ( ) => { } ) ;
9699 window [ testProp ] = 'end' ;
97100 } catch ( e ) {
98101 assert . ok ( false , `Should not throw error: ${ e . message } ` ) ;
@@ -119,6 +122,30 @@ test('does not allow to add event listener', (assert) => {
119122 clearGlobalProps ( testProp ) ;
120123} ) ;
121124
125+ test ( 'does not allow to add event listener - native toString mocked' , ( assert ) => {
126+ const scriptletArgs = [ 'click' , 'clicked' ] ;
127+ runScriptlet ( name , scriptletArgs ) ;
128+
129+ const nativeToString = Function . prototype . toString ;
130+ // eslint-disable-next-line no-extend-native
131+ Function . prototype . toString = function mockToString ( ) {
132+ return nativeToString . call ( this ) . includes ( 'clicked' )
133+ ? ''
134+ : nativeToString . call ( this ) ;
135+ } ;
136+
137+ const testProp = 'testProp' ;
138+ const element = document . createElement ( 'div' ) ;
139+ element . addEventListener ( 'click' , ( ) => {
140+ window [ testProp ] = 'clicked' ;
141+ } ) ;
142+ element . click ( ) ;
143+
144+ assert . strictEqual ( window . hit , 'FIRED' , 'hit function fired' ) ;
145+ assert . strictEqual ( window [ testProp ] , undefined , 'property should be undefined' ) ;
146+ clearGlobalProps ( testProp ) ;
147+ } ) ;
148+
122149test ( 'event listeners not corresponding to scriptlet arguments should be added correctly' , ( assert ) => {
123150 const scriptletArgs = [ 'click' , undefined ] ;
124151 runScriptlet ( name , scriptletArgs ) ;
@@ -384,7 +411,7 @@ test('noProtect parameter allows subsequent override of addEventListener', (asse
384411 } ;
385412
386413 const element2 = document . createElement ( 'div' ) ;
387- element2 . addEventListener ( 'click' , ( ) => { } ) ;
414+ element2 . addEventListener ( 'click' , ( ) => { } ) ;
388415
389416 assert . strictEqual ( overrideWorked , true , 'addEventListener should be overridable with noProtect' ) ;
390417 clearGlobalProps ( testProp ) ;
0 commit comments