Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6374 from bsclifton/fix-separator-part2
Browse files Browse the repository at this point in the history
Updated sanitization logic for separators
  • Loading branch information
bbondy authored Dec 22, 2016
2 parents 4726f5e + 9d755dd commit 1517f60
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 14 additions & 4 deletions app/common/lib/menuUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const isItemValid = (currentItem, previousItem) => {
* - entries which don't have a label or type
*/
module.exports.sanitizeTemplateItems = (template) => {
const result = template.reduce((previousValue, currentValue, currentIndex, array) => {
const reduced = template.reduce((previousValue, currentValue, currentIndex, array) => {
const result = currentIndex === 1 ? [] : previousValue
if (currentIndex === 1) {
if (isItemValid(previousValue)) {
Expand All @@ -177,7 +177,17 @@ module.exports.sanitizeTemplateItems = (template) => {
return result
})

return Array.isArray(result)
? result
: [result]
const result = Array.isArray(reduced)
? reduced
: [reduced]

if (result.length > 0 && result[0] === CommonMenu.separatorMenuItem) {
result.shift()
}

if (result.length > 0 && result[result.length - 1] === CommonMenu.separatorMenuItem) {
result.pop()
}

return result
}
22 changes: 20 additions & 2 deletions test/unit/app/common/lib/menuUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ describe('menuUtil tests', function () {
assert.deepEqual(result, expectedResult)
})
it('removes duplicate menu separators', function () {
const template = [separator, separator, {label: 'lol'}]
const template = [{label: 'lol1'}, separator, separator, {label: 'lol2'}]
const result = menuUtil.sanitizeTemplateItems(template)
const expectedResult = [separator, {label: 'lol'}]
const expectedResult = [{label: 'lol1'}, separator, {label: 'lol2'}]
assert.deepEqual(result, expectedResult)
})
it('removes items which are missing label or type', function () {
Expand All @@ -267,5 +267,23 @@ describe('menuUtil tests', function () {
const expectedResult = [{label: 'lol'}]
assert.deepEqual(result, expectedResult)
})
it('does not allow the list to start with a separator', function () {
const template = [separator, {label: 'lol'}]
const result = menuUtil.sanitizeTemplateItems(template)
const expectedResult = [{label: 'lol'}]
assert.deepEqual(result, expectedResult)
})
it('does not allow the list to end with a separator', function () {
const template = [{label: 'lol'}, separator]
const result = menuUtil.sanitizeTemplateItems(template)
const expectedResult = [{label: 'lol'}]
assert.deepEqual(result, expectedResult)
})
it('does not allow only a separator', function () {
const template = [separator]
const result = menuUtil.sanitizeTemplateItems(template)
const expectedResult = []
assert.deepEqual(result, expectedResult)
})
})
})

0 comments on commit 1517f60

Please sign in to comment.