From 52ba27e09215f102122339abde3599f444e44f66 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 14 Mar 2016 13:16:54 +0100 Subject: [PATCH] test for errors, use profiles --- test/cli.tests.js | 128 +++++++++++++++++++++++++++++++++---------- test/config.tests.js | 57 +++++++++++++++++++ 2 files changed, 155 insertions(+), 30 deletions(-) create mode 100644 test/config.tests.js diff --git a/test/cli.tests.js b/test/cli.tests.js index e79b60f..5decb3e 100644 --- a/test/cli.tests.js +++ b/test/cli.tests.js @@ -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,32 +29,44 @@ describe('cli', () => { assert.equal(consoleLogSpy.args[0][0], '[usage]'); }); - it('lists devices', function() { - const minimistStub = sandbox.stub(); - minimistStub.returns({l: true}); - const getDevicesStub = sandbox.stub(); + describe('device list', () => { + let minimistStub, getDevicesStub; - proxyquire('../cli.js', { - 'minimist': minimistStub, - './swm.js': { - getDevices: getDevicesStub - } - }); - getDevicesStub.args[0][0](null, { - LVDS1: {connected: true}, - HDMI2: {connected: false} + beforeEach(() => { + minimistStub = sandbox.stub(); + minimistStub.returns({l: true}); + getDevicesStub = sandbox.stub(); + + proxyquire('../cli.js', { + 'minimist': minimistStub, + './swm.js': { + getDevices: getDevicesStub + } + }); }); - 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('lists devices', () => { + 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'); + 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', () => { 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,27 +100,84 @@ 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', () => { - assert.equal(switchDevicesStub.callCount, 1); - assert.equal(switchDevicesStub.args[0][0], '[some xrandr options]'); + getDevicesStub.args[0][0](null, deviceData); + assert.equal(switchDevicesStub.callCount, 1); + assert.equal(switchDevicesStub.args[0][0], '[some xrandr options]'); }); it('calls executePostCmd', () => { - assert.equal(executePostCmdStub.callCount, 1); - assert.equal(executePostCmdStub.args[0][0], '[some post cmd]'); + 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']); }); }); }); diff --git a/test/config.tests.js b/test/config.tests.js new file mode 100644 index 0000000..035a5d6 --- /dev/null +++ b/test/config.tests.js @@ -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); + }); +});