-
-
Notifications
You must be signed in to change notification settings - Fork 758
Expand file tree
/
Copy pathevents.js
More file actions
208 lines (167 loc) · 5.89 KB
/
events.js
File metadata and controls
208 lines (167 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
const { test, describe, after } = require('node:test')
const { WebSocketServer } = require('ws')
const { MessageEvent, CloseEvent, ErrorEvent } = require('../../lib/web/websocket/events')
const { WebSocket } = require('../..')
test('MessageEvent', (t) => {
t.assert.throws(() => new MessageEvent(), TypeError, 'no arguments')
t.assert.throws(() => new MessageEvent('').initMessageEvent(), TypeError)
const noInitEvent = new MessageEvent('message')
t.assert.strictEqual(noInitEvent.origin, '')
t.assert.strictEqual(noInitEvent.data, null)
t.assert.strictEqual(noInitEvent.lastEventId, '')
t.assert.strictEqual(noInitEvent.source, null)
t.assert.ok(Array.isArray(noInitEvent.ports))
t.assert.ok(Object.isFrozen(noInitEvent.ports))
t.assert.ok(new MessageEvent('').initMessageEvent('message') instanceof MessageEvent)
})
test('CloseEvent', (t) => {
t.assert.throws(() => new CloseEvent(), TypeError)
const noInitEvent = new CloseEvent('close')
t.assert.strictEqual(noInitEvent.wasClean, false)
t.assert.strictEqual(noInitEvent.code, 0)
t.assert.strictEqual(noInitEvent.reason, '')
})
test('ErrorEvent', (t) => {
t.assert.throws(() => new ErrorEvent(), TypeError)
const noInitEvent = new ErrorEvent('error')
t.assert.strictEqual(noInitEvent.message, '')
t.assert.strictEqual(noInitEvent.filename, '')
t.assert.strictEqual(noInitEvent.lineno, 0)
t.assert.strictEqual(noInitEvent.colno, 0)
t.assert.strictEqual(noInitEvent.error, undefined)
})
describe('Event handlers', () => {
const server = new WebSocketServer({ port: 0 })
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
after(() => {
server.close()
ws.close()
})
function listen () {}
describe('onopen', () => {
test('should be null initially', (t) => {
t.assert.strictEqual(ws.onopen, null)
})
test('should not allow non-function assignments', (t) => {
ws.onopen = 3
t.assert.strictEqual(ws.onopen, null)
})
test('should allow function assignments', (t) => {
ws.onopen = listen
t.assert.strictEqual(ws.onopen, listen)
})
})
describe('onerror', () => {
test('should be null initially', (t) => {
t.assert.strictEqual(ws.onerror, null)
})
test('should not allow non-function assignments', (t) => {
ws.onerror = 3
t.assert.strictEqual(ws.onerror, null)
})
test('should allow function assignments', (t) => {
ws.onerror = listen
t.assert.strictEqual(ws.onerror, listen)
})
})
describe('onclose', () => {
test('should be null initially', (t) => {
t.assert.strictEqual(ws.onclose, null)
})
test('should not allow non-function assignments', (t) => {
ws.onclose = 3
t.assert.strictEqual(ws.onclose, null)
})
test('should allow function assignments', (t) => {
ws.onclose = listen
t.assert.strictEqual(ws.onclose, listen)
})
})
describe('onmessage', () => {
test('should be null initially', (t) => {
t.assert.strictEqual(ws.onmessage, null)
})
test('should not allow non-function assignments', (t) => {
ws.onmessage = 3
t.assert.strictEqual(ws.onmessage, null)
})
test('should allow function assignments', (t) => {
ws.onmessage = listen
t.assert.strictEqual(ws.onmessage, listen)
})
})
})
describe('CloseEvent WPTs ported', () => {
test('initCloseEvent', (t) => {
// Taken from websockets/interfaces/CloseEvent/historical.html
t.assert.ok(!('initCloseEvent' in CloseEvent.prototype))
t.assert.ok(!('initCloseEvent' in new CloseEvent('close')))
})
test('CloseEvent constructor', (t) => {
// Taken from websockets/interfaces/CloseEvent/constructor.html
{
const event = new CloseEvent('foo')
t.assert.ok(event instanceof CloseEvent, 'should be a CloseEvent')
t.assert.strictEqual(event.type, 'foo')
t.assert.ok(!event.bubbles, 'bubbles')
t.assert.ok(!event.cancelable, 'cancelable')
t.assert.ok(!event.wasClean, 'wasClean')
t.assert.strictEqual(event.code, 0)
t.assert.strictEqual(event.reason, '')
}
{
const event = new CloseEvent('foo', {
bubbles: true,
cancelable: true,
wasClean: true,
code: 7,
reason: 'x'
})
t.assert.ok(event instanceof CloseEvent, 'should be a CloseEvent')
t.assert.strictEqual(event.type, 'foo')
t.assert.ok(event.bubbles, 'bubbles')
t.assert.ok(event.cancelable, 'cancelable')
t.assert.ok(event.wasClean, 'wasClean')
t.assert.strictEqual(event.code, 7)
t.assert.strictEqual(event.reason, 'x')
}
})
})
describe('ErrorEvent WPTs ported', () => {
test('Synthetic ErrorEvent', (t) => {
// Taken from html/webappapis/scripting/events/event-handler-processing-algorithm-error/document-synthetic-errorevent.html
{
const e = new ErrorEvent('error')
t.assert.strictEqual(e.message, '')
t.assert.strictEqual(e.filename, '')
t.assert.strictEqual(e.lineno, 0)
t.assert.strictEqual(e.colno, 0)
t.assert.strictEqual(e.error, undefined)
}
{
const e = new ErrorEvent('error', { error: null })
t.assert.strictEqual(e.error, null)
}
{
const e = new ErrorEvent('error', { error: undefined })
t.assert.strictEqual(e.error, undefined)
}
{
const e = new ErrorEvent('error', { error: 'foo' })
t.assert.strictEqual(e.error, 'foo')
}
})
test('webidl', (t) => {
// Taken from webidl/ecmascript-binding/no-regexp-special-casing.any.js
const regExp = new RegExp()
regExp.message = 'some message'
const errorEvent = new ErrorEvent('type', regExp)
t.assert.strictEqual(errorEvent.message, 'some message')
})
test('initErrorEvent', (t) => {
// Taken from workers/Worker_dispatchEvent_ErrorEvent.htm
const e = new ErrorEvent('error')
t.assert.ok(!('initErrorEvent' in e), 'should not be supported')
})
})