Skip to content

Commit

Permalink
feat: 'serverReady' event
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava Baginov committed Jun 21, 2019
1 parent dbb5332 commit 1623baa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/gui/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ module.exports = class Api {
initServer(server) {
this._gui.emit(this._gui.events.SERVER_INIT, server);
}

serverReady(data) {
this._gui.emit(this._gui.events.SERVER_READY, data);
}
};
3 changes: 2 additions & 1 deletion lib/gui/constants/gui-events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

module.exports = {
SERVER_INIT: 'serverInit'
SERVER_INIT: 'serverInit',
SERVER_READY: 'serverReady'
};
6 changes: 5 additions & 1 deletion lib/gui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ exports.start = ({paths, tool, guiApi, configs}) => {
server.listen(options.port, options.hostname, callback);
});
})
.then(() => ({url: `http://${options.hostname}:${options.port}`}));
.then(() => {
const data = {url: `http://${options.hostname}:${options.port}`};
guiApi.serverReady(data);
return data;
});
};
13 changes: 13 additions & 0 deletions test/lib/gui/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ describe('lig/gui/api', () => {
assert.calledOnceWith(onServerInit, {foo: 'bar'});
});
});

describe('serverReady', () => {
it('should emit "SERVER_READY" event through gui api', () => {
const tool = stubTool();
const api = Api.create(tool);
const onServerReady = sinon.spy().named('onServerReady');
tool.gui.on(guiEvents.SERVER_READY, onServerReady);

api.serverReady({url: 'http://my.server'});

assert.calledOnceWith(onServerReady, {url: 'http://my.server'});
});
});
});
19 changes: 14 additions & 5 deletions test/lib/gui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ describe('lib/gui/server', () => {
static: sandbox.stub()
});

const mkApi_ = () => ({initServer: sandbox.stub()});
const mkApi_ = () => ({
initServer: sandbox.stub(),
serverReady: sandbox.stub()
});

const startServer = (opts = {}) => {
opts = _.defaults(opts, {
paths: [],
tool: stubTool(),
guiApi: mkApi_(),
configs: {options: {}, pluginConfig: {path: 'default-path'}}
configs: {
options: {hostname: 'localhost', port: '4444'},
pluginConfig: {path: 'default-path'}
}
});

return server.start(opts);
Expand Down Expand Up @@ -58,21 +64,24 @@ describe('lib/gui/server', () => {
const guiApi = mkApi_();

return startServer({guiApi})
.then(() => assert.calledOnceWith(guiApi.initServer, expressStub));
.then(() => {
assert.calledOnceWith(guiApi.initServer, expressStub);
assert.calledOnceWith(guiApi.serverReady, {url: 'http://localhost:4444'});
});
});

it('should init server only after body is parsed', () => {
const guiApi = mkApi_();

return startServer({guiApi})
.then(() => assert.callOrder(bodyParserStub.json, guiApi.initServer));
.then(() => assert.callOrder(bodyParserStub.json, guiApi.initServer, guiApi.serverReady));
});

it('should init server before any static middleware starts', () => {
const guiApi = mkApi_();

return startServer({guiApi})
.then(() => assert.callOrder(guiApi.initServer, staticMiddleware));
.then(() => assert.callOrder(guiApi.initServer, staticMiddleware, guiApi.serverReady));
});

it('should properly complete app working', () => {
Expand Down

0 comments on commit 1623baa

Please sign in to comment.