From f666770315ab65a46d52b5a17317c65571ec2455 Mon Sep 17 00:00:00 2001 From: Tanner Nielsen Date: Tue, 13 Nov 2018 19:42:12 -0600 Subject: [PATCH] feat: added a waitFor parameter to SocketClient#connect() to resolve connection issue --- src/client/socket-client.ts | 29 ++++++++++------------- test/integration/Socket.test.ts | 41 +++++++++++++++++---------------- test/shared/socket.ts | 2 +- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/client/socket-client.ts b/src/client/socket-client.ts index aa9a0de..381e2b8 100644 --- a/src/client/socket-client.ts +++ b/src/client/socket-client.ts @@ -81,33 +81,28 @@ export abstract class SocketClient { } /** - * Attempts to connect to a SocketServer. Returns a Promise for when the process completes or fails. + * Attempts to connect to a SocketServer. */ - public connect(url: string = '', options?: SocketIOClient.ConnectOpts): Promise { + public connect(url: string = '', waitFor: WaitFor = null, options?: SocketIOClient.ConnectOpts): WaitFor extends string ? Promise : void { if (!options) options = {}; Object.assign(options, { autoConnect: false }); - return new Promise((resolve, reject) => { - this.socket = socketio(url, options); - this.attachSocketHandlers(); - this.socket.io.open((err?) => { - if (err) reject(err); - else resolve(); - }); - }); + this.socket = socketio(url, options); + this.attachSocketHandlers(); + this.socket.open(); + + if (typeof waitFor === 'string') + return this.blockEvent(waitFor); } /** * Disconnects from the SocketServer, if there was a connection. */ - public disconnect(): Promise { - return new Promise((resolve, reject) => { - if (this.socket) this.socket.close(); - this.socket = null; - resolve(); - }); + public disconnect() { + if (this.socket) this.socket.close(); + this.socket = null; } /** @@ -120,7 +115,7 @@ export abstract class SocketClient { /** * Gives the ability to block and wait for an event. Usage: `await client.blockEvent('some-event');` */ - public blockEvent(event: Event): Promise { + public blockEvent(event: Event): Promise { return new Promise((resolve, reject) => { if (!this.waiters[event]) this.waiters[event] = []; diff --git a/test/integration/Socket.test.ts b/test/integration/Socket.test.ts index 1b288dd..d846277 100644 --- a/test/integration/Socket.test.ts +++ b/test/integration/Socket.test.ts @@ -17,43 +17,44 @@ describe('SocketServer + SocketClient', function(){ describe('Connecting/disconnecting a client to/from the server', function(){ it('should throw an error if the client connects to a bad address', function(){ - expect(async () => { await c.connect('http://localhost:3001') }).to.throw; + expect(async () => { await c.connect('http://localhost:3001', 'connected') }).to.throw; }); it('should not throw an error if the client connects to the correct address', function(){ - expect(async () => { await c.connect('http://localhost:3000') }).to.not.throw; + expect(async () => { await c.connect('http://localhost:3000', 'connected') }).to.not.throw; }); it('should give proper results for isConnected()', async function(){ expect(c.isConnected()).to.be.false; - await c.connect('http://localhost:3000/'); - // expect(c.isConnected()).to.be.true; + await c.connect('http://localhost:3000', 'connected'); + expect(c.isConnected()).to.be.true; - await c.disconnect(); + c.disconnect('disconnected'); expect(c.isConnected()).to.be.false; }); it('should properly handle disconnects', async function(){ - await c.connect('http://localhost:3000'); + await c.connect('http://localhost:3000', 'connected'); expect(c.socket).to.not.equal(null); - await c.disconnect(); + c.disconnect(); expect(c.socket).to.equal(null); }); }); - // describe('Transmitting and receiving data', function(){ - // it('should be able to accept changes from the server', async function(){ - // await c.connect('http://localhost:3000'); - // - // console.log('sending event...'); - // s.emit(s.io.nsps['/'], 'reset-data', {newKey: 'new value'}); - // console.log('awaiting event...'); - // await c.blockEvent('reset-data'); - // console.log('received event...'); - // - // await c.disconnect(); - // }); - // }); + describe('Transmitting and receiving data', function(){ + it('should be able to accept changes from the server', async function(){ + await c.connect('http://localhost:3000', 'connected'); + + s.emit(s.io.nsps['/'], 'reset-data', {newKey: 'new value'}); + await c.blockEvent('reset-data'); + + expect(c.data).to.deep.equal({newKey: 'new value'}); + + await c.disconnect(); + }); + + // TODO: ... + }); }); \ No newline at end of file diff --git a/test/shared/socket.ts b/test/shared/socket.ts index 049f0cb..6e5b3a3 100644 --- a/test/shared/socket.ts +++ b/test/shared/socket.ts @@ -47,7 +47,7 @@ export class Client extends SocketClient { protected socketHandlers: SocketHandlers> = { 'connected': (id: string) => { - console.log(id, 'has connected!'); + // console.log(id, 'has connected!'); }, 'patch-data': (key: string, value: string) => { this.data[key] = value;