From 9d88af021ff665d7c8a4731857eeadebecfd062f Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 5 Sep 2018 09:47:39 -0400 Subject: [PATCH 1/6] Add tests to validate config setting per platform --- __tests__/config.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 __tests__/config.js diff --git a/__tests__/config.js b/__tests__/config.js new file mode 100644 index 00000000..438dd1b0 --- /dev/null +++ b/__tests__/config.js @@ -0,0 +1,24 @@ +let oldPlatform; + +describe('config', () => { + beforeEach(() => { + oldPlatform = process.platform; + }); + + afterEach(() => { + Object.defineProperty(process, 'platform', { + value: oldPlatform + }); + }); + + test('appends .exe to binary if win32 platform', () => { + Object.defineProperty(process, 'platform', { + value: 'win32' + }); + + // require config after setting mock platform + const config = require('../lib/config'); + + expect(config.binName === 'theme.exe').toBe(true); + }); +}); From 09fc8b719059f71112a602790d95896faea442ad Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 5 Sep 2018 09:48:06 -0400 Subject: [PATCH 2/6] Add test to validate installer --- __mocks__/bin-wrapper.js | 18 ++++++++++ __tests__/install.js | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 __mocks__/bin-wrapper.js create mode 100644 __tests__/install.js diff --git a/__mocks__/bin-wrapper.js b/__mocks__/bin-wrapper.js new file mode 100644 index 00000000..f73689c2 --- /dev/null +++ b/__mocks__/bin-wrapper.js @@ -0,0 +1,18 @@ +/* eslint-disable no-use-before-define */ +const src = jest.fn(() => binWrapperObject); +const dest = jest.fn(() => binWrapperObject); +const path = jest.fn(() => binWrapperObject); +const run = jest.fn((_, cb) => { + cb(); + return binWrapperObject; +}); +const use = jest.fn(() => binWrapperObject); + +const binWrapperObject = {src, dest, path, run, use}; + +module.exports = jest.fn().mockImplementation(() => binWrapperObject); +module.exports.src = src; +module.exports.dest = dest; +module.exports.path = path; +module.exports.run = run; +module.exports.use = use; diff --git a/__tests__/install.js b/__tests__/install.js new file mode 100644 index 00000000..020126a0 --- /dev/null +++ b/__tests__/install.js @@ -0,0 +1,76 @@ +// these exports only exist in the mock +const {src, dest, path, run, use} = require('bin-wrapper'); + +const install = require('../lib/install'); + +jest.mock('../lib/utils'); +jest.mock('../lib/config', () => { + return { + baseURL: 'example.com', + version: '0.0.0', + destination: 'myDir', + binName: 'bin' + }; +}); + +describe('install', () => { + + afterEach(() => { + jest.clearAllMocks(); + }); + + test('returns a promise with undefined return value', () => { + expect(install()).resolves.toBeUndefined(); + }); + + test('correctly sources bin files', async () => { + await install(); + + expect(src).toHaveBeenCalledWith('example.com/v0.0.0/darwin-amd64/theme', 'darwin'); + expect(src).toHaveBeenCalledWith('example.com/v0.0.0/linux-386/theme', 'linux'); + expect(src).toHaveBeenCalledWith('example.com/v0.0.0/linux-amd64/theme', 'linux', 'x64'); + expect(src).toHaveBeenCalledWith('example.com/v0.0.0/windows-386/theme.exe', 'win32'); + expect(src).toHaveBeenCalledWith('example.com/v0.0.0/windows-amd64/theme.exe', 'win32', 'x64'); + expect(src).toHaveBeenCalledTimes(5); + }); + + test('correctly applies destination', async () => { + await install(); + + expect(dest).toHaveBeenCalledWith('myDir'); + expect(dest).toHaveBeenCalledTimes(1); + }); + + test('correctly applies binary name', async () => { + await install(); + expect(use).toHaveBeenCalledWith('bin'); + expect(use).toHaveBeenCalledTimes(1); + }); + + test('gets path', async () => { + await install(); + expect(path).toHaveBeenCalledWith(); + expect(path).toHaveBeenCalledTimes(1); + }); + + test('runs after installation', async () => { + await install(); + expect(run).toHaveBeenCalledWith(['version'], expect.any(Function)); + expect(run).toHaveBeenCalledTimes(1); + }); + + test('runs after installation', async () => { + await install(); + expect(run).toHaveBeenCalledWith(['version'], expect.any(Function)); + expect(run).toHaveBeenCalledTimes(1); + }); + + test('rejects promise if error in installation run', () => { + const errorMessage = 'some err'; + run.mockImplementationOnce((_, cb) => { + cb(errorMessage); + }); + + expect(install()).rejects.toMatch(errorMessage); + }); +}); From 63913421376c5f0c6ef2fb88b3d26061dd9ca557 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 5 Sep 2018 13:37:29 -0400 Subject: [PATCH 3/6] Fix logger not working with multiple pararmeters --- lib/logger.js | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index edda580b..18cbde94 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -20,31 +20,19 @@ module.exports = function logger(logLevel) { return { level, - error(args) { + error(...args) { if (level >= 1) { - if (typeof args === 'string') { - console.log(args); - } else { - console.log(...args); - } + console.log(...args); } }, - info(args) { + info(...args) { if (level >= 2) { - if (typeof args === 'string') { - console.log(args); - } else { - console.log(...args); - } + console.log(...args); } }, - silly(args) { + silly(...args) { if (level >= 3) { - if (typeof args === 'string') { - console.log(args); - } else { - console.log(...args); - } + console.log(...args); } } }; From 311e03d7e87fbf437e43207c8bfe140f92179756 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 5 Sep 2018 13:49:30 -0400 Subject: [PATCH 4/6] Add logger mocks and tests --- __tests__/install.js | 7 ++++ __tests__/logger.js | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 __tests__/logger.js diff --git a/__tests__/install.js b/__tests__/install.js index 020126a0..aefcff20 100644 --- a/__tests__/install.js +++ b/__tests__/install.js @@ -4,6 +4,13 @@ const {src, dest, path, run, use} = require('bin-wrapper'); const install = require('../lib/install'); jest.mock('../lib/utils'); +jest.mock('../lib/logger', () => { + return () => ({ + error: jest.fn(), + info: jest.fn(), + silly: jest.fn() + }); +}); jest.mock('../lib/config', () => { return { baseURL: 'example.com', diff --git a/__tests__/logger.js b/__tests__/logger.js new file mode 100644 index 00000000..e22da7c6 --- /dev/null +++ b/__tests__/logger.js @@ -0,0 +1,97 @@ +const logger = require('../lib/logger'); + +const originalConsole = global.console; + +describe('logger', () => { + beforeAll(() => { + global.console = {log: jest.fn()}; + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + afterAll(() => { + global.console = originalConsole; + }); + + test('imported function returns logger functions', () => { + const log = logger(); + expect(log).toHaveProperty('error', expect.any(Function)); + expect(log).toHaveProperty('info', expect.any(Function)); + expect(log).toHaveProperty('silly', expect.any(Function)); + }); + + test('logs error at log level 0', () => { + const log = logger('silent'); + const str = 'hello'; + const {error, info, silly} = log; + + error(str); + info(str, str); + silly(str, str, str); + + expect(log.level).toBe(0); + expect(console.log).not.toHaveBeenCalled(); + }); + + test('logs error at log level 1', () => { + const log = logger('error'); + const str = 'hello'; + const {error, info, silly} = log; + + error(str); + expect(console.log).toHaveBeenNthCalledWith(1, str); + info(str, str); + silly(str, str, str); + expect(console.log).toHaveBeenCalledTimes(1); + + expect(log.level).toBe(1); + }); + + test('logs error and info at log level 2', () => { + const log = logger('info'); + const str = 'hello'; + const {error, info, silly} = log; + + error(str); + expect(console.log).toHaveBeenNthCalledWith(1, str); + info(str, str); + expect(console.log).toHaveBeenNthCalledWith(2, str, str); + silly(str, str, str); + expect(console.log).toHaveBeenCalledTimes(2); + + expect(log.level).toBe(2); + }); + + test('logs error, info, and silly at log level 3', () => { + const log = logger('silly'); + const str = 'hello'; + const {error, info, silly} = log; + + error(str); + expect(console.log).toHaveBeenNthCalledWith(1, str); + info(str, str); + expect(console.log).toHaveBeenNthCalledWith(2, str, str); + silly(str, str, str); + expect(console.log).toHaveBeenNthCalledWith(3, str, str, str); + expect(console.log).toHaveBeenCalledTimes(3); + + expect(log.level).toBe(3); + }); + + test('defaults to level 2 without input parameters', () => { + const log = logger('info'); + const str = 'hello'; + const {error, info, silly} = log; + + error(str); + expect(console.log).toHaveBeenNthCalledWith(1, str); + info(str, str); + expect(console.log).toHaveBeenNthCalledWith(2, str, str); + silly(str, str, str); + expect(console.log).toHaveBeenCalledTimes(2); + + expect(log.level).toBe(2); + }); +}); From 04f77825364cd3701f48b52257216e97477fe4e0 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 5 Sep 2018 13:50:24 -0400 Subject: [PATCH 5/6] Change name of logger param --- lib/logger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger.js b/lib/logger.js index 18cbde94..c8526db7 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -8,7 +8,7 @@ module.exports = function logger(logLevel) { case 'error': level = 1; break; - case 'all': + case 'info': level = 2; break; case 'silly': From 9ed37c2f7c7b52ca22a28ce27e29858816f2cf67 Mon Sep 17 00:00:00 2001 From: erick Date: Thu, 13 Sep 2018 13:22:24 -0400 Subject: [PATCH 6/6] Clean tests up a bit more --- __tests__/config.js | 2 +- __tests__/logger.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/config.js b/__tests__/config.js index 438dd1b0..32e40fc5 100644 --- a/__tests__/config.js +++ b/__tests__/config.js @@ -19,6 +19,6 @@ describe('config', () => { // require config after setting mock platform const config = require('../lib/config'); - expect(config.binName === 'theme.exe').toBe(true); + expect(config.binName).toBe('theme.exe'); }); }); diff --git a/__tests__/logger.js b/__tests__/logger.js index e22da7c6..68bd72a7 100644 --- a/__tests__/logger.js +++ b/__tests__/logger.js @@ -81,7 +81,7 @@ describe('logger', () => { }); test('defaults to level 2 without input parameters', () => { - const log = logger('info'); + const log = logger(); const str = 'hello'; const {error, info, silly} = log;