How to use addEventListener method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

comparefile.js

Source: comparefile.js Github

copy
1// Detectar el nombre del html
2function filename(){
3    var absoluteRoute = self.location.href;   
4    var positionLastBar = absoluteRoute.lastIndexOf("/");
5    var relativeRoute = absoluteRoute.substring( positionLastBar + "/".length , absoluteRoute.length );
6    return relativeRoute;  
7}
8var file_name = filename();
9// Comparar el nombre para ejecutarse
10switch(file_name){
11    case "calculator.html":
12        btn_addition.addEventListener('click', additionC);
13        btn_substraction.addEventListener('click', substractionC);
14        btn_multiply.addEventListener("click", multiplyC);
15        btn_divide.addEventListener("click", divideC);
16        break;
17    case "imccalculator.html":
18        btn_BMI.addEventListener("click", IMCCalculator);
19        break;
20    case "measures.html":
21        btn_metric.addEventListener("click", measureMeters);
22        break;
23    case "temperatureConversion.html":
24        btn_temperature.addEventListener('click', temperatureConversor);
25        break;
26    case "geometricFigures.html":
27        btn_areaS.addEventListener("click", squareA);
28        btn_areaR.addEventListener("click", rectangleA);
29        btn_areaT.addEventListener("click", triangleA);
30        btn_areaC.addEventListener("click", circleA);
31        btn_perimeterS.addEventListener("click", squareP);
32        btn_perimeterR.addEventListener("click", rectangleP);
33        btn_perimeterT.addEventListener("click", triangleP);
34        btn_perimeterC.addEventListener("click", circleP);
35        btn_radiusC.addEventListener("click", circleR);
36        btn_diameterC.addEventListener("click", circleD);
37        btn_circumferenceC.addEventListener("click", circleC);
38        break;
39    case "mru.html":
40        btn_timeMRU.addEventListener("click", mruTime);
41        btn_distMRU.addEventListener("click", mruDistance);
42        btn_dispMRU.addEventListener("click", mruDisplacement);
43        btn_speedMRU.addEventListener("click", mruSpeed);
44        btn_averagespeedMRU.addEventListener("click", mruAverageS);
45        break;
46    case "interest.html":
47        btn_composedI.addEventListener("click", composedInterest);
48        btn_simpleI.addEventListener("click", simpleInterest);
49        break;
50    case "equationscndgrade.html":
51        btn_eqscndgrade.addEventListener("click", eqscndgrade);
52        break;
53    case "undolatory.html":
54        btn_propagationsW.addEventListener("click", propagationsWave);
55        btn_lineardW.addEventListener("click", lineardWave);
56        btn_keystrokefW.addEventListener("click", keystrokefWave);
57        btn_keystroketW.addEventListener("click", keystrokepWave);
58        btn_periodwW.addEventListener("click", periodWave);
59        btn_frequencywW.addEventListener("click", frequencyWave);
60        btn_wavelenghtW.addEventListener("click", wavelengthWave)
61        break;
62    case "contactforce.html":
63        btn_resultingforcefrCF.addEventListener("click", resultingforcefrCF);
64        btn_resultingforcemaCF.addEventListener("click", resultingforcemaCF);
65        btn_resultingforceaCF.addEventListener("click", resultingforceaCF)
66        break;
67    case "conversormass.html":
68        btn_conversionMass.addEventListener("click", measureGrams)
69        break;
70    case "energies.html":
71        btn_potentialE.addEventListener("click", potentialEnergy);
72        btn_kinecticE.addEventListener("click", kinecticEnergy);
73        btn_mechanicalE.addEventListener("click", mechanicalEnergy);
74        break;
75    case "heattransferred.html":
76        btn_heattHT.addEventListener("click", heattransferredHT);
77        btn_massHT.addEventListener("click", massHT);
78        btn_specifichHT.addEventListener("click", specificheatHT);
79        btn_temperaturevhmsHT.addEventListener("click", hmstemperatureVariation);
80        btn_temperaturevtf.addEventListener("click", tftemperatureVariation);
81        break;
82    case "volumeconversor.html":
83        btn_conversionVolume.addEventListener("click", convertVolume);
84        break;
85    case "pythagorastheorem.html":
86        btn_hypothenusePT.addEventListener("click", hypothenusePT);
87        btn_rednackaPT.addEventListener("click", rednackaPT);
88        btn_rednackbPT.addEventListener("click", rednackbPT);
89        break;
90    case "binariesnumbers.html":
91        btn_textToBinaries.addEventListener("click", textToBinaries);
92        break;
93    case "asciinumbers.html":
94        btn_textToAscii.addEventListener("click", textToAscii);
95        break;
96    case "kdr.html":
97        btn_calculateKDR.addEventListener("click", calculateKdr);
98        break;
99}
Full Screen

menu.js

Source: menu.js Github

copy
1//popup menu button
2
3document.getElementById("menu").addEventListener("click", function(){
4    document.querySelector(".popupmenu").style.display = "flex";
5})
6
7document.getElementById("menu").addEventListener("click", function(){
8    document.querySelector(".menu").style.display = "none";
9})
10
11//close button
12
13document.querySelector(".closemenu").addEventListener("click", function(){
14    document.querySelector(".popupmenu").style.display = "none";
15})
16
17//reveal menu again hehe
18document.querySelector(".closemenu").addEventListener("click", function(){
19    document.querySelector(".menu").style.display = "flex";
20})
21
22//blur nanaman dami neto
23
24document.getElementById("menu").addEventListener("click", function(){
25    document.querySelector(".mainrec").style.filter = "blur(4px)";
26})
27
28document.getElementById("menu").addEventListener("click", function(){
29    document.querySelector(".recipes").style.filter = "blur(4px)";
30})
31
32document.getElementById("menu").addEventListener("click", function(){
33    document.querySelector(".origin").style.filter = "blur(4px)";
34})
35
36document.getElementById("menu").addEventListener("click", function(){
37    document.querySelector(".bc").style.filter = "blur(4px)";
38})
39
40document.getElementById("menu").addEventListener("click", function(){
41    document.querySelector(".bct").style.filter = "blur(4px)";
42})
43
44document.getElementById("menu").addEventListener("click", function(){
45    document.querySelector(".mainrec1").style.filter = "blur(4px)";
46})
47
48document.getElementById("menu").addEventListener("click", function(){
49    document.querySelector(".mainell").style.filter = "blur(4px)";
50})
51
52document.getElementById("menu").addEventListener("click", function(){
53    document.querySelector(".cat1").style.filter = "blur(4px)";
54})
55
56document.getElementById("menu").addEventListener("click", function(){
57    document.querySelector(".maincapulus").style.filter = "blur(4px)";
58})
59
60document.getElementById("menu").addEventListener("click", function(){
61    document.querySelector(".home").style.filter = "blur(4px)";
62})
63
64document.getElementById("menu").addEventListener("click", function(){
65    document.querySelector(".coffeebg").style.filter = "blur(4px)";
66})
67
68document.getElementById("menu").addEventListener("click", function(){
69    document.querySelector(".cover").style.filter = "blur(4px)";
70})
71
72document.getElementById("menu").addEventListener("click", function(){
73    document.querySelector(".owl").style.filter = "blur(4px)";
74})
75
76document.getElementById("menu").addEventListener("click", function(){
77    document.querySelector(".poison").style.filter = "blur(4px)";
78})
79
80document.getElementById("menu").addEventListener("click", function(){
81    document.querySelector(".coffeerec").style.filter = "blur(4px)";
82})
83
84//eto naman tanggal nanaman ng pakeng blur dami nanaman shet
85
86document.querySelector(".closemenu").addEventListener("click", function(){
87    document.querySelector(".mainrec").style.filter = "none";
88})
89
90document.querySelector(".closemenu").addEventListener("click", function(){
91    document.querySelector(".origin").style.filter = "none";
92})
93
94document.querySelector(".closemenu").addEventListener("click", function(){
95    document.querySelector(".recipes").style.filter = "none";
96})
97
98document.querySelector(".closemenu").addEventListener("click", function(){
99    document.querySelector(".bc").style.filter = "none";
100})
101
102document.querySelector(".closemenu").addEventListener("click", function(){
103    document.querySelector(".bct").style.filter = "none";
104})
105
106document.querySelector(".closemenu").addEventListener("click", function(){
107    document.querySelector(".mainrec1").style.filter = "none";
108})
109
110document.querySelector(".closemenu").addEventListener("click", function(){
111    document.querySelector(".mainell").style.filter = "none";
112})
113
114document.querySelector(".closemenu").addEventListener("click", function(){
115    document.querySelector(".cat1").style.filter = "none";
116})
117
118document.querySelector(".closemenu").addEventListener("click", function(){
119    document.querySelector(".maincapulus").style.filter = "none";
120})
121
122document.querySelector(".closemenu").addEventListener("click", function(){
123    document.querySelector(".home").style.filter = "none";
124})
125
126document.querySelector(".closemenu").addEventListener("click", function(){
127    document.querySelector(".coffeebg").style.filter = "none";
128})
129
130document.querySelector(".closemenu").addEventListener("click", function(){
131    document.querySelector(".cover").style.filter = "none";
132})
133
134document.querySelector(".closemenu").addEventListener("click", function(){
135    document.querySelector(".owl").style.filter = "none";
136})
137
138document.querySelector(".closemenu").addEventListener("click", function(){
139    document.querySelector(".poison").style.filter = "none";
140})
141
142document.querySelector(".closemenu").addEventListener("click", function(){
143    document.querySelector(".coffeerec").style.filter = "none";
144})
145
146// hide ulet
147
148window.addEventListener('mouseup', function(event){
149    var hidediv = document.querySelector(".popupmenu");
150    if(event.target != hidediv){
151        hidediv.style.display = 'none';
152    }
153})  
154
155// haha tanggal blur nanaman nanman dami ko kase alam shet haha ser pasado na pls ty
156
157window.addEventListener('mouseup', function(event){
158    var hidediv = document.querySelector(".mainrec");
159    if(event.target != hidediv){
160        hidediv.style.filter = 'none';
161    }
162})
163
164window.addEventListener('mouseup', function(event){
165    var hidediv = document.querySelector(".recipes");
166    if(event.target != hidediv){
167        hidediv.style.filter = 'none';
168    }
169})
170
171window.addEventListener('mouseup', function(event){
172    var hidediv = document.querySelector(".origin");
173    if(event.target != hidediv){
174        hidediv.style.filter = 'none';
175    }
176})
177
178window.addEventListener('mouseup', function(event){
179    var hidediv = document.querySelector(".bc");
180    if(event.target != hidediv){
181        hidediv.style.filter = 'none';
182    }
183})
184
185window.addEventListener('mouseup', function(event){
186    var hidediv = document.querySelector(".bct");
187    if(event.target != hidediv){
188        hidediv.style.filter = 'none';
189    }
190})
191
192window.addEventListener('mouseup', function(event){
193    var hidediv = document.querySelector(".mainrec1");
194    if(event.target != hidediv){
195        hidediv.style.filter = 'none';
196    }
197})
198
199window.addEventListener('mouseup', function(event){
200    var hidediv = document.querySelector(".cat1");
201    if(event.target != hidediv){
202        hidediv.style.filter = 'none';
203    }
204})
205
206window.addEventListener('mouseup', function(event){
207    var hidediv = document.querySelector(".mainell");
208    if(event.target != hidediv){
209        hidediv.style.filter = 'none';
210    }
211})
212
213window.addEventListener('mouseup', function(event){
214    var hidediv = document.querySelector(".maincapulus");
215    if(event.target != hidediv){
216        hidediv.style.filter = 'none';
217    }
218})
219
220window.addEventListener('mouseup', function(event){
221    var hidediv = document.querySelector(".home");
222    if(event.target != hidediv){
223        hidediv.style.filter = 'none';
224    }
225})
226
227window.addEventListener('mouseup', function(event){
228    var hidediv = document.querySelector(".coffeebg");
229    if(event.target != hidediv){
230        hidediv.style.filter = 'none';
231    }
232})
233
234window.addEventListener('mouseup', function(event){
235    var hidediv = document.querySelector(".cover");
236    if(event.target != hidediv){
237        hidediv.style.filter = 'none';
238    }
239})
240
241window.addEventListener('mouseup', function(event){
242    var hidediv = document.querySelector(".owl");
243    if(event.target != hidediv){
244        hidediv.style.filter = 'none';
245    }
246})
247
248window.addEventListener('mouseup', function(event){
249    var hidediv = document.querySelector(".poison");
250    if(event.target != hidediv){
251        hidediv.style.filter = 'none';
252    }
253})
254
255// eto para lumabas ulet menu nawawala kasi amp
256window.addEventListener('mouseup', function(event){
257    var hidediv = document.querySelector(".menu");
258    if(event.target != hidediv){
259        hidediv.style.display = 'flex';
260    }
261})
262
263
264
265
266
267
Full Screen

view.js

Source: view.js Github

copy
1/* global $, cy */
2
3(function(){
4
5	$("#zoom-pan-button").addEventListener('click', function(){
6		cy.reset();
7	});
8
9	$("#fit-button").addEventListener('click', function(){
10		cy.fit();
11	});
12
13	$("#fit-selected-button").addEventListener('click', function(){
14		cy.fit( cy.elements(":selected") );
15	});
16
17	$("#center-selected-button").addEventListener('click', function(){
18		cy.center( cy.elements(":selected") );
19	});
20
21	$("#enable-panning").addEventListener('click', function(){
22		cy.panningEnabled(true);
23	});
24
25	$("#disable-panning").addEventListener('click', function(){
26		cy.panningEnabled(false);
27	});
28
29	$("#enable-user-panning").addEventListener('click', function(){
30		cy.userPanningEnabled(true);
31	});
32
33	$("#disable-user-panning").addEventListener('click', function(){
34		cy.userPanningEnabled(false);
35	});
36
37	$("#enable-zooming").addEventListener('click', function(){
38		cy.zoomingEnabled(true);
39	});
40
41	$("#disable-zooming").addEventListener('click', function(){
42		cy.zoomingEnabled(false);
43	});
44
45	$("#enable-user-zooming").addEventListener('click', function(){
46		cy.userZoomingEnabled(true);
47	});
48
49	$("#disable-user-zooming").addEventListener('click', function(){
50		cy.userZoomingEnabled(false);
51	});
52
53	$("#enable-autolock").addEventListener('click', function(){
54		cy.autolock(true);
55	});
56
57	$("#disable-autolock").addEventListener('click', function(){
58		cy.autolock(false);
59	});
60
61	$("#enable-autoungrabify").addEventListener('click', function(){
62		cy.autoungrabify(true);
63	});
64
65	$("#disable-autoungrabify").addEventListener('click', function(){
66		cy.autoungrabify(false);
67	});
68
69	$("#enable-autounselectify").addEventListener('click', function(){
70		cy.autounselectify(true);
71	});
72
73	$("#disable-autounselectify").addEventListener('click', function(){
74		cy.autounselectify(false);
75	});
76
77	$("#show-debug").addEventListener('click', function(){
78		cy.renderer().debug = true;
79
80		// force redraws
81		cy.panBy({ x: 1 });
82		cy.panBy({ x: -1 });
83	});
84
85	$("#hide-debug").addEventListener('click', function(){
86		cy.renderer().debug = false;
87
88		// force redraws
89		cy.panBy({ x: 1 });
90		cy.panBy({ x: -1 });
91	});
92
93	var showBB = function( eles ){
94		var bb = eles.renderedBoundingBox();
95
96		var $bb = $('#bb');
97
98		var style = {
99			left: bb.x1 + 'px',
100			top: bb.y1 + 'px',
101			width: (bb.x2 - bb.x1) + 'px',
102			height: (bb.y2 - bb.y1) + 'px',
103			display: 'block'
104		};
105
106		Object.keys( style ).forEach(function( key ){
107			var val = style[key];
108
109			$bb.style[ key ] = val;
110		});
111	};
112
113	$("#show-bb").addEventListener('click', function(){
114		var eles = cy.$(':selected');
115
116		if( eles.length === 0 ){
117			eles = cy.elements();
118		}
119
120		showBB( eles );
121	});
122
123	$("#hide-bb").addEventListener('click', function(){
124		$('#bb').style.display = 'none';
125	});
126
127	$('#mount').addEventListener('click', function(){
128		cy.mount( $('#cytoscape') );
129	});
130
131	$('#unmount').addEventListener('click', function(){
132		cy.unmount();
133	});
134})();
135
Full Screen

events.js

Source: events.js Github

copy
1
2// window.onload = function() {
3
4//     let block = document.querySelector('.block');
5
6
7//     // DOM Level 0
8//     // Add Event Handler
9//     // block.onclick = function() {
10//     //     console.log('click');
11//     // }
12
13//     block.onmouseover = function() {
14//         // console.log('mouseover', this);
15//         // f1();
16//         // f2();
17//     }
18
19//     // Remove Event handler
20//     setTimeout(function() {
21//         block.onmouseover = null;
22//     }, 2000)
23
24//     console.dir(block)
25
26
27//     // DOM Level 2
28//     // Add Event Handler
29//     block.addEventListener('click', function() {
30//         console.log('click');
31//     });
32
33//     block.addEventListener('mouseover', f1);
34//     block.addEventListener('mouseover', f2);
35
36//     // Remove Event handler
37//     block.removeEventListener('mouseover', f1);
38//     block.removeEventListener('mouseover', f2);
39// }
40
41
42// function f1() {
43//     console.log('mouseover 1', this);
44// }
45// function f2() {
46//     console.log('mouseover 2', this);
47// }
48
49
50// -----------------------------------------
51
52
53window.addEventListener('load', function() {
54
55    let block = document.querySelector('.block');
56    let textarea = document.querySelector('.textarea');
57
58    // Mouse events
59
60    // block.addEventListener('mouseover', handler)
61    // block.addEventListener('mouseout', handler)
62    // block.addEventListener('mousemove', handler)
63
64    // block.addEventListener('mouseenter', handler)
65    // block.addEventListener('mouseleave', handler)
66
67    // block.addEventListener('click', handler);
68    // block.addEventListener('dblclick', handler);
69    // block.addEventListener('contextmenu', handler);
70
71    // block.addEventListener('mousedown', handler);
72    // block.addEventListener('mouseup', handler);
73
74
75    // Keyboard Events
76    // textarea.addEventListener('keydown', handler);
77    // textarea.addEventListener('keypress', handler);
78    // textarea.addEventListener('keyup', handler);
79
80    // Input events
81    // textarea.addEventListener('input', handler);
82    // textarea.addEventListener('change', handler);
83
84    // Focus Events
85    // textarea.addEventListener('focus', handler);
86    // textarea.addEventListener('blur', handler);
87
88    // ---------------------------------------
89    // let checkbox = document.querySelector('#checkbox');
90    // let select = document.querySelector('#select');
91    
92    // checkbox.addEventListener('change', handler)
93    // checkbox.addEventListener('input', handler)
94    // checkbox.addEventListener('focus', handler)
95    // checkbox.addEventListener('blur', handler)
96
97    // select.addEventListener('change', handler)
98    // select.addEventListener('input', handler)
99    // select.addEventListener('focus', handler)
100    // select.addEventListener('blur', handler)
101    // select.addEventListener('keydown', handler)
102
103})
104
105// function handler(event) {
106//     if (event.shiftKey) {
107//         return;
108//     }
109//     console.log(event.type, event);
110// }
111
112
113// ----------------------------
114
115let step = 5;
116
117window.addEventListener('load', function(){
118    let block = document.querySelector('.block');
119
120    document.addEventListener('keydown', function(event){
121        const target = block;
122        if (event.keyCode === 37) {
123            console.log('left');
124            target.style.left = target.offsetLeft - step + 'px';
125        } else if (event.keyCode === 38) {
126            console.log('top');
127            target.style.top = target.offsetTop - step + 'px';
128        } else if (event.keyCode === 39) {
129            console.log('right');
130            target.style.left = target.offsetLeft + step + 'px';
131        } else if (event.keyCode === 40) {
132            console.log('bottom');
133            target.style.top = target.offsetTop + step + 'px';
134        } 
135    })
136
137
138    let catched = false;
139
140    setInterval(() => {
141        if(catched) {
142            return;
143        }
144        let x = window.innerWidth - block.offsetWidth;
145        let y = window.innerHeight - block.offsetHeight;
146
147        block.style.left = getRand(x) + 'px';
148        block.style.top = getRand(y) + 'px';
149        block.style.backgroundColor = 'rgb('+getRand(256)+', '+getRand(256)+', '+getRand(256)+')';
150    }, Math.floor(Math.random()*200 + 100));
151
152    block.addEventListener('mousedown', function() {
153        catched = true;
154    })
155
156    block.addEventListener('mouseup', function() {
157        catched = false;
158    })
159
160    function getRand(max) {
161        return Math.floor(Math.random()*max);
162    }
163})
164
Full Screen

EventSpec.js

Source: EventSpec.js Github

copy
1import { Event } from "../../Source/Cesium.js";
2
3describe("Core/Event", function () {
4  var event;
5  var spyListener;
6  beforeEach(function () {
7    event = new Event();
8    spyListener = jasmine.createSpy("listener");
9  });
10
11  it("works with no scope", function () {
12    var someValue = 123;
13
14    event.addEventListener(spyListener);
15    event.raiseEvent(someValue);
16
17    expect(spyListener).toHaveBeenCalledWith(someValue);
18
19    spyListener.calls.reset();
20
21    event.removeEventListener(spyListener);
22    event.raiseEvent(someValue);
23
24    expect(spyListener).not.toHaveBeenCalled();
25  });
26
27  it("works with scope", function () {
28    var someValue = 123;
29    var scope = {};
30
31    event.addEventListener(spyListener, scope);
32    event.raiseEvent(someValue);
33
34    expect(spyListener).toHaveBeenCalledWith(someValue);
35    expect(spyListener.calls.first().object).toBe(scope);
36
37    spyListener.calls.reset();
38
39    event.removeEventListener(spyListener, scope);
40    event.raiseEvent(someValue);
41
42    expect(spyListener).not.toHaveBeenCalled();
43  });
44
45  it("can remove from within a callback", function () {
46    var doNothing = function (evt) {};
47
48    var removeEventCb = function (evt) {
49      event.removeEventListener(removeEventCb);
50    };
51
52    var doNothing2 = function (evt) {};
53
54    event.addEventListener(doNothing);
55    event.addEventListener(removeEventCb);
56    event.addEventListener(doNothing2);
57    event.raiseEvent();
58    expect(event.numberOfListeners).toEqual(2);
59
60    event.removeEventListener(doNothing);
61    event.removeEventListener(doNothing2);
62    expect(event.numberOfListeners).toEqual(0);
63  });
64
65  it("can remove multiple listeners within a callback", function () {
66    var removeEvent0 = event.addEventListener(function () {
67      removeEvent0();
68    });
69    event.addEventListener(function () {});
70    var removeEvent2 = event.addEventListener(function () {
71      removeEvent2();
72    });
73    event.addEventListener(function () {});
74    var removeEvent4 = event.addEventListener(function () {
75      removeEvent4();
76    });
77    event.addEventListener(function () {});
78    var removeEvent6 = event.addEventListener(function () {
79      removeEvent6();
80    });
81    event.addEventListener(function () {});
82    var removeEvent8 = event.addEventListener(function () {
83      removeEvent8();
84    });
85    event.addEventListener(function () {});
86
87    expect(event.numberOfListeners).toEqual(10);
88    event.raiseEvent();
89    expect(event.numberOfListeners).toEqual(5);
90    event.raiseEvent();
91    expect(event.numberOfListeners).toEqual(5);
92  });
93
94  it("addEventListener and removeEventListener works with same function of different scopes", function () {
95    var Scope = function () {
96      this.timesCalled = 0;
97    };
98
99    Scope.prototype.myCallback = function () {
100      this.timesCalled++;
101    };
102
103    var scope1 = new Scope();
104    var scope2 = new Scope();
105
106    event.addEventListener(Scope.prototype.myCallback, scope1);
107    event.addEventListener(Scope.prototype.myCallback, scope2);
108    event.raiseEvent();
109
110    expect(scope1.timesCalled).toEqual(1);
111    expect(scope2.timesCalled).toEqual(1);
112
113    event.removeEventListener(Scope.prototype.myCallback, scope1);
114    expect(event.numberOfListeners).toEqual(1);
115    event.raiseEvent();
116
117    expect(scope1.timesCalled).toEqual(1);
118    expect(scope2.timesCalled).toEqual(2);
119
120    event.removeEventListener(Scope.prototype.myCallback, scope2);
121    expect(event.numberOfListeners).toEqual(0);
122  });
123
124  it("numberOfListeners returns the correct number", function () {
125    var callback1 = function () {};
126
127    var callback2 = function () {};
128
129    expect(event.numberOfListeners).toEqual(0);
130
131    event.addEventListener(callback1);
132    expect(event.numberOfListeners).toEqual(1);
133
134    event.addEventListener(callback2);
135    expect(event.numberOfListeners).toEqual(2);
136
137    event.removeEventListener(callback2);
138    expect(event.numberOfListeners).toEqual(1);
139  });
140
141  it("removeEventListener indicates if the listener is registered with the event", function () {
142    var callback = function () {};
143
144    event.addEventListener(callback);
145    expect(event.numberOfListeners).toEqual(1);
146
147    expect(event.removeEventListener(callback)).toEqual(true);
148    expect(event.numberOfListeners).toEqual(0);
149
150    expect(event.removeEventListener(callback)).toEqual(false);
151  });
152
153  it("removeEventListener does not remove a registered listener of a different scope", function () {
154    var myFunc = function () {};
155    var scope = {};
156    event.addEventListener(myFunc, scope);
157    expect(event.removeEventListener(myFunc)).toEqual(false);
158  });
159
160  it("works with no listeners", function () {
161    event.raiseEvent(123);
162  });
163
164  it("addEventListener returns a function allowing removal", function () {
165    var someValue = 123;
166
167    var remove = event.addEventListener(spyListener);
168    event.raiseEvent(someValue);
169
170    expect(spyListener).toHaveBeenCalledWith(someValue);
171    spyListener.calls.reset();
172
173    remove();
174    event.raiseEvent(someValue);
175
176    expect(spyListener).not.toHaveBeenCalled();
177  });
178
179  it("addEventListener with scope returns a function allowing removal", function () {
180    var someValue = 123;
181    var scope = {};
182
183    var remove = event.addEventListener(spyListener, scope);
184    event.raiseEvent(someValue);
185
186    expect(spyListener).toHaveBeenCalledWith(someValue);
187    spyListener.calls.reset();
188
189    remove();
190    event.raiseEvent(someValue);
191
192    expect(spyListener).not.toHaveBeenCalled();
193  });
194
195  it("addEventListener throws with undefined listener", function () {
196    expect(function () {
197      event.addEventListener(undefined);
198    }).toThrowDeveloperError();
199  });
200
201  it("addEventListener throws with null listener", function () {
202    expect(function () {
203      event.addEventListener(null);
204    }).toThrowDeveloperError();
205  });
206
207  it("addEventListener throws with non-function listener", function () {
208    expect(function () {
209      event.addEventListener({});
210    }).toThrowDeveloperError();
211  });
212
213  it("removeEventListener throws with undefined listener", function () {
214    expect(function () {
215      event.removeEventListener(undefined);
216    }).toThrowDeveloperError();
217  });
218
219  it("removeEventListener throws with null listener", function () {
220    expect(function () {
221      event.removeEventListener(null);
222    }).toThrowDeveloperError();
223  });
224});
225
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)