@@ -83,3 +83,50 @@ const {
83
83
document . dispatchEvent ( new Event ( 'test' ) ) ;
84
84
strictEqual ( invoked_count , 2 , 'Once handler should only be invoked once' ) ;
85
85
}
86
+
87
+ // Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-once.html
88
+ // in order to define the `document` ourselves
89
+
90
+ {
91
+ const document = new EventTarget ( ) ;
92
+
93
+ // Should only fire for first event
94
+ document . addEventListener ( 'test' , common . mustCall ( 1 ) , { once : true } ) ;
95
+ // Should fire for both events
96
+ document . addEventListener ( 'test' , common . mustCall ( 2 ) ) ;
97
+ // Fire events
98
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
99
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
100
+ }
101
+ {
102
+ const document = new EventTarget ( ) ;
103
+
104
+ const handler = common . mustCall ( 2 ) ;
105
+ // Both should only fire on first event
106
+ document . addEventListener ( 'test' , handler . bind ( ) , { once : true } ) ;
107
+ document . addEventListener ( 'test' , handler . bind ( ) , { once : true } ) ;
108
+ // Fire events
109
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
110
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
111
+ }
112
+ {
113
+ const document = new EventTarget ( ) ;
114
+
115
+ const handler = common . mustCall ( 2 ) ;
116
+
117
+ // Should only fire once on first event
118
+ document . addEventListener ( 'test' , common . mustCall ( 1 ) , { once : true } ) ;
119
+ // Should fire twice until removed
120
+ document . addEventListener ( 'test' , handler ) ;
121
+ // Fire two events
122
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
123
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
124
+
125
+ // Should only fire once on the next event
126
+ document . addEventListener ( 'test' , common . mustCall ( 1 ) , { once : true } ) ;
127
+ // The previous handler should no longer fire
128
+ document . removeEventListener ( 'test' , handler ) ;
129
+
130
+ // Fire final event triggering
131
+ document . dispatchEvent ( new Event ( 'test' ) ) ;
132
+ }
0 commit comments