Skip to content

Commit

Permalink
chore(tests): dry up + clean up stop tests
Browse files Browse the repository at this point in the history
refs 9a7ddc2 - made those tests pass, and majorly refactored how the tests are structured for dryer testing
  • Loading branch information
vikaspotluri123 committed Jul 29, 2018
1 parent 06a90da commit 68f4241
Showing 1 changed file with 73 additions and 150 deletions.
223 changes: 73 additions & 150 deletions test/unit/commands/stop-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,215 +4,138 @@ const sinon = require('sinon');
const proxyquire = require('proxyquire');

const modulePath = '../../../lib/commands/stop';
const errors = require('../../../lib/errors');
const StopCommand = require(modulePath);

function proxiedCommand() {
return new(proxyquire(modulePath, {'../utils/check-valid-install': () => true}))();
}
// @todo: abstract to util
const noop = () => true;

describe('Unit: Commands > Stop', function () {
describe('run', function () {
it('stops all if flag is provided', function () {
const stop = new StopCommand();
const sAstub = sinon.stub();
const context = {stopAll: sAstub};
stop.run.call(context, {all: true});
expect(sAstub.calledOnce).to.be.true;
let myInstance, StopCommand, ui;

beforeEach(function () {
myInstance = {
checkEnvironment: noop,
running: () => Promise.resolve(false),
loadRunningEnvironment: noop,
config: {get: sinon.stub()}
};

StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': noop,
'../utils/get-instance': () => myInstance
});

ui = {log: sinon.stub().resolves()};
});

it('errors when unknown instance is specified', function () {
const stop = new StopCommand();
const gIstub = sinon.stub().returns(false);
const context = {system: {getInstance: gIstub}};
afterEach(function () {
sinon.restore();
});

return stop.run.call(context, {name: 'ghost'}).then(() => {
expect(false, 'Promise should have rejected').to.be.true;
}).catch((error) => {
expect(error).to.be.ok;
expect(error instanceof errors.SystemError).to.be.true;
expect(error.message).to.match(/does not exist/);
it('uses getInstance to get proper instance', function () {
StopCommand = proxyquire(modulePath, {
'../utils/get-instance': sinon.stub().throws(new Error('YES_IT_DOES'))
});
});

it('chdirs to instance install directory', function () {
const originalCommand = process.chdir;
process.chdir = sinon.stub().throws(new Error('SHORT_CIRCUIT'));
const stop = new StopCommand();
const gIstub = sinon.stub().returns({dir: '../ghost'});
const context = {system: {getInstance: gIstub}};
stop.run({}).then(() => {
expect(false, 'An error should have been thrown').to.be.true;
}).catch(error => {
expect(error.message).to.equal('YES_IT_DOES');
});
});

try {
stop.run.call(context, {name: 'ghost'});
process.chdir = originalCommand;
} catch (error) {
const pcss = process.chdir;
process.chdir = originalCommand;
expect(error).to.be.ok;
expect(error.message).to.equal('SHORT_CIRCUIT');
expect(pcss.calledOnce).to.be.true;
expect(pcss.args[0][0]).to.equal('../ghost');
}
it('stops all if flag is provided', function () {
const stop = new StopCommand();
stop.stopAll = sinon.stub()
stop.run({all: true});
expect(stop.stopAll.calledOnce).to.be.true;
});

it('gracefully notifies of already stopped instance', function () {
const runningFake = () => Promise.resolve(false);
const gIfake = () => ({running: runningFake});
const logStub = sinon.stub();
const stop = proxiedCommand();
const context = {
system: {getInstance: gIfake},
ui: {log: logStub}
};
const stop = new StopCommand(ui);

return stop.run.call(context, {}).then(() => {
expect(logStub.calledOnce).to.be.true;
expect(logStub.args[0][0]).to.match(/Ghost is already stopped!/);
return stop.run({}).then(() => {
expect(ui.log.calledOnce).to.be.true;
expect(ui.log.args[0][0]).to.match(/Ghost is already stopped!/);
});
});

it('calls process manger stop', function () {
const stop = proxiedCommand();
const stopStub = sinon.stub().resolves();
const runningStub = sinon.stub().resolves(true);
const gIstub = sinon.stub().returns({
running: runningStub,
process: {stop: stopStub},
loadRunningEnvironment: () => true
});

const runner = (fn, name) => {
expect(fn).to.be.ok;
expect(name).to.equal('Stopping Ghost');
return fn().then(() => {
expect(stopStub.calledOnce).to.be.true;
expect(stopStub.args[0][0]).to.equal(process.cwd());
expect(runningStub.calledTwice).to.be.true;
expect(runningStub.args[1][0]).to.equal(null);
expect(myInstance.running.calledTwice).to.be.true;
expect(myInstance.running.args[1][0]).to.equal(null);
});
}

const context = {
system: {getInstance: gIstub},
ui: {run: runner}
};
ui.run = runner;

const stop = new StopCommand(ui);
myInstance.process = {stop: stopStub};
myInstance.running = sinon.stub().resolves(true);

return stop.run.call(context, {});

return stop.run({});
});

describe('handles disabling', function () {
it('skips disabling if disable flag is not set', function () {
const instance = {
running: () => Promise.resolve(true),
beforeEach(function () {
Object.assign(myInstance, {
process: {
enable: () => Promise.resolve(),
isEnabled: sinon.stub().resolves(true),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(true)
stop: sinon.stub().resolves(true)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
running: () => Promise.resolve(true)
});
const stop = new StopCommand(ui, system);
ui.run = sinon.stub().resolves();
});

it('skips disabling if disable flag is not set', function () {
const stop = new StopCommand(ui);

return stop.run({disable: false}).then(() => {
expect(instance.process.isEnabled.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
expect(myInstance.process.isEnabled.called).to.be.false;
});
});

it('skips disabling if process manager doesn\'t support enable behavior', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(false)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);
// no assignment because the other properties shouldn't exist
myInstance.process = {isEnabled: sinon.stub().resolves(false)};
const stop = new StopCommand(ui);

return stop.run({disable: true}).then(() => {
expect(ui.run.calledOnce).to.be.true;
expect(instance.process.isEnabled.called).to.be.false;
expect(myInstance.process.isEnabled.called).to.be.false;
});
});

it('skips disabling if isEnabled returns false', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
enable: () => Promise.resolve(),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(false)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);
myInstance.process.isEnabled.resolves(false);
const stop = new StopCommand(ui);

return stop.run({disable: true}).then(() => {
expect(instance.process.isEnabled.calledOnce).to.be.true;
expect(instance.process.disable.called).to.be.false;
expect(myInstance.process.isEnabled.calledOnce).to.be.true;
expect(myInstance.process.disable.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
});
});

it('disables if necessary', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
enable: () => Promise.resolve(),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(true)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().callsFake(fn => Promise.resolve(fn()))
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);
myInstance.process.stop = sinon.stub().resolves();
ui.run = sinon.stub().callsFake(fn => Promise.resolve(fn()));
const stop = new StopCommand(ui);

return stop.run({disable: true}).then(() => {
expect(instance.process.isEnabled.calledOnce).to.be.true;
expect(instance.process.disable.calledOnce).to.be.true;
expect(myInstance.process.isEnabled.calledOnce).to.be.true;
expect(myInstance.process.disable.calledOnce).to.be.true;
expect(ui.run.calledTwice).to.be.true;
});
});
Expand Down Expand Up @@ -258,7 +181,7 @@ describe('Unit: Commands > Stop', function () {
config: {options: {stop: {test: true}}}
}, {}];

const yargs = {option: sinon.stub(), epilogue: () => true};
const yargs = {option: sinon.stub(), epilogue: noop};
yargs.option.returns(yargs);
StopCommand.configureOptions.call({options: {}}, 'Test', yargs, extensions, true);
expect(yargs.option.called).to.be.true;
Expand Down

0 comments on commit 68f4241

Please sign in to comment.