Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more complete unit tests #28

Merged
merged 6 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __mocks__/bin-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-use-before-define */
const src = jest.fn(() => binWrapperObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't binWrapperObject throw a ReferenceError at this point since const variables are not hoisted and is binWrapperObject is declared below?

Or I guess it is not referenced until this callback is called... in which case binWrapperObject is declared in the parent scope? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly not sure at all. I just tried stuff until it worked for this one, which was tricky since I needed the mutually recursive definitions. In any case, the mock seems to behave as expected. 🤷🏻‍♂️

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;
24 changes: 24 additions & 0 deletions __tests__/config.js
Original file line number Diff line number Diff line change
@@ -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).toBe('theme.exe');
});
});
83 changes: 83 additions & 0 deletions __tests__/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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/logger', () => {
return () => ({
error: jest.fn(),
info: jest.fn(),
silly: jest.fn()
});
});
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of mockImplementationOnce() 👍

cb(errorMessage);
});

expect(install()).rejects.toMatch(errorMessage);
});
});
97 changes: 97 additions & 0 deletions __tests__/logger.js
Original file line number Diff line number Diff line change
@@ -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();
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);
});
});
26 changes: 7 additions & 19 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function logger(logLevel) {
case 'error':
level = 1;
break;
case 'all':
case 'info':
level = 2;
break;
case 'silly':
Expand All @@ -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);
}
}
};
Expand Down