diff --git a/swm.js b/swm.js index 026b60b..56940d4 100644 --- a/swm.js +++ b/swm.js @@ -8,7 +8,7 @@ function executeCmd(cmd, 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) { diff --git a/test/cli.tests.js b/test/cli.tests.js index d48addd..e79b60f 100644 --- a/test/cli.tests.js +++ b/test/cli.tests.js @@ -33,19 +33,18 @@ describe('cli', () => { const minimistStub = sandbox.stub(); minimistStub.returns({l: true}); 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, './swm.js': { getDevices: getDevicesStub } }); + getDevicesStub.args[0][0](null, { + LVDS1: {connected: true}, + HDMI2: {connected: false} + }); + assert.equal(getDevicesStub.callCount, 1); assert.equal(consoleLogSpy.callCount, 3); assert.equal(consoleLogSpy.args[0][0], 'Detected devices:\n'); @@ -75,9 +74,7 @@ describe('cli', () => { HDMI2: {connected: false} }; - getDevicesStub = sandbox.stub().returns({ - then: cb => cb(deviceData) - }); + getDevicesStub = sandbox.stub(); generateXrandrOptionsStub = sandbox.stub().returns('[some xrandr options]'); switchDevicesStub = sandbox.stub(); executePostCmdStub = sandbox.stub(); @@ -92,6 +89,8 @@ describe('cli', () => { }, './config.js': {} }); + + getDevicesStub.args[0][0](null, deviceData); }); it('calls getDevices', () => { @@ -111,7 +110,6 @@ describe('cli', () => { it('calls executePostCmd', () => { assert.equal(executePostCmdStub.callCount, 1); assert.equal(executePostCmdStub.args[0][0], '[some post cmd]'); - }); }); }); diff --git a/test/swm.tests.js b/test/swm.tests.js index a3d4a34..2a212c4 100644 --- a/test/swm.tests.js +++ b/test/swm.tests.js @@ -32,43 +32,32 @@ describe('swm', () => { }); it('calls exec', () => { - swm.getDevices(); + swm.getDevices(sandbox.stub()); assert.equal(execStub.callCount, 1); assert.equal(execStub.args[0][0], 'xrandr'); }); - it('returns promise', () => { - const devices = swm.getDevices(); - 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) => { + it('passes error when exec passes error', (done) => { + const devices = swm.getDevices((err, devices) => { assert.equal(xrandrParseStub.callCount, 0); assert.equal(err, 'some error'); done(); }); - + const cb = execStub.args[0][1]; + cb('some error'); }); it('parses result', (done) => { - xrandrParseStub.returns('some result'); - const devices = swm.getDevices(); - const cb = execStub.args[0][1]; - cb(null, 'stdout'); - - return devices.then((result) => { - assert.equal(xrandrParseStub.args[0][0], 'stdout'); + xrandrParseStub.returns('[some result]'); + swm.getDevices((err, devices) => { + assert.equal(xrandrParseStub.args[0][0], '[some stdout]'); assert.equal(xrandrParseStub.callCount, 1); - assert.equal(result, 'some result'); + assert.equal(devices, '[some result]'); done(); }); + const cb = execStub.args[0][1]; + cb(null, '[some stdout]'); }); }); @@ -144,27 +133,19 @@ describe('swm', () => { }); 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.args[0][0], 'xrandr [some options]'); }); - it('returns promise', () => { - const promise = swm.switchDevices('[some options]'); - 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) => { + it('passes error when exec passes error', (done) => { + swm.switchDevices('[some options]', (err, devices) => { assert.equal(err, 'some error'); done(); }); + const cb = execStub.args[0][1]; + cb('some error'); }); }); @@ -188,21 +169,13 @@ describe('swm', () => { assert.equal(execStub.args[0][0], '[some cmd]'); }); - it('returns promise', () => { - const promise = swm.executePostCmd('[some cmd]'); - assert.equal(typeof promise.then, 'function'); - 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'); + it('passes error when exec passes error', (done) => { + swm.executePostCmd('[some cmd]', (err, devices) => { + assert.equal(err, '[some error]'); done(); }); + const cb = execStub.args[0][1]; + cb('[some error]'); }); }); });