-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose MessageEvent in fetch bundle (#2770)
* expose MessageEvent * fixup
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const assert = require('assert') | ||
const { MessageEvent } = require('../..') | ||
|
||
test('test/parallel/test-messageevent-brandcheck.js', () => { | ||
[ | ||
'data', | ||
'origin', | ||
'lastEventId', | ||
'source', | ||
'ports' | ||
].forEach((i) => { | ||
assert.throws(() => Reflect.get(MessageEvent.prototype, i, {}), { | ||
constructor: TypeError, | ||
message: 'Illegal invocation' | ||
}) | ||
}) | ||
}) | ||
|
||
test('test/parallel/test-worker-message-port.js', () => { | ||
const dummyPort = new MessageChannel().port1 | ||
|
||
for (const [args, expected] of [ | ||
[ | ||
['message'], | ||
{ | ||
type: 'message', | ||
data: null, | ||
origin: '', | ||
lastEventId: '', | ||
source: null, | ||
ports: [] | ||
} | ||
], | ||
[ | ||
['message', { data: undefined, origin: 'foo' }], | ||
{ | ||
type: 'message', | ||
data: null, | ||
origin: 'foo', | ||
lastEventId: '', | ||
source: null, | ||
ports: [] | ||
} | ||
], | ||
[ | ||
['message', { data: 2, origin: 1, lastEventId: 0 }], | ||
{ | ||
type: 'message', | ||
data: 2, | ||
origin: '1', | ||
lastEventId: '0', | ||
source: null, | ||
ports: [] | ||
} | ||
], | ||
[ | ||
['message', { lastEventId: 'foo' }], | ||
{ | ||
type: 'message', | ||
data: null, | ||
origin: '', | ||
lastEventId: 'foo', | ||
source: null, | ||
ports: [] | ||
} | ||
], | ||
[ | ||
['messageerror', { lastEventId: 'foo', source: dummyPort }], | ||
{ | ||
type: 'messageerror', | ||
data: null, | ||
origin: '', | ||
lastEventId: 'foo', | ||
source: dummyPort, | ||
ports: [] | ||
} | ||
], | ||
[ | ||
['message', { ports: [dummyPort], source: null }], | ||
{ | ||
type: 'message', | ||
data: null, | ||
origin: '', | ||
lastEventId: '', | ||
source: null, | ||
ports: [dummyPort] | ||
} | ||
] | ||
]) { | ||
const ev = new MessageEvent(...args) | ||
const { type, data, origin, lastEventId, source, ports } = ev | ||
assert.deepStrictEqual(expected, { | ||
type, data, origin, lastEventId, source, ports | ||
}) | ||
} | ||
|
||
assert.throws(() => new MessageEvent('message', { source: 1 }), { | ||
constructor: TypeError, | ||
message: 'MessagePort: Expected 1 to be an instance of MessagePort.' | ||
}) | ||
assert.throws(() => new MessageEvent('message', { source: {} }), { | ||
constructor: TypeError, | ||
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.' | ||
}) | ||
assert.throws(() => new MessageEvent('message', { ports: 0 }), { | ||
constructor: TypeError, | ||
message: 'Sequence: Value of type Number is not an Object.' | ||
}) | ||
assert.throws(() => new MessageEvent('message', { ports: [null] }), { | ||
constructor: TypeError, | ||
message: 'MessagePort: Expected null to be an instance of MessagePort.' | ||
}) | ||
assert.throws(() => | ||
new MessageEvent('message', { ports: [{}] }) | ||
, { | ||
constructor: TypeError, | ||
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.' | ||
}) | ||
|
||
assert(new MessageEvent('message') instanceof Event) | ||
|
||
// https://github.com/nodejs/node/issues/51767 | ||
const event = new MessageEvent('type', { cancelable: true }) | ||
event.preventDefault() | ||
|
||
assert(event.cancelable) | ||
assert(event.defaultPrevented) | ||
}) | ||
|
||
test('bug in node core', () => { | ||
// In node core, this will throw an error. | ||
new MessageEvent('', null) // eslint-disable-line no-new | ||
}) |