Skip to content

Commit

Permalink
🐛 changes type
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush2k committed Aug 17, 2016
1 parent 07e726d commit 1bd9569
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
1 change: 0 additions & 1 deletion content-types/bar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ attributes:
reference:
settings:
contentType: services
view: select
- type: reference
id: foo-reference
name: Foo reference
Expand Down
5 changes: 3 additions & 2 deletions lib/init/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module.exports = (app) => {
return types(raw);
});
}).then(cts => {
const result = utils.references(cts);
app.set('name', config.site.name);
app.set('content-types', cts);
app.set('references', utils.references(cts));
app.set('content-types', result.cts);
app.set('references', result.references);

return app;
});
Expand Down
14 changes: 10 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const format = body => {
* @param {object} types - content types object
*
*
* @returns {array[Reference]} - content type with reference data
* * @returns {object} - content type and reference data
*/
const references = (types) => {
const cts = _.cloneDeep(types);
Expand Down Expand Up @@ -281,15 +281,18 @@ const references = (types) => {
// Checks if input is a reference
if (inputs[input].hasOwnProperty('reference') && inputs[input].reference === true) {
// Throw error if contentType is not defined
if (!inputs[input].settings.hasOwnProperty('contentType')) {
if (!_.get(inputs[input], 'settings.contentType')) {
throw new Error('Reference must have a content type');
}

// Throw error if contentType is not valid
if (!position.hasOwnProperty(inputs[input].settings.contentType)) {
throw new Error(`Content Type ${inputs[input].settings.contentType} is not valid`);
}

if (_.get(inputs[input], 'settings.view') === 'radio') {
inputs[input].type = 'radio';
}

// Defines and push reference object
refs.push({
type: ct.id,
Expand All @@ -306,7 +309,10 @@ const references = (types) => {
});
});

return refs;
return {
cts,
references: refs,
};
};


Expand Down
109 changes: 86 additions & 23 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ test('Reference array has values', t => {
options: [],
settings: {
contentType: 'test-service',
view: 'select',
view: 'radio',
},
reference: true,
type: 'radio',
},
},
id: 'service-reference',
type: 'select',
},
{
name: 'Service Reference',
Expand All @@ -268,9 +268,9 @@ test('Reference array has values', t => {
view: 'select',
},
reference: true,
type: 'select',
},
id: 'service-reference-0',
type: 'select',
},
],
},
Expand All @@ -294,30 +294,93 @@ test('Reference array has values', t => {
],
},
];
const expected = [
{
type: 'test-reference',
attr: 0,
input: 'reference',
ct: {
index: 1,
id: 'test-service',
const expected = {
cts: [
{
id: 'test-reference',
attributes: [
{
name: 'Service Reference',
description: 'Add a reference',
inputs: {
reference: {
name: 'service-reference--reference',
options: [],
settings: {
contentType: 'test-service',
view: 'radio',
},
reference: true,
type: 'radio',
},
},
id: 'service-reference',
},
{
name: 'Service Reference',
description: 'Add a reference',
inputs: [
{
reference: {
name: 'service-reference--reference--0',
options: [],
settings: {
contentType: 'test-service',
view: 'select',
},
reference: true,
type: 'select',
},
id: 'service-reference-0',
},
],
},
],
},
},
{
type: 'test-reference',
attr: 1,
input: 'reference',
length: 1,
ct: {
index: 1,
{
id: 'test-service',
identifier: 'service-name',
attributes: [
{
name: 'Service Name',
description: 'Write a really cool name please.',
inputs: {
text: {
name: 'service-name--text',
},
},
id: 'service-name',
type: 'text',
},
],
},
},
];
],
references: [
{
type: 'test-reference',
attr: 0,
input: 'reference',
ct: {
index: 1,
id: 'test-service',
},
},
{
type: 'test-reference',
attr: 1,
input: 'reference',
length: 1,
ct: {
index: 1,
id: 'test-service',
},
},
],
};
const result = utils.references(types);
t.is(result.length, expected.length, 'length of arrays is equal');
t.is(JSON.stringify(result, null, 2), JSON.stringify(expected, null, 2), 'Reference array created');
console.log(JSON.stringify(result, null, 2));
t.is(result.length, expected.length, 'reference equal length');
t.is(JSON.stringify(result), JSON.stringify(expected), 'cts and reference exists');
});

test('Reference array - Invalid Content type - Fail', t => {
Expand Down

0 comments on commit 1bd9569

Please sign in to comment.