mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-03 06:27:23 +01:00
test for errors, use profiles
This commit is contained in:
@@ -13,11 +13,11 @@ describe('cli', () => {
|
|||||||
consoleLogSpy = sandbox.spy(console, 'log');
|
consoleLogSpy = sandbox.spy(console, 'log');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(() => {
|
||||||
sandbox.restore();
|
sandbox.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shows help', function() {
|
it('shows help', () => {
|
||||||
const minimistStub = sandbox.stub();
|
const minimistStub = sandbox.stub();
|
||||||
minimistStub.returns({h: true});
|
minimistStub.returns({h: true});
|
||||||
|
|
||||||
@@ -29,32 +29,44 @@ describe('cli', () => {
|
|||||||
assert.equal(consoleLogSpy.args[0][0], '[usage]');
|
assert.equal(consoleLogSpy.args[0][0], '[usage]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('lists devices', function() {
|
describe('device list', () => {
|
||||||
const minimistStub = sandbox.stub();
|
let minimistStub, getDevicesStub;
|
||||||
minimistStub.returns({l: true});
|
|
||||||
const getDevicesStub = sandbox.stub();
|
|
||||||
|
|
||||||
proxyquire('../cli.js', {
|
beforeEach(() => {
|
||||||
'minimist': minimistStub,
|
minimistStub = sandbox.stub();
|
||||||
'./swm.js': {
|
minimistStub.returns({l: true});
|
||||||
getDevices: getDevicesStub
|
getDevicesStub = sandbox.stub();
|
||||||
}
|
|
||||||
});
|
proxyquire('../cli.js', {
|
||||||
getDevicesStub.args[0][0](null, {
|
'minimist': minimistStub,
|
||||||
LVDS1: {connected: true},
|
'./swm.js': {
|
||||||
HDMI2: {connected: false}
|
getDevices: getDevicesStub
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(getDevicesStub.callCount, 1);
|
it('lists devices', () => {
|
||||||
assert.equal(consoleLogSpy.callCount, 3);
|
getDevicesStub.args[0][0](null, {
|
||||||
assert.equal(consoleLogSpy.args[0][0], 'Detected devices:\n');
|
LVDS1: {connected: true},
|
||||||
assert.equal(consoleLogSpy.args[1].join(' '), 'LVDS1: Connected');
|
HDMI2: {connected: false}
|
||||||
assert.equal(consoleLogSpy.args[2].join(' '), 'HDMI2: Disconnected');
|
});
|
||||||
|
|
||||||
|
assert.equal(getDevicesStub.callCount, 1);
|
||||||
|
assert.equal(consoleLogSpy.callCount, 3);
|
||||||
|
assert.equal(consoleLogSpy.args[0][0], 'Detected devices:\n');
|
||||||
|
assert.equal(consoleLogSpy.args[1].join(' '), 'LVDS1: Connected');
|
||||||
|
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', () => {
|
describe('switching', () => {
|
||||||
let minimistStub;
|
let minimistStub;
|
||||||
let selectedMonitors;
|
|
||||||
let deviceData;
|
let deviceData;
|
||||||
let getDevicesStub;
|
let getDevicesStub;
|
||||||
let generateXrandrOptionsStub;
|
let generateXrandrOptionsStub;
|
||||||
@@ -62,10 +74,9 @@ describe('cli', () => {
|
|||||||
let executePostCmdStub;
|
let executePostCmdStub;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
selectedMonitors = ['LVDS1'];
|
|
||||||
minimistStub = sandbox.stub();
|
minimistStub = sandbox.stub();
|
||||||
minimistStub.returns({
|
minimistStub.returns({
|
||||||
_: selectedMonitors,
|
_: ['LVDS1'],
|
||||||
postCmd: '[some post cmd]'
|
postCmd: '[some post cmd]'
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -89,27 +100,84 @@ describe('cli', () => {
|
|||||||
},
|
},
|
||||||
'./config.js': {}
|
'./config.js': {}
|
||||||
});
|
});
|
||||||
|
|
||||||
getDevicesStub.args[0][0](null, deviceData);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls getDevices', () => {
|
it('calls getDevices', () => {
|
||||||
|
getDevicesStub.args[0][0](null, deviceData);
|
||||||
assert.equal(getDevicesStub.callCount, 1, 'calls device stub');
|
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', () => {
|
it('calls generateXrandrOptions', () => {
|
||||||
|
getDevicesStub.args[0][0](null, deviceData);
|
||||||
assert.equal(generateXrandrOptionsStub.callCount, 1, 'calls generateXrandrOptions');
|
assert.equal(generateXrandrOptionsStub.callCount, 1, 'calls generateXrandrOptions');
|
||||||
assert.equal(generateXrandrOptionsStub.args[0][0], selectedMonitors);
|
assert.deepEqual(generateXrandrOptionsStub.args[0][0], ['LVDS1']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls switchDevices', () => {
|
it('calls switchDevices', () => {
|
||||||
assert.equal(switchDevicesStub.callCount, 1);
|
getDevicesStub.args[0][0](null, deviceData);
|
||||||
assert.equal(switchDevicesStub.args[0][0], '[some xrandr options]');
|
assert.equal(switchDevicesStub.callCount, 1);
|
||||||
|
assert.equal(switchDevicesStub.args[0][0], '[some xrandr options]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls executePostCmd', () => {
|
it('calls executePostCmd', () => {
|
||||||
assert.equal(executePostCmdStub.callCount, 1);
|
getDevicesStub.args[0][0](null, deviceData);
|
||||||
assert.equal(executePostCmdStub.args[0][0], '[some post cmd]');
|
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(function() {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user