|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('tap') |
| 4 | +const { WebSocketServer } = require('ws') |
| 5 | +const { MessageEvent, CloseEvent, ErrorEvent } = require('../../lib/websocket/events') |
| 6 | +const { WebSocket } = require('../..') |
| 7 | + |
| 8 | +test('MessageEvent', (t) => { |
| 9 | + t.throws(() => new MessageEvent(), TypeError, 'no arguments') |
| 10 | + t.throws(() => new MessageEvent('').initMessageEvent(), TypeError) |
| 11 | + |
| 12 | + const noInitEvent = new MessageEvent('message') |
| 13 | + |
| 14 | + t.equal(noInitEvent.origin, '') |
| 15 | + t.equal(noInitEvent.data, null) |
| 16 | + t.equal(noInitEvent.lastEventId, '') |
| 17 | + t.equal(noInitEvent.source, null) |
| 18 | + t.ok(Array.isArray(noInitEvent.ports)) |
| 19 | + t.ok(Object.isFrozen(noInitEvent.ports)) |
| 20 | + t.type(new MessageEvent('').initMessageEvent('message'), MessageEvent) |
| 21 | + |
| 22 | + t.end() |
| 23 | +}) |
| 24 | + |
| 25 | +test('CloseEvent', (t) => { |
| 26 | + t.throws(() => new CloseEvent(), TypeError) |
| 27 | + |
| 28 | + const noInitEvent = new CloseEvent('close') |
| 29 | + |
| 30 | + t.equal(noInitEvent.wasClean, false) |
| 31 | + t.equal(noInitEvent.code, 0) |
| 32 | + t.equal(noInitEvent.reason, '') |
| 33 | + |
| 34 | + t.end() |
| 35 | +}) |
| 36 | + |
| 37 | +test('ErrorEvent', (t) => { |
| 38 | + t.throws(() => new ErrorEvent(), TypeError) |
| 39 | + |
| 40 | + const noInitEvent = new ErrorEvent('error') |
| 41 | + |
| 42 | + t.equal(noInitEvent.message, '') |
| 43 | + t.equal(noInitEvent.filename, '') |
| 44 | + t.equal(noInitEvent.lineno, 0) |
| 45 | + t.equal(noInitEvent.colno, 0) |
| 46 | + t.equal(noInitEvent.error, undefined) |
| 47 | + |
| 48 | + t.end() |
| 49 | +}) |
| 50 | + |
| 51 | +test('Event handlers', (t) => { |
| 52 | + t.plan(4) |
| 53 | + |
| 54 | + const server = new WebSocketServer({ port: 0 }) |
| 55 | + const ws = new WebSocket(`ws://localhost:${server.address().port}`) |
| 56 | + |
| 57 | + function listen () {} |
| 58 | + |
| 59 | + t.teardown(server.close.bind(server)) |
| 60 | + t.teardown(() => ws.close()) |
| 61 | + |
| 62 | + t.test('onopen', (t) => { |
| 63 | + t.plan(3) |
| 64 | + |
| 65 | + t.equal(ws.onopen, null) |
| 66 | + ws.onopen = 3 |
| 67 | + t.equal(ws.onopen, null) |
| 68 | + ws.onopen = listen |
| 69 | + t.equal(ws.onopen, listen) |
| 70 | + }) |
| 71 | + |
| 72 | + t.test('onerror', (t) => { |
| 73 | + t.plan(3) |
| 74 | + |
| 75 | + t.equal(ws.onerror, null) |
| 76 | + ws.onerror = 3 |
| 77 | + t.equal(ws.onerror, null) |
| 78 | + ws.onerror = listen |
| 79 | + t.equal(ws.onerror, listen) |
| 80 | + }) |
| 81 | + |
| 82 | + t.test('onclose', (t) => { |
| 83 | + t.plan(3) |
| 84 | + |
| 85 | + t.equal(ws.onclose, null) |
| 86 | + ws.onclose = 3 |
| 87 | + t.equal(ws.onclose, null) |
| 88 | + ws.onclose = listen |
| 89 | + t.equal(ws.onclose, listen) |
| 90 | + }) |
| 91 | + |
| 92 | + t.test('onmessage', (t) => { |
| 93 | + t.plan(3) |
| 94 | + |
| 95 | + t.equal(ws.onmessage, null) |
| 96 | + ws.onmessage = 3 |
| 97 | + t.equal(ws.onmessage, null) |
| 98 | + ws.onmessage = listen |
| 99 | + t.equal(ws.onmessage, listen) |
| 100 | + }) |
| 101 | +}) |
0 commit comments