mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-03 06:27:23 +01:00
Merge pull request #4 from tomru/increase-coverage
test for errors, use profiles
This commit is contained in:
@@ -13,11 +13,11 @@ describe('cli', () => {
|
||||
consoleLogSpy = sandbox.spy(console, 'log');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('shows help', function() {
|
||||
it('shows help', () => {
|
||||
const minimistStub = sandbox.stub();
|
||||
minimistStub.returns({h: true});
|
||||
|
||||
@@ -29,10 +29,13 @@ describe('cli', () => {
|
||||
assert.equal(consoleLogSpy.args[0][0], '[usage]');
|
||||
});
|
||||
|
||||
it('lists devices', function() {
|
||||
const minimistStub = sandbox.stub();
|
||||
describe('device list', () => {
|
||||
let minimistStub, getDevicesStub;
|
||||
|
||||
beforeEach(() => {
|
||||
minimistStub = sandbox.stub();
|
||||
minimistStub.returns({l: true});
|
||||
const getDevicesStub = sandbox.stub();
|
||||
getDevicesStub = sandbox.stub();
|
||||
|
||||
proxyquire('../cli.js', {
|
||||
'minimist': minimistStub,
|
||||
@@ -40,6 +43,9 @@ describe('cli', () => {
|
||||
getDevices: getDevicesStub
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('lists devices', () => {
|
||||
getDevicesStub.args[0][0](null, {
|
||||
LVDS1: {connected: true},
|
||||
HDMI2: {connected: false}
|
||||
@@ -52,9 +58,15 @@ describe('cli', () => {
|
||||
assert.equal(consoleLogSpy.args[2].join(' '), 'HDMI2: Disconnected');
|
||||
});
|
||||
|
||||
it('throws error when listing devices fails', () => {
|
||||
assert.throws(() => {
|
||||
getDevicesStub.args[0][0]('[some err]');
|
||||
}, /some err/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('switching', () => {
|
||||
let minimistStub;
|
||||
let selectedMonitors;
|
||||
let deviceData;
|
||||
let getDevicesStub;
|
||||
let generateXrandrOptionsStub;
|
||||
@@ -62,10 +74,9 @@ describe('cli', () => {
|
||||
let executePostCmdStub;
|
||||
|
||||
beforeEach(() => {
|
||||
selectedMonitors = ['LVDS1'];
|
||||
minimistStub = sandbox.stub();
|
||||
minimistStub.returns({
|
||||
_: selectedMonitors,
|
||||
_: ['LVDS1'],
|
||||
postCmd: '[some post cmd]'
|
||||
});
|
||||
|
||||
@@ -89,28 +100,85 @@ describe('cli', () => {
|
||||
},
|
||||
'./config.js': {}
|
||||
});
|
||||
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
});
|
||||
|
||||
it('calls getDevices', () => {
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
assert.equal(getDevicesStub.callCount, 1, 'calls device stub');
|
||||
});
|
||||
|
||||
it('throws error when getDevices fails', () => {
|
||||
assert.throws(() => {
|
||||
getDevicesStub.args[0][0]('[some err]');
|
||||
}, /some err/);
|
||||
});
|
||||
|
||||
it('calls generateXrandrOptions', () => {
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
assert.equal(generateXrandrOptionsStub.callCount, 1, 'calls generateXrandrOptions');
|
||||
assert.equal(generateXrandrOptionsStub.args[0][0], selectedMonitors);
|
||||
assert.deepEqual(generateXrandrOptionsStub.args[0][0], ['LVDS1']);
|
||||
});
|
||||
|
||||
it('calls switchDevices', () => {
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
assert.equal(switchDevicesStub.callCount, 1);
|
||||
assert.equal(switchDevicesStub.args[0][0], '[some xrandr options]');
|
||||
});
|
||||
|
||||
it('calls executePostCmd', () => {
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
assert.equal(executePostCmdStub.callCount, 1);
|
||||
assert.equal(executePostCmdStub.args[0][0], '[some post cmd]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('switching with profiles', () => {
|
||||
let minimistStub;
|
||||
let deviceData;
|
||||
let getDevicesStub;
|
||||
let generateXrandrOptionsStub;
|
||||
let switchDevicesStub;
|
||||
let executePostCmdStub;
|
||||
|
||||
beforeEach(() => {
|
||||
minimistStub = sandbox.stub();
|
||||
minimistStub.returns({
|
||||
_: ['LVDS1'],
|
||||
postCmd: '[some post cmd]',
|
||||
profile: 'profile1'
|
||||
});
|
||||
|
||||
deviceData = {
|
||||
LVDS1: {connected: true},
|
||||
HDMI2: {connected: false}
|
||||
};
|
||||
|
||||
getDevicesStub = sandbox.stub();
|
||||
generateXrandrOptionsStub = sandbox.stub().returns('[some xrandr options]');
|
||||
switchDevicesStub = sandbox.stub();
|
||||
executePostCmdStub = sandbox.stub();
|
||||
|
||||
proxyquire('../cli.js', {
|
||||
'minimist': minimistStub,
|
||||
'./swm.js': {
|
||||
getDevices: getDevicesStub,
|
||||
generateXrandrOptions: generateXrandrOptionsStub,
|
||||
switchDevices: switchDevicesStub,
|
||||
executePostCmd: executePostCmdStub
|
||||
},
|
||||
'./config.js': {
|
||||
profiles: {
|
||||
profile1: ['HDMI1', 'HDMI2']
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('calls generateXrandrOptions with profile settings', () => {
|
||||
getDevicesStub.args[0][0](null, deviceData);
|
||||
assert.equal(generateXrandrOptionsStub.callCount, 1, 'calls generateXrandrOptions');
|
||||
assert.deepEqual(generateXrandrOptionsStub.args[0][0], ['HDMI1', 'HDMI2']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
57
test/config.tests.js
Normal file
57
test/config.tests.js
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const sinon = require('sinon');
|
||||
const assert = require('assert');
|
||||
const proxyquire = require('proxyquire').noCallThru();
|
||||
|
||||
describe('config', () => {
|
||||
let sandbox;
|
||||
let consoleLogSpy;
|
||||
let xdgBasedirConfigStub;
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
consoleLogSpy = sandbox.spy(console, 'log');
|
||||
xdgBasedirConfigStub = sandbox.stub();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('loads config if existent', () => {
|
||||
xdgBasedirConfigStub.returns('./some_config.json');
|
||||
|
||||
const config = proxyquire('../config.js', {
|
||||
'xdg': {
|
||||
basedir: {
|
||||
configPath: xdgBasedirConfigStub
|
||||
}
|
||||
},
|
||||
'./some_config.json': {
|
||||
postCmd: '[some postCmd]',
|
||||
profiles: {
|
||||
profile1: ['HDMI1', 'HDMI2'],
|
||||
profile2: ['LVDS1']
|
||||
}
|
||||
}
|
||||
});
|
||||
assert.equal(config.postCmd, '[some postCmd]');
|
||||
assert.deepEqual(config.profiles.profile1, ['HDMI1', 'HDMI2']);
|
||||
assert.deepEqual(config.profiles.profile2, ['LVDS1']);
|
||||
});
|
||||
|
||||
it('uses defaults if not existent', () => {
|
||||
xdgBasedirConfigStub.returns('./not_existing_config.json');
|
||||
|
||||
const config = proxyquire('../config.js', {
|
||||
'xdg': {
|
||||
basedir: {
|
||||
configPath: xdgBasedirConfigStub
|
||||
}
|
||||
}
|
||||
});
|
||||
assert.equal(config.postCmd, undefined);
|
||||
assert.equal(Object.keys(config.profiles).length, 0);
|
||||
});
|
||||
});
|
||||
@@ -11,7 +11,7 @@ describe('swm', () => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
@@ -149,7 +149,7 @@ describe('swm', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('executePostCmd', function() {
|
||||
describe('executePostCmd', () => {
|
||||
let execStub;
|
||||
let swm;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user