Skip to content

Commit

Permalink
Write unit tests for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kainic authored and Kainic committed Jul 12, 2018
1 parent 2c004fd commit b4f7814
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
9 changes: 6 additions & 3 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
9 changes: 6 additions & 3 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export function getActivePages(pages, data) {
export function getActiveProperties(activePages) {
let allProperties = [];
activePages.map(page => {
allProperties = _.uniq(_.concat(Object.keys(page.schema.properties), allProperties));
if (page.schema) {
allProperties = _.uniq(_.concat(Object.keys(page.schema.properties), allProperties));
}
return allProperties;
});

Expand Down Expand Up @@ -227,14 +229,15 @@ export function filterViewFields(data) {

export function filterInactivePages(inactivePages, activePages, form) {
const activeProperties = getActiveProperties(activePages);
let newData;

return inactivePages.reduce((formData, page) => {
return Object.keys(page.schema.properties)
.reduce((currentData, prop) => {
if (!activeProperties.includes(prop)) {
_.unset(prop, currentData);
newData = _.unset(prop, currentData);
}
return currentData;
return newData;
}, formData);
}, form.data);
}
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 b4f7814

Please sign in to comment.