Skip to content

Commit

Permalink
test config
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Dec 17, 2018
1 parent 32f0105 commit 036eb74
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 5 deletions.
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@
"cross-env": "^5.2.0",
"eslint": "^5.10.0",
"eslint-config-google": "^0.11.0",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"npm-run-all": "^4.1.5",
"nyc": "^13.1.0",
"rimraf": "^2.6.2"
"rimraf": "^2.6.2",
"sinon": "^7.2.2",
"sinon-chai": "^3.3.0"
},
"dependencies": {
"inquirer": "^6.2.1",
Expand Down
9 changes: 7 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {requires, entry} from './utils';

export class Schema {
constructor(params) {
this.params = params;
requires(params.require);
this.entry = entry(params.entry);
}

generate() {
return this.params;
const documents = {};
this.entry(documents);
return documents;
}
}
30 changes: 30 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {resolve} from 'path';

function resolveRequire(path) {
if (path.startsWith('.')) {
return resolve(path);
}
return path;
}

export function requires(paths, req = require) {
if (paths) {
if (typeof paths === 'string') {
req(resolveRequire(paths));
} else {
paths.forEach((path) => {
req(resolveRequire(path));
});
}
}
}

export function entry(entry, req = require) {
if (typeof entry === 'object') {
if (entry.export) {
return req(resolveRequire(entry.path))[entry.export];
}
return req(resolveRequire(entry.path));
}
return req(resolveRequire(entry));
}
2 changes: 2 additions & 0 deletions test/helpers/common.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import chai from 'chai';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
chai.should();
2 changes: 2 additions & 0 deletions test/scenarios/basic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (documents) => {
};
12 changes: 10 additions & 2 deletions test/src/schema.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {Schema} from '../../src';

const CONFIG = {};
const CONFIG = {
entry: {
path: './test/scenarios/basic',
export: 'default',
},
require: [
'@babel/register',
],
};

describe('Schema', () => {
it('should pass', () => {
const schema = new Schema(CONFIG);
schema.generate().should.eql(CONFIG);
schema.generate().should.eql({});
});
});
106 changes: 106 additions & 0 deletions test/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import _ from 'lodash';
import sinon from 'sinon';
import {resolve} from 'path';

import {entry, requires} from '../../src/utils';

const RELATIVE_PATH = './name';
const NODE_MODULES_PATH = 'name';
const EXPORT_NAME = 'export name';
const EXPORT = 'export';
const ENTRY = {
[EXPORT_NAME]: EXPORT,
};

describe('utils', () => {
describe('entry', () => {
const variations = {
'with a string': {
'should support a relative path': {
entry: RELATIVE_PATH,
ret: ENTRY,
req: resolve(RELATIVE_PATH),
},
'should support a node_modules path': {
entry: NODE_MODULES_PATH,
ret: ENTRY,
req: NODE_MODULES_PATH,
},
},
'with an object': {
'should support a relative path': {
entry: {path: RELATIVE_PATH},
ret: ENTRY,
req: resolve(RELATIVE_PATH),
},
'should support a node_modules path': {
entry: {path: NODE_MODULES_PATH},
ret: ENTRY,
req: NODE_MODULES_PATH,
},
},
'with an object and export': {
'should support a relative path': {
entry: {path: RELATIVE_PATH, export: EXPORT_NAME},
ret: EXPORT,
req: resolve(RELATIVE_PATH),
},
'should support a node_modules path': {
entry: {path: NODE_MODULES_PATH, export: EXPORT_NAME},
ret: EXPORT,
req: NODE_MODULES_PATH,
},
},
};

_.forEach(variations, (variation, name) => {
describe(name, () => {
_.forEach(variation, (test, name) => {
it(name, () => {
const req = sinon.spy(() => ENTRY);
const ret = entry(test.entry, req);
req.should.have.been.calledOnce;
req.should.have.been.calledWith(test.req);
ret.should.eql(test.ret);
});
});
});
});
});

describe('requires', () => {
describe('when undefined', () => {
it('should not require anything', () => {
const req = sinon.spy();
requires(undefined, req);
req.should.not.have.been.called;
});
});

describe('when a string', () => {
it('should support a relative path', () => {
const req = sinon.spy();
requires(RELATIVE_PATH, req);
req.should.have.been.calledOnce;
req.should.have.been.calledWith(resolve(RELATIVE_PATH));
});

it('should support a node_modules path', () => {
const req = sinon.spy();
requires(NODE_MODULES_PATH, req);
req.should.have.been.calledOnce;
req.should.have.been.calledWith(NODE_MODULES_PATH);
});
});

describe('when an array', () => {
it('should require all', () => {
const req = sinon.spy();
requires([RELATIVE_PATH, NODE_MODULES_PATH], req);
req.should.have.been.calledTwice;
req.should.have.been.calledWith(resolve(RELATIVE_PATH));
req.should.have.been.calledWith(NODE_MODULES_PATH);
});
});
});
});

0 comments on commit 036eb74

Please sign in to comment.