mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
f08a55c21f
refs: https://github.com/TryGhost/Team/issues/856 refs: https://github.com/TryGhost/Team/issues/756 - The .test.js extension is better than _spec.js as it's more obvious that it's an extension - It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test` - It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856) - Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
107 lines
4.1 KiB
JavaScript
107 lines
4.1 KiB
JavaScript
const should = require('should');
|
|
const sinon = require('sinon');
|
|
|
|
const configUtils = require('../../utils/configUtils');
|
|
const events = require('../../../core/server/lib/common/events');
|
|
|
|
const bootstrapSocket = require('@tryghost/bootstrap-socket');
|
|
|
|
describe('Notify', function () {
|
|
describe('notifyServerStarted', function () {
|
|
let notify;
|
|
let socketStub;
|
|
let eventSpy;
|
|
|
|
beforeEach(function () {
|
|
// Have to re-require each time to clear the internal flag
|
|
delete require.cache[require.resolve('../../../core/server/notify')];
|
|
notify = require('../../../core/server/notify');
|
|
|
|
// process.send isn't set for tests, we can safely override;
|
|
process.send = sinon.stub();
|
|
|
|
// stub socket connectAndSend method
|
|
socketStub = sinon.stub(bootstrapSocket, 'connectAndSend');
|
|
|
|
// Spy for the events that get called
|
|
eventSpy = sinon.spy(events, 'emit');
|
|
});
|
|
|
|
afterEach(function () {
|
|
process.send = undefined;
|
|
configUtils.restore();
|
|
socketStub.restore();
|
|
eventSpy.restore();
|
|
});
|
|
|
|
it('it resolves a promise', function () {
|
|
notify.notifyServerStarted().should.be.fulfilled();
|
|
});
|
|
|
|
it('it communicates with IPC correctly on success', function () {
|
|
notify.notifyServerStarted();
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
|
|
let message = process.send.firstCall.args[0];
|
|
message.should.be.an.Object().with.properties('started', 'debug');
|
|
message.should.not.have.property('error');
|
|
message.started.should.be.true();
|
|
});
|
|
|
|
it('communicates with IPC correctly on failure', function () {
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
|
|
let message = process.send.firstCall.args[0];
|
|
message.should.be.an.Object().with.properties('started', 'debug', 'error');
|
|
message.started.should.be.false();
|
|
message.error.should.be.an.Object().with.properties('message');
|
|
message.error.message.should.eql('something went wrong');
|
|
});
|
|
|
|
it('communicates via bootstrap socket correctly on success', function () {
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
notify.notifyServerStarted();
|
|
|
|
socketStub.calledOnce.should.be.true();
|
|
socketStub.firstCall.args[0].should.eql('testing');
|
|
socketStub.firstCall.args[1].should.be.an.Object().with.properties('info', 'warn');
|
|
|
|
let message = socketStub.firstCall.args[2];
|
|
message.should.be.an.Object().with.properties('started', 'debug');
|
|
message.should.not.have.property('error');
|
|
message.started.should.be.true();
|
|
});
|
|
|
|
it('communicates via bootstrap socket correctly on failure', function () {
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
|
|
|
socketStub.calledOnce.should.be.true();
|
|
socketStub.firstCall.args[0].should.eql('testing');
|
|
socketStub.firstCall.args[1].should.be.an.Object().with.properties('info', 'warn');
|
|
|
|
let message = socketStub.firstCall.args[2];
|
|
message.should.be.an.Object().with.properties('started', 'debug', 'error');
|
|
message.started.should.be.false();
|
|
message.error.should.be.an.Object().with.properties('message');
|
|
message.error.message.should.eql('something went wrong');
|
|
});
|
|
|
|
it('can be called multiple times, but only communicates once', function () {
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
notify.notifyServerStarted();
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
|
notify.notifyServerStarted();
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
socketStub.calledOnce.should.be.true();
|
|
});
|
|
});
|
|
});
|