Skip to content

Commit

Permalink
style: Clean up test structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Sep 9, 2016
1 parent 1f3724d commit 147c861
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 513 deletions.
532 changes: 20 additions & 512 deletions test/indexr.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions test/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ export const paths = {
outputFolder: path.resolve(__dirname, '..', './fixtures/output'),
};

export const deleteFiles = (...deletePaths) => {
deletePaths.forEach((filePath) => {
if (fileExists(filePath)) {
fs.unlinkSync(filePath);
}
});
};
2 changes: 1 addition & 1 deletion test/chainMap.js → test/modules/chainMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from 'chai';
import { chain, map } from '../lib/utils/chainMap';
import { chain, map } from '../../lib/utils/chainMap';

describe('chain', () => {

Expand Down
20 changes: 20 additions & 0 deletions test/modules/checkErrorPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import indexr from '../../lib';
import { assert } from 'chai';
import { resetLog, setLogLevel, logHistory } from '../../lib/utils/logger';
export default () => {
it('should throw an error in a nice way', (endTest) => {
resetLog();
setLogLevel('none');
indexr(/blow/)
.catch(() => {
const actual = logHistory()[0];
const expected = {
level: 'error',
message: 'indexr >> ERROR: TypeError: Path must be a string. Received /blow/',
};
assert.deepEqual(expected, actual);
endTest();
});

});
};
124 changes: 124 additions & 0 deletions test/modules/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { runCLI } from '../lib/utils';
import { assert } from 'chai';
export default () => {
it('should support --out', () => {
const actual = runCLI('indexr', '.', '--out', 'index.js');
const expected = {
inputFolder: '.',
options: {
outputFilename: 'index.js',
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --es5', () => {
const actual = runCLI('indexr', '.', '--es5');
const expected = {
inputFolder: '.',
options: {
es5: true,
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --direct-import', () => {
const actual = runCLI('indexr', '.', '--direct-import');
const expected = {
inputFolder: '.',
options: {
directImport: true,
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --direct-import', () => {
const actual = runCLI('indexr', '.', '--direct-import');
const expected = {
inputFolder: '.',
options: {
directImport: true,
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --named-exports', () => {
const actual = runCLI('indexr', '.', '--named-exports');
const expected = {
inputFolder: '.',
options: {
namedExports: true,
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --modules', () => {
const actual = runCLI('indexr', '.', '--modules', '**/fooo/');
const expected = {
inputFolder: '.',
options: {
modules: ['**/fooo/'],
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --modules-ignore', () => {
const actual = runCLI('indexr', '.', '--modules-ignore', '**/fooo/');
const expected = {
inputFolder: '.',
options: {
modulesIgnore: ['**/fooo/'],
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --submodules', () => {
const actual = runCLI('indexr', '.', '--submodules', '**/fooo/');
const expected = {
inputFolder: '.',
options: {
submodules: ['**/fooo/'],
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --submodules-ignore', () => {
const actual = runCLI('indexr', '.', '--submodules-ignore', '**/fooo/');
const expected = {
inputFolder: '.',
options: {
submodulesIgnore: ['**/fooo/'],
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should support --watch', () => {
const actual = runCLI('indexr', '.', '--watch', '**/fooo/');
const expected = {
inputFolder: '.',
options: {
watch: '**/fooo/',
},
};
assert.deepEqual(expected, actual, 'Function did not return expected output.');
});

it('should return correct --exts', () => {
const actual = runCLI('indexr', '.', '--ext', 'js', '--ext', 'jsx');
const expected = {
inputFolder: '.',
options: {
exts: ['js', 'jsx'],
},
};
assert.deepEqual(expected, actual);
});
};
33 changes: 33 additions & 0 deletions test/modules/extendHelp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import extendedHelp from '../../lib/modules/cli/extendedHelp';
import { assert } from 'chai';
import sinon from 'sinon';

export default () => {
it('extendHelp', () => {
const flags = '-f, --some-flag';
const description = 'Some flag';
const long = 'Some long';
const coercion = undefined;
const defaults = undefined;
const allmessages = [];

sinon.stub(console, 'log', (msg) => allmessages.push(msg));

// const option = sinon.spy();
const option = () => {};
const on = (tag, func) => func();

extendedHelp({ option, on }, [{
flags,
description,
coercion,
defaults,
long,
}]);
console.log.restore();
assert.deepEqual(
allmessages,
['\n Some flag\n ----------\n -f, --some-flag\n\n Some long\n\n']
);
});
};
14 changes: 14 additions & 0 deletions test/modules/getFileList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assert } from 'chai';
import getFileList from '../../lib/utils/getFileList';
export default () => {
it('should blow up if it is given a non string path', (endTest) => {
getFileList(/1234/, ['one'])
.then(() => {
endTest('No error was thrown but it should have.');
})
.catch((e) => {
assert(e);
endTest();
});
});
};
44 changes: 44 additions & 0 deletions test/modules/handleDeprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import handleDeprecation from '../../lib/modules/args/handleDeprecation';
import { assert } from 'chai';
import sinon from 'sinon';

export default () => {
const deprecated = {
include: {
sub: 'submodules',
message: 'This is a message',
},
};

const warnFunc = sinon.spy();

it('should handle a deprecated object', () => {
const actual = handleDeprecation(deprecated, {
include: 'foo',
warnFunc,
});

const expected = {
submodules: 'foo',
};

assert(warnFunc.called);
assert.deepEqual(actual, expected, 'handleDeprecation');
});

it('should not allow a deprecated prop through but instead copy its value to the new prop', () => {
const actual = handleDeprecation(deprecated, {
include: 'foo',
other: 'prop',
submodules: 'bar',
warnFunc,
});

const expected = {
other: 'prop',
submodules: 'foo',
};
assert(warnFunc.called);
assert.deepEqual(actual, expected, 'handleDeprecation');
});
};
21 changes: 21 additions & 0 deletions test/modules/ignoreNodeModulesByDefault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import indexr from '../../lib';
import { fileExists, paths } from '../lib/utils';
import path from 'path';
import { assert } from 'chai';
const {
inputFolder,
} = paths;

export default () => {
it('should ignore node_modules', (endTest) => {
indexr(inputFolder)
.then(() => {
const actual = fileExists(path.resolve(inputFolder, 'module-3/node_modules/modules/index.r.js'));
const expected = false;
assert.equal(expected, actual);
endTest();
})
.catch(endTest);

});
};
14 changes: 14 additions & 0 deletions test/modules/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assert } from 'chai';
import { resetLog, setLogLevel, logHistory, info } from '../../lib/utils/logger';

export default () => {
it('should actually log stuff and print to console', () => {
resetLog();
setLogLevel('none');
info('foo');
const actual = logHistory('info')[0];
const expected = { level: 'info', message: 'indexr >> foo' };
assert.deepEqual(expected, actual);
setLogLevel('none');
});
};
Loading

0 comments on commit 147c861

Please sign in to comment.