diff --git a/src/Client.spec.ts b/src/Client.spec.ts index 2b37a44..439b75e 100644 --- a/src/Client.spec.ts +++ b/src/Client.spec.ts @@ -1,8 +1,10 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; import * as WebSocket from 'ws'; import { setWebSocket } from './'; -import { Method } from './wire/packets'; - import { Client, ClientType } from './Client'; +import { IClient } from './IClient'; +import { Method } from './wire/packets'; setWebSocket(WebSocket); const port = process.env.SERVER_PORT || 1339; @@ -61,5 +63,37 @@ describe('client', () => { }); }); + describe('state synchronization', () => { + let mockClient: IClient; + beforeEach(() => { + client = createClient(); + client.execute = sinon.stub().returns(new Promise(resolve => { resolve(); })); + //client.open(socketOptions); + }); + it('synchronizes scenes', () => { + const scenes = client.getScenes(); + mockClient = client; + //const stub = sinon.stub(mockClient, 'execute'); + client.synchronizeScenes(); + expect(client.execute).to.be.calledWith(new Promise(resolve => {resolve(scenes); })); + }); + it('synchronizes groups', () => { + const groups = client.getGroups(); + mockClient = client; + //const stub = sinon.stub(mockClient, 'execute'); + client.synchronizeGroups(); + expect(client.execute).to.be.calledWith(groups); + }); + it('synchronizes state', () => { + const scenes = client.getScenes(); + const groups = client.getGroups(); + mockClient = client; + //const stub = sinon.stub(mockClient, 'execute'); + client.synchronizeState(); + expect(client.execute).to.be.calledWith(scenes); + expect(client.execute).to.be.calledWith(groups); + }); + }); + afterEach(done => tearDown(done)); }); diff --git a/src/Client.ts b/src/Client.ts index ef28f37..deaf3ba 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -133,7 +133,7 @@ export class Client extends EventEmitter implements IClient { this.createSocket(options); this.socket.connect(); return resolveOn(this, 'open') - .then(() => this); + .then(() => this); } /** @@ -199,9 +199,11 @@ export class Client extends EventEmitter implements IClient { /** * Retrieves and hydrates client side stores with state from the server */ - public synchronizeState(): Promise { - return Promise.all([this.synchronizeGroups(), this.synchronizeScenes()]) - .then(() => {/** */}); + public synchronizeState(): Promise<[IGroup[], IScene[]]> { + return Promise.all([ + this.getGroups().then(res => this.state.synchronizeGroups(res)), + this.getScenes().then(res => this.state.synchronizeScenes(res)), + ]); } /**