@@ -11,7 +11,7 @@ const fs = require('fs');
11
11
const { URL } = require ( 'url' ) ;
12
12
13
13
const WebSocket = require ( '..' ) ;
14
- const { EMPTY_BUFFER , GUID , NOOP } = require ( '../lib/constants' ) ;
14
+ const { EMPTY_BUFFER , GUID , kListener , NOOP } = require ( '../lib/constants' ) ;
15
15
16
16
class CustomAgent extends http . Agent {
17
17
addRequest ( ) { }
@@ -2157,88 +2157,82 @@ describe('WebSocket', () => {
2157
2157
2158
2158
assert . strictEqual ( listeners . length , 2 ) ;
2159
2159
assert . strictEqual ( listeners [ 0 ] , NOOP ) ;
2160
- assert . strictEqual ( listeners [ 1 ] . _listener , NOOP ) ;
2160
+ assert . strictEqual ( listeners [ 1 ] [ kListener ] , NOOP ) ;
2161
2161
2162
2162
ws . onclose = NOOP ;
2163
2163
2164
2164
listeners = ws . listeners ( 'close' ) ;
2165
2165
2166
2166
assert . strictEqual ( listeners . length , 2 ) ;
2167
2167
assert . strictEqual ( listeners [ 0 ] , NOOP ) ;
2168
- assert . strictEqual ( listeners [ 1 ] . _listener , NOOP ) ;
2168
+ assert . strictEqual ( listeners [ 1 ] [ kListener ] , NOOP ) ;
2169
2169
} ) ;
2170
2170
2171
- it ( 'adds listeners for custom events with `addEventListener`' , ( ) => {
2171
+ it ( 'supports the `addEventListener` method' , ( ) => {
2172
+ const events = [ ] ;
2172
2173
const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
2173
2174
2174
- ws . addEventListener ( 'foo' , NOOP ) ;
2175
- assert . strictEqual ( ws . listeners ( 'foo' ) [ 0 ] , NOOP ) ;
2175
+ ws . addEventListener ( 'foo' , ( ) => { } ) ;
2176
+ assert . strictEqual ( ws . listenerCount ( 'foo' ) , 0 ) ;
2176
2177
2177
- //
2178
- // Fails silently when the `listener` is not a function.
2179
- //
2180
- ws . addEventListener ( 'bar' , { } ) ;
2181
- assert . strictEqual ( ws . listeners ( 'bar' ) . length , 0 ) ;
2182
- } ) ;
2178
+ ws . addEventListener ( 'open' , ( ) => {
2179
+ events . push ( 'open' ) ;
2180
+ assert . strictEqual ( ws . listenerCount ( 'open' ) , 1 ) ;
2181
+ } ) ;
2183
2182
2184
- it ( 'allows to add one time listeners with `addEventListener`' , ( done ) => {
2185
- const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
2183
+ assert . strictEqual ( ws . listenerCount ( 'open' ) , 1 ) ;
2186
2184
2187
2185
ws . addEventListener (
2188
- 'foo ' ,
2186
+ 'message ' ,
2189
2187
( ) => {
2190
- assert . strictEqual ( ws . listenerCount ( 'foo' ) , 0 ) ;
2191
- done ( ) ;
2188
+ events . push ( 'message' ) ;
2189
+ assert . strictEqual ( ws . listenerCount ( 'message' ) , 0 ) ;
2192
2190
} ,
2193
2191
{ once : true }
2194
2192
) ;
2195
2193
2196
- assert . strictEqual ( ws . listenerCount ( 'foo' ) , 1 ) ;
2197
- ws . emit ( 'foo' ) ;
2194
+ assert . strictEqual ( ws . listenerCount ( 'message' ) , 1 ) ;
2195
+
2196
+ ws . emit ( 'open' ) ;
2197
+ ws . emit ( 'message' , EMPTY_BUFFER , false ) ;
2198
+
2199
+ assert . deepStrictEqual ( events , [ 'open' , 'message' ] ) ;
2198
2200
} ) ;
2199
2201
2200
2202
it ( 'supports the `removeEventListener` method' , ( ) => {
2201
2203
const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
2202
2204
2203
2205
ws . addEventListener ( 'message' , NOOP ) ;
2204
2206
ws . addEventListener ( 'open' , NOOP ) ;
2205
- ws . addEventListener ( 'foo' , NOOP ) ;
2206
2207
2207
- assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] . _listener , NOOP ) ;
2208
- assert . strictEqual ( ws . listeners ( 'open' ) [ 0 ] . _listener , NOOP ) ;
2209
- assert . strictEqual ( ws . listeners ( 'foo' ) [ 0 ] , NOOP ) ;
2208
+ assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] [ kListener ] , NOOP ) ;
2209
+ assert . strictEqual ( ws . listeners ( 'open' ) [ 0 ] [ kListener ] , NOOP ) ;
2210
2210
2211
2211
ws . removeEventListener ( 'message' , ( ) => { } ) ;
2212
2212
2213
- assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] . _listener , NOOP ) ;
2213
+ assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] [ kListener ] , NOOP ) ;
2214
2214
2215
2215
ws . removeEventListener ( 'message' , NOOP ) ;
2216
2216
ws . removeEventListener ( 'open' , NOOP ) ;
2217
- ws . removeEventListener ( 'foo' , NOOP ) ;
2218
2217
2219
2218
assert . strictEqual ( ws . listenerCount ( 'message' ) , 0 ) ;
2220
2219
assert . strictEqual ( ws . listenerCount ( 'open' ) , 0 ) ;
2221
- assert . strictEqual ( ws . listenerCount ( 'foo' ) , 0 ) ;
2222
2220
2223
2221
ws . addEventListener ( 'message' , NOOP , { once : true } ) ;
2224
2222
ws . addEventListener ( 'open' , NOOP , { once : true } ) ;
2225
- ws . addEventListener ( 'foo' , NOOP , { once : true } ) ;
2226
2223
2227
- assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] . _listener , NOOP ) ;
2228
- assert . strictEqual ( ws . listeners ( 'open' ) [ 0 ] . _listener , NOOP ) ;
2229
- assert . strictEqual ( ws . listeners ( 'foo' ) [ 0 ] , NOOP ) ;
2224
+ assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] [ kListener ] , NOOP ) ;
2225
+ assert . strictEqual ( ws . listeners ( 'open' ) [ 0 ] [ kListener ] , NOOP ) ;
2230
2226
2231
2227
ws . removeEventListener ( 'message' , ( ) => { } ) ;
2232
2228
2233
- assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] . _listener , NOOP ) ;
2229
+ assert . strictEqual ( ws . listeners ( 'message' ) [ 0 ] [ kListener ] , NOOP ) ;
2234
2230
2235
2231
ws . removeEventListener ( 'message' , NOOP ) ;
2236
2232
ws . removeEventListener ( 'open' , NOOP ) ;
2237
- ws . removeEventListener ( 'foo' , NOOP ) ;
2238
2233
2239
2234
assert . strictEqual ( ws . listenerCount ( 'message' ) , 0 ) ;
2240
2235
assert . strictEqual ( ws . listenerCount ( 'open' ) , 0 ) ;
2241
- assert . strictEqual ( ws . listenerCount ( 'foo' ) , 0 ) ;
2242
2236
} ) ;
2243
2237
2244
2238
it ( 'wraps text data in a `MessageEvent`' , ( done ) => {
0 commit comments