Skip to content

Commit

Permalink
Fix _unchecked falsey value on check your answers page
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed May 20, 2022
1 parent 8b21a11 commit 4bcf7cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
5 changes: 1 addition & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ var storeData = function (input, data) {

// Remove _unchecked from arrays of checkboxes
if (Array.isArray(val)) {
var index = val.indexOf('_unchecked')
if (index !== -1) {
val.splice(index, 1)
}
val = val.filter((item) => item !== '_unchecked')
} else if (typeof val === 'object') {
// Store nested objects that aren't arrays
if (typeof data[i] !== 'object') {
Expand Down
50 changes: 50 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,53 @@ describe('getRenderOptions', () => {
expect(document).toContain('<pre tabindex="0">')
})
})

describe('autoStoreData', () => {
const req = {
session: {}
}
const res = { }

beforeEach(() => {
req.body = {}
req.query = {}
req.session.data = {
existingData: 'existing data'
}
res.locals = {}
})

it('strips all properties where the name is prefixed with an underscore when saving the request query to the session and locals data', () => {
req.query = {
_omitMe: 'omit me',
doIncludeMe: 'include me'
}
const expectedData = {
doIncludeMe: 'include me',
existingData: 'existing data'
}
utils.autoStoreData(req, res, () => {
expect(res.locals.data).toEqual(expectedData)
expect(req.session.data).toEqual(expectedData)
})
})

it('removes all occurrences of the value "_unchecked" in checkboxes when saving the request body to the session and locals data', () => {
req.body = {
checkBoxes1: ['_unchecked', 'cb1-1', '_unchecked', '_unchecked', 'cb1-2', '_unchecked'],
checkBoxes2: ['_unchecked', '_unchecked'],
checkBoxes3: ['cb3-1', 'cb3-2'],
existingData: 'existing data'
}
const expectedData = {
checkBoxes1: ['cb1-1', 'cb1-2'],
checkBoxes2: [],
checkBoxes3: ['cb3-1', 'cb3-2'],
existingData: 'existing data'
}
utils.autoStoreData(req, res, () => {
expect(res.locals.data).toEqual(expectedData)
expect(req.session.data).toEqual(expectedData)
})
})
})

0 comments on commit 4bcf7cd

Please sign in to comment.