Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed May 7, 2020
1 parent a266b21 commit 43419ff
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/websocket.test.js
Expand Up @@ -1809,6 +1809,22 @@ describe('WebSocket', () => {
assert.strictEqual(ws.listeners('bar').length, 0);
});

it('allows to add one time listeners with `addEventListener`', (done) => {
const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });

ws.addEventListener(
'foo',
() => {
assert.strictEqual(ws.listenerCount('foo'), 0);
done();
},
{ once: true }
);

assert.strictEqual(ws.listenerCount('foo'), 1);
ws.emit('foo');
});

it('supports the `removeEventListener` method', () => {
const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });

Expand All @@ -1831,6 +1847,22 @@ describe('WebSocket', () => {
assert.strictEqual(ws.listenerCount('message'), 0);
assert.strictEqual(ws.listenerCount('open'), 0);
assert.strictEqual(ws.listenerCount('foo'), 0);

ws.addEventListener('message', NOOP, { once: true });
ws.addEventListener('open', NOOP, { once: true });
ws.addEventListener('foo', NOOP, { once: true });

ws.removeEventListener('message', () => {});

assert.strictEqual(ws.listeners('message')[0]._listener, NOOP);

ws.removeEventListener('message', NOOP);
ws.removeEventListener('open', NOOP);
ws.removeEventListener('foo', NOOP);

assert.strictEqual(ws.listenerCount('message'), 0);
assert.strictEqual(ws.listenerCount('open'), 0);
assert.strictEqual(ws.listenerCount('foo'), 0);
});

it('wraps text data in a `MessageEvent`', (done) => {
Expand Down

0 comments on commit 43419ff

Please sign in to comment.