Skip to content

Commit

Permalink
Update type of url parameter of websocket constructor (#335)
Browse files Browse the repository at this point in the history
* Update type of url parameter of websocket constructor

Typescript expects the type of url to be string | URL. Update types
to reflect this.

* Remove option type and add test

* add check for url property of websocket

* add URL import for node v8

Co-authored-by: Albert Seo <[email protected]>
  • Loading branch information
albertseo and Albert Seo authored Nov 15, 2021
1 parent 5a63a4d commit b6fe0eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare module 'mock-socket' {
// https://html.spec.whatwg.org/multipage/web-sockets.html#websocket
//
class WebSocket extends EventTarget {
constructor(url?: string, protocols?: string|string[]);
constructor(url: string | URL, protocols?: string|string[]);

static readonly CONNECTING: 0;
static readonly OPEN: 1;
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/websocket.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import URL from 'url-parse';
import test from 'ava';
import WebSocket from '../../src/websocket';
import EventTarget from '../../src/event/target';
Expand All @@ -8,14 +9,16 @@ test.skip('that not passing a url throws an error', t => {
}, "Failed to construct 'WebSocket': 1 argument required, but only 0 present");
});

test('that websockets inherents EventTarget methods', t => {
test('that websockets inherents EventTarget methods with string type url', t => {
const mySocket = new WebSocket('ws://not-real');
t.true(mySocket instanceof EventTarget);
});

test('that websockets inherents EventTarget methods', t => {
const mySocket = new WebSocket('ws://not-real');
test('that websockets inherents EventTarget methods with URL type url', t => {
const mySocket = new WebSocket(new URL('ws://not-real'));

t.true(mySocket instanceof EventTarget);
t.is(mySocket.url, 'ws://not-real/');
});

test('that on(open, message, error, and close) can be set', t => {
Expand Down

0 comments on commit b6fe0eb

Please sign in to comment.