fix tests for switch to callbacks

This commit is contained in:
Thomas Ruoff
2016-03-11 15:23:53 +01:00
parent a64a14419b
commit 90ad436396
3 changed files with 31 additions and 60 deletions

2
swm.js
View File

@@ -8,7 +8,7 @@ function executeCmd(cmd, callback) {
} }
function getDevices(callback) { function getDevices(callback) {
executeCmd('xrandr', (err, stdout) => callback(err, xrandrParse(stdout))); executeCmd('xrandr', (err, stdout) => callback(err, err ? null : xrandrParse(stdout)));
} }
function switchDevices(xrandrOptions, callback) { function switchDevices(xrandrOptions, callback) {

View File

@@ -33,19 +33,18 @@ describe('cli', () => {
const minimistStub = sandbox.stub(); const minimistStub = sandbox.stub();
minimistStub.returns({l: true}); minimistStub.returns({l: true});
const getDevicesStub = sandbox.stub(); const getDevicesStub = sandbox.stub();
getDevicesStub.returns({
then: cb => cb({
LVDS1: {connected: true},
HDMI2: {connected: false}
})
});
const cli = proxyquire('../cli.js', { proxyquire('../cli.js', {
'minimist': minimistStub, 'minimist': minimistStub,
'./swm.js': { './swm.js': {
getDevices: getDevicesStub getDevices: getDevicesStub
} }
}); });
getDevicesStub.args[0][0](null, {
LVDS1: {connected: true},
HDMI2: {connected: false}
});
assert.equal(getDevicesStub.callCount, 1); assert.equal(getDevicesStub.callCount, 1);
assert.equal(consoleLogSpy.callCount, 3); assert.equal(consoleLogSpy.callCount, 3);
assert.equal(consoleLogSpy.args[0][0], 'Detected devices:\n'); assert.equal(consoleLogSpy.args[0][0], 'Detected devices:\n');
@@ -75,9 +74,7 @@ describe('cli', () => {
HDMI2: {connected: false} HDMI2: {connected: false}
}; };
getDevicesStub = sandbox.stub().returns({ getDevicesStub = sandbox.stub();
then: cb => cb(deviceData)
});
generateXrandrOptionsStub = sandbox.stub().returns('[some xrandr options]'); generateXrandrOptionsStub = sandbox.stub().returns('[some xrandr options]');
switchDevicesStub = sandbox.stub(); switchDevicesStub = sandbox.stub();
executePostCmdStub = sandbox.stub(); executePostCmdStub = sandbox.stub();
@@ -92,6 +89,8 @@ describe('cli', () => {
}, },
'./config.js': {} './config.js': {}
}); });
getDevicesStub.args[0][0](null, deviceData);
}); });
it('calls getDevices', () => { it('calls getDevices', () => {
@@ -111,7 +110,6 @@ describe('cli', () => {
it('calls executePostCmd', () => { it('calls executePostCmd', () => {
assert.equal(executePostCmdStub.callCount, 1); assert.equal(executePostCmdStub.callCount, 1);
assert.equal(executePostCmdStub.args[0][0], '[some post cmd]'); assert.equal(executePostCmdStub.args[0][0], '[some post cmd]');
}); });
}); });
}); });

View File

@@ -32,43 +32,32 @@ describe('swm', () => {
}); });
it('calls exec', () => { it('calls exec', () => {
swm.getDevices(); swm.getDevices(sandbox.stub());
assert.equal(execStub.callCount, 1); assert.equal(execStub.callCount, 1);
assert.equal(execStub.args[0][0], 'xrandr'); assert.equal(execStub.args[0][0], 'xrandr');
}); });
it('returns promise', () => { it('passes error when exec passes error', (done) => {
const devices = swm.getDevices(); const devices = swm.getDevices((err, devices) => {
assert.equal(typeof devices.then, 'function');
assert.equal(typeof devices.catch, 'function');
});
it('rejects when exec passes error', (done) => {
const devices = swm.getDevices();
const cb = execStub.args[0][1];
cb('some error');
return devices.catch((err) => {
assert.equal(xrandrParseStub.callCount, 0); assert.equal(xrandrParseStub.callCount, 0);
assert.equal(err, 'some error'); assert.equal(err, 'some error');
done(); done();
}); });
const cb = execStub.args[0][1];
cb('some error');
}); });
it('parses result', (done) => { it('parses result', (done) => {
xrandrParseStub.returns('some result'); xrandrParseStub.returns('[some result]');
const devices = swm.getDevices(); swm.getDevices((err, devices) => {
const cb = execStub.args[0][1]; assert.equal(xrandrParseStub.args[0][0], '[some stdout]');
cb(null, 'stdout');
return devices.then((result) => {
assert.equal(xrandrParseStub.args[0][0], 'stdout');
assert.equal(xrandrParseStub.callCount, 1); assert.equal(xrandrParseStub.callCount, 1);
assert.equal(result, 'some result'); assert.equal(devices, '[some result]');
done(); done();
}); });
const cb = execStub.args[0][1];
cb(null, '[some stdout]');
}); });
}); });
@@ -144,27 +133,19 @@ describe('swm', () => {
}); });
it('calls exec with xrandr and opitons', () => { it('calls exec with xrandr and opitons', () => {
swm.switchDevices('[some options]'); swm.switchDevices('[some options]', sandbox.stub());
assert.equal(execStub.callCount, 1); assert.equal(execStub.callCount, 1);
assert.equal(execStub.args[0][0], 'xrandr [some options]'); assert.equal(execStub.args[0][0], 'xrandr [some options]');
}); });
it('returns promise', () => { it('passes error when exec passes error', (done) => {
const promise = swm.switchDevices('[some options]'); swm.switchDevices('[some options]', (err, devices) => {
assert.equal(typeof promise.then, 'function');
assert.equal(typeof promise.catch, 'function');
});
it('rejects when exec passes error', (done) => {
const promise = swm.switchDevices('[some options]');
const cb = execStub.args[0][1];
cb('some error');
return promise.catch((err) => {
assert.equal(err, 'some error'); assert.equal(err, 'some error');
done(); done();
}); });
const cb = execStub.args[0][1];
cb('some error');
}); });
}); });
@@ -188,21 +169,13 @@ describe('swm', () => {
assert.equal(execStub.args[0][0], '[some cmd]'); assert.equal(execStub.args[0][0], '[some cmd]');
}); });
it('returns promise', () => { it('passes error when exec passes error', (done) => {
const promise = swm.executePostCmd('[some cmd]'); swm.executePostCmd('[some cmd]', (err, devices) => {
assert.equal(typeof promise.then, 'function'); assert.equal(err, '[some error]');
assert.equal(typeof promise.catch, 'function');
});
it('rejects when exec passes error', (done) => {
const promise = swm.executePostCmd('[some cmd]');
const cb = execStub.args[0][1];
cb('some error');
return promise.catch((err) => {
assert.equal(err, 'some error');
done(); done();
}); });
const cb = execStub.args[0][1];
cb('[some error]');
}); });
}); });
}); });