Skip to content

Commit

Permalink
Merge 2855a0b into c73541c
Browse files Browse the repository at this point in the history
  • Loading branch information
sophypal committed Aug 7, 2018
2 parents c73541c + 2855a0b commit 80b3589
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 19 deletions.
40 changes: 25 additions & 15 deletions addon/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,7 @@ export function isChildRequiredToSubmitForm (path, bunsenModel, value, parentReq
bunsenModel.required.indexOf(segment) !== -1
)

if (/^\d+$/.test(segment)) {
bunsenModel = bunsenModel.items
} else {
bunsenModel = bunsenModel.properties[segment]
}
bunsenModel = getSubModel(segment, bunsenModel)
}
}

Expand Down Expand Up @@ -344,11 +340,7 @@ export function isRequired (cell, cellDefinitions, bunsenModel, value, parentReq
bunsenModel.required.indexOf(segment) !== -1
)

if (/^\d+$/.test(segment)) {
bunsenModel = bunsenModel.items
} else {
bunsenModel = bunsenModel.properties[segment]
}
bunsenModel = getSubModel(segment, bunsenModel)
}

value = get(value || {}, cell.model)
Expand Down Expand Up @@ -390,6 +382,28 @@ export function getErrorMessage (error) {
}
/* eslint-enable complexity */

/**
* Gets the sub model associated with the path segment
* @param {String} pathSegment - path of the segment (must be direct descendant)
* @param {Object} bunsenModel - starting bunsen model
* @returns {Object} the submodel or undefined if it doesn't exist
*/
export function getSubModel (pathSegment, bunsenModel) {
if (/^\d+$/.test(pathSegment)) {
bunsenModel = bunsenModel.items

if (Array.isArray(bunsenModel)) {
bunsenModel = bunsenModel[parseInt(pathSegment)]
}
} else if ('properties' in bunsenModel) {
bunsenModel = bunsenModel.properties[pathSegment]
} else {
return undefined
}

return bunsenModel
}

/**
* Used to sanity check if the path to the model is valid
* @param {String} path - bunsen model path reference
Expand All @@ -402,11 +416,7 @@ export function isModelPathValid (path, bunsenModel) {
while (segments.length !== 0 && bunsenModel) {
const segment = segments.shift()

if (/^\d+$/.test(segment)) {
bunsenModel = bunsenModel.items
} else {
bunsenModel = bunsenModel.properties[segment]
}
bunsenModel = getSubModel(segment, bunsenModel)
}

return Boolean(bunsenModel)
Expand Down
74 changes: 70 additions & 4 deletions tests/unit/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
generateFacetView,
generateLabelFromModel,
getMergedConfigRecursive,
getSubModel,
isModelPathValid,
isRegisteredEmberDataModel,
isRequired,
Expand Down Expand Up @@ -426,6 +427,75 @@ describe('bunsen-utils', function () {
})
})

describe('getSubModel', function () {
let bunsenModel

describe('object', function () {
beforeEach(function () {
bunsenModel = {
properties: {
foo: {type: 'string'}
},
type: 'object'
}
})

it('should return valid model with valid path', function () {
expect(getSubModel('foo', bunsenModel)).to.eql({type: 'string'})
})

it('should return undefined with invalid path', function () {
expect(getSubModel('bar', bunsenModel)).to.eql(undefined)
})
})

describe('arrays', function () {
beforeEach(function () {
bunsenModel = {
items: {
type: 'string'
},
type: 'array'
}
})

it('should return valid model with valid path', function () {
expect(getSubModel('0', bunsenModel)).to.eql({type: 'string'})
})

it('should return undefined with invalid path', function () {
expect(getSubModel('bar', bunsenModel)).to.eql(undefined)
})
})

describe('heterogenous arrays', function () {
beforeEach(function () {
bunsenModel = {
items: [{
type: 'object',
properties: {
foo: {type: 'string'}
}
}],
type: 'array'
}
})

it('should return valid model with valid path', function () {
expect(getSubModel('0', bunsenModel)).to.eql({
type: 'object',
properties: {
foo: {type: 'string'}
}
})
})

it('should return undefined with invalid path', function () {
expect(getSubModel('bar', bunsenModel)).to.eql(undefined)
})
})
})

describe('isModelPathValid', function () {
let bunsenModel

Expand All @@ -443,10 +513,6 @@ describe('bunsen-utils', function () {
}
})

afterEach(function () {
bunsenModel = null
})

it('returns false when the path is invalid', function () {
expect(isModelPathValid('baz', bunsenModel)).to.equal(false)
expect(isModelPathValid('foo.baz', bunsenModel)).to.equal(false)
Expand Down

0 comments on commit 80b3589

Please sign in to comment.