Skip to content

Commit

Permalink
added tests for file output
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonpecora committed Jun 1, 2017
1 parent 16ed138 commit 5a0dd34
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/io/output-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const _ = require('lodash'),
fs = require('fs-extra'),
yaml = require('js-yaml'),
h = require('highland'),
logger = require('../utils/logger'),
types = [
'uris',
'pages',
Expand All @@ -26,10 +25,11 @@ function saveBootstrap(filename) {

_.set(obj, `components[${name}].instances[${id}]`, data);
} else if (type === 'components') { // component default data
const name = name = parts[2],
instances = _.get(obj, `components[${name}]`); // grab instances if they've already been added
const name = parts[2],
instances = _.get(obj, `components[${name}].instances`),
objToMerge = instances ? { instances } : {}; // grab instances if they've already been added

_.set(obj, `components[${name}]`, _.assign({}, data, instances));
_.set(obj, `components[${name}]`, _.assign({}, data, objToMerge));
} else if (_.includes(types, type)) { // some other type
const id = parts[2]; // the string after the type will be the ID for other types

Expand All @@ -38,7 +38,6 @@ function saveBootstrap(filename) {
return obj;
}, {});

logger.debug(`Attempting to write to ${filename}`);
if (path.extname(filename) === '.json') {
// write to a json file (creating directory if it doesn't exist)
return h(fs.outputJson(filename, bootstrapObj)
Expand Down
151 changes: 151 additions & 0 deletions lib/io/output-files.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const fs = require('fs-extra'),
lib = require('./output-files');

describe('output files', () => {
let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(fs);
fs.outputJson.returns(Promise.resolve());
fs.outputFile.returns(Promise.resolve());
});

afterEach(() => {
sandbox.restore();
});

describe('saveBootstrap', () => {
const fn = lib.saveBootstrap,
filePath = 'path/to/file',
jsonFile = `${filePath}.json`,
ymlFile = `${filePath}.yml`,
yamlFile = `${filePath}.yaml`,
data = { a: 'b' };

it('generates bootstrap data with component default data', (done) => {
fn(jsonFile)([{ '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, { components: { foo: data }});
done(err);
});
});

it('generates bootstrap data with component instance data', (done) => {
fn(jsonFile)([{ '/components/foo/instances/bar': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, { components: { foo: { instances: { bar: data }} }});
done(err);
});
});

it('generates bootstrap data with component default and instance data', (done) => {
fn(jsonFile)([{ '/components/foo': data }, { '/components/foo/instances/bar': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, { components: { foo: { a: 'b', instances: { bar: data }} }});
done(err);
});
});

it('generates bootstrap data with component default and instance data, in any order', (done) => {
fn(jsonFile)([{ '/components/foo/instances/bar': data }, { '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, { components: { foo: { a: 'b', instances: { bar: data }} }});
done(err);
});
});

it('generates bootstrap data for pages, uris, users, lists', (done) => {
fn(jsonFile)([
{ '/pages/foo': data },
{ '/uris/foo': 'abc' },
{ '/users/foo': data },
{ '/lists/foo': [1, 2, 3] }
]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, {
pages: {
foo: data
},
uris: {
foo: 'abc'
},
users: {
foo: data
},
lists: {
foo: [1, 2, 3]
}
});
done(err);
});
});

it('ignores types we have not specified', (done) => {
fn(jsonFile)([{ '/foooooooos/foo': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, {});
done(err);
});
});

it('deduplicates chunks', (done) => {
fn(jsonFile)([{ '/components/foo': data }, { '/components/foo': { c: 'd' }}]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile, { components: { foo: { c: 'd' } } });
done(err);
});
});

it('writes to yml if no ext', (done) => {
fn(filePath)([{ '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputFile).to.have.been.calledWith(ymlFile);
done(err);
});
});

it('writes to json', (done) => {
fn(jsonFile)([{ '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputJson).to.have.been.calledWith(jsonFile);
done(err);
});
});

it('writes to yml', (done) => {
fn(ymlFile)([{ '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputFile).to.have.been.calledWith(ymlFile);
done(err);
});
});

it('writes to yaml', (done) => {
fn(yamlFile)([{ '/components/foo': data }]).collect().toCallback((err) => {
expect(fs.outputFile).to.have.been.calledWith(yamlFile);
done(err);
});
});

it('returns success objects when writing json', (done) => {
fn(jsonFile)([{ '/components/foo': data }]).collect().toCallback((err, res) => {
expect(res).to.eql([{ result: 'success', filename: jsonFile }]);
done(err);
});
});

it('emits error objects when writing json', (done) => {
fs.outputJson.returns(Promise.reject(new Error('nope')));
fn(jsonFile)([{ '/components/foo': data }]).collect().toCallback((err, res) => {
expect(res).to.eql([{ result: 'error', filename: jsonFile, message: 'nope' }]);
done(err);
});
});

it('returns success objects when writing yml', (done) => {
fn(ymlFile)([{ '/components/foo': data }]).collect().toCallback((err, res) => {
expect(res).to.eql([{ result: 'success', filename: ymlFile }]);
done(err);
});
});

it('emits error objects when writing yml', (done) => {
fs.outputFile.returns(Promise.reject(new Error('nope')));
fn(ymlFile)([{ '/components/foo': data }]).collect().toCallback((err, res) => {
expect(res).to.eql([{ result: 'error', filename: ymlFile, message: 'nope' }]);
done(err);
});
});
});
});

0 comments on commit 5a0dd34

Please sign in to comment.