Skip to content

Commit

Permalink
Do not filter out properties on inactive pages that also appear on ac…
Browse files Browse the repository at this point in the history
…tive pages (#155)

* Add functionality to not filter out properties on inactive pages that also appear on active pages

Refactor

Refactor

Remove duplicate properties from array

Return properties

* Write unit tests for new functionality

* Refactor from code review

* Change _.uniq to only be called once

* Run build on latest changes
  • Loading branch information
annekainicUSDS authored Aug 9, 2018
1 parent 807d26b commit 8527e2c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 20 deletions.
48 changes: 35 additions & 13 deletions lib/js/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/js/helpers.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint": "npm run lint:js && npm run lint:sass",
"lint:js": "eslint --quiet --ext .js --ext .jsx .",
"lint:sass": "sass-lint -c test/config/sass-lint.yaml --verbose",
"test": "BABEL_ENV=test mocha --require test/config/unit-test-setup.js --require test/config/enzyme-setup.jsx --compilers js:babel-core/register ./test/**/*.spec.jsx",
"test": "BABEL_ENV=test mocha --require test/config/unit-test-setup.js --require test/config/enzyme-setup.jsx --compilers js:babel-core/register './test/**/*.spec.js?(x)' ",
"webpack": "webpack"
},
"repository": {
Expand Down
29 changes: 25 additions & 4 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { deepEquals } from '@department-of-veterans-affairs/react-jsonschema-for
import FormPage from './containers/FormPage';
import ReviewPage from './review/ReviewPage';

// An active page is one that will be shown to the user.
// Pages become inactive if they are conditionally shown based
// on answers to previous questions.
export function isActivePage(page, data) {
if (typeof page.depends === 'function') {
return page.depends(data, page.index);
Expand All @@ -21,6 +24,16 @@ export function getActivePages(pages, data) {
return pages.filter(page => isActivePage(page, data));
}

export function getActiveProperties(activePages) {
const allProperties = [];
activePages.forEach(page => {
if (page.schema) {
allProperties.push(...Object.keys(page.schema.properties));
}
});
return _.uniq(allProperties);
}

export function getInactivePages(pages, data) {
return pages.filter(page => !isActivePage(page, data));
}
Expand Down Expand Up @@ -212,11 +225,18 @@ export function filterViewFields(data) {
}, {});
}

export function filterInactivePages(pages, form) {
return pages.reduce((formData, page) => {
export function filterInactivePageData(inactivePages, activePages, form) {
const activeProperties = getActiveProperties(activePages);
let newData;

return inactivePages.reduce((formData, page) => {
return Object.keys(page.schema.properties)
.reduce((currentData, prop) => {
return _.unset(prop, currentData);
newData = currentData;
if (!activeProperties.includes(prop)) {
delete newData[prop];
}
return newData;
}, formData);
}, form.data);
}
Expand Down Expand Up @@ -270,8 +290,9 @@ export function isInProgress(pathName) {
* Normal transform for schemaform data
*/
export function transformForSubmit(formConfig, form, replacer = stringifyFormReplacer) {
const activePages = getActivePages(createFormPageList(formConfig), form.data);
const inactivePages = getInactivePages(createFormPageList(formConfig), form.data);
const withoutInactivePages = filterInactivePages(inactivePages, form);
const withoutInactivePages = filterInactivePageData(inactivePages, activePages, form);
const withoutViewFields = filterViewFields(withoutInactivePages);

return JSON.stringify(withoutViewFields, replacer) || '{}';
Expand Down
61 changes: 60 additions & 1 deletion test/js/helpers.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ describe('Schemaform helpers:', () => {
},
chapter2: {
pages: {
page2: {}
page2: {
schema: {
type: 'object',
properties: {}
}
}
}
}
}
Expand All @@ -366,6 +371,60 @@ describe('Schemaform helpers:', () => {
field: 'testing'
});
});
it('should not remove properties that are on both active and inactive pages', () => {
const formConfig = {
chapters: {
chapter1: {
pages: {
page1: {
schema: {
type: 'object',
properties: {
otherField: {
type: 'string'
},
anotherField: {
type: 'string'
}
}
},
depends: {
field: 'something'
}
},
}
},
chapter2: {
pages: {
page2: {
schema: {
type: 'object',
properties: {
anotherField: {
type: 'string'
}
}
}
}
}
}
}
};
const formData = {
data: {
otherField: 'testing2',
anotherField: 'testing3',
field: 'testing'
}
};

const output = JSON.parse(transformForSubmit(formConfig, formData));

expect(output).to.eql({
field: 'testing',
anotherField: 'testing3'
});
});
it('should remove empty addresses', () => {
const formConfig = {
chapters: {
Expand Down

0 comments on commit 8527e2c

Please sign in to comment.