diff --git a/content-types/bar.yml b/content-types/bar.yml index c94808a..6efd3b7 100644 --- a/content-types/bar.yml +++ b/content-types/bar.yml @@ -20,7 +20,6 @@ attributes: reference: settings: contentType: services - view: select - type: reference id: foo-reference name: Foo reference diff --git a/lib/init/settings.js b/lib/init/settings.js index 5545c6f..6323cb5 100644 --- a/lib/init/settings.js +++ b/lib/init/settings.js @@ -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; }); diff --git a/lib/utils.js b/lib/utils.js index 618f9a7..4232489 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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); @@ -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, @@ -306,7 +309,10 @@ const references = (types) => { }); }); - return refs; + return { + cts, + references: refs, + }; }; diff --git a/tests/utils.js b/tests/utils.js index c0bee83..8d5b96a 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -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', @@ -268,9 +268,9 @@ test('Reference array has values', t => { view: 'select', }, reference: true, + type: 'select', }, id: 'service-reference-0', - type: 'select', }, ], }, @@ -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 => {