mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-03 22:47:31 +01:00
tests for getDevices
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
"description": "Simple helper for turning on/off connected/disconnected monitors with xrandr",
|
"description": "Simple helper for turning on/off connected/disconnected monitors with xrandr",
|
||||||
"main": "indes.js",
|
"main": "indes.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "mocha"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"swm": "./cli.js"
|
"swm": "./cli.js"
|
||||||
@@ -27,5 +27,10 @@
|
|||||||
"minimist": "^1.2.0",
|
"minimist": "^1.2.0",
|
||||||
"xdg": "^0.1.1",
|
"xdg": "^0.1.1",
|
||||||
"xrandr-parse": "^0.1.1"
|
"xrandr-parse": "^0.1.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^2.4.5",
|
||||||
|
"proxyquire": "^1.7.4",
|
||||||
|
"sinon": "^1.17.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
76
test/swm.tests.js
Normal file
76
test/swm.tests.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const sinon = require('sinon');
|
||||||
|
const assert = require('assert');
|
||||||
|
const proxyquire = require('proxyquire');
|
||||||
|
|
||||||
|
describe('swm', () => {
|
||||||
|
let sandbox;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getDevices', () => {
|
||||||
|
let execStub;
|
||||||
|
let xrandrParseStub;
|
||||||
|
let swm;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
execStub = sandbox.stub();
|
||||||
|
xrandrParseStub = sandbox.stub();
|
||||||
|
swm = proxyquire('../swm.js', {
|
||||||
|
child_process: {
|
||||||
|
exec: execStub
|
||||||
|
},
|
||||||
|
'xrandr-parse': xrandrParseStub
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls exec', () => {
|
||||||
|
swm.getDevices();
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
assert.equal(xrandrParseStub.callCount, 0);
|
||||||
|
assert.equal(err, 'some error');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
assert.equal(xrandrParseStub.callCount, 1);
|
||||||
|
assert.equal(result, 'some result');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user