Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expand active order in ReviewChapters #267

Merged
merged 9 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 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.

5 changes: 3 additions & 2 deletions lib/js/review/ReviewChapters.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/review/ReviewChapters.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/js/review/SubmitController.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/review/SubmitController.js.map

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions lib/js/routing.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/routing.js.map

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

23 changes: 19 additions & 4 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ export function expandArrayPages(pageList, data) {
return result.currentList;
}

/**
* Gets active and expanded pages, in the correct order
*
* Any `showPagePerItem` pages are expanded to create items for each array item.
* We update the `path` for each of those pages to replace `:index` with the current item index.
*
* @param pages {Array<Object>} List of page configs
* @param data {Object} Current form data
* @returns {Array<Object>} A list of pages, includeing individual array
* pages that are active
*/
export function getActiveExpandedPages(pages, data) {
const expandedPages = expandArrayPages(pages, data);
return getActivePages(expandedPages, data);
}

/**
* getPageKeys returns a list of keys for the currently active pages
*
Expand All @@ -532,8 +548,7 @@ export function expandArrayPages(pageList, data) {
* and the index if it’s a pagePerItem page
*/
export function getPageKeys(pages, formData) {
const eligiblePageList = getActivePages(pages, formData);
const expandedPageList = expandArrayPages(eligiblePageList, formData);
const expandedPageList = getActiveExpandedPages(pages, formData);

return expandedPageList.map(page => {
let pageKey = page.pageKey;
Expand All @@ -554,8 +569,7 @@ export function getPageKeys(pages, formData) {
export function getActiveChapters(formConfig, formData) {
const formPages = createFormPageList(formConfig);
const pageList = createPageList(formConfig, formPages);
const eligiblePageList = getActivePages(pageList, formData);
const expandedPageList = expandArrayPages(eligiblePageList, formData);
const expandedPageList = getActiveExpandedPages(pageList, formData);

return _.uniq(expandedPageList.map(p => p.chapterKey).filter(key => !!key && key !== 'review'));
}
Expand All @@ -578,3 +592,4 @@ export function omitRequired(schema) {

return newSchema;
}

11 changes: 4 additions & 7 deletions src/js/review/ReviewChapters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import Scroll from 'react-scroll';
import ReviewCollapsibleChapter from './ReviewCollapsibleChapter';
import {
createPageListByChapter,
expandArrayPages,
getActiveExpandedPages,
getActiveChapters,
getActivePages,
getPageKeys
} from '../helpers';
import {
Expand Down Expand Up @@ -75,8 +74,7 @@ class ReviewChapters extends React.Component {
chapters,
form,
formContext,
setValid,
viewedPages
setValid, viewedPages
} = this.props;

return (
Expand Down Expand Up @@ -106,7 +104,7 @@ class ReviewChapters extends React.Component {
}
}

function mapStateToProps(state, ownProps) {
export function mapStateToProps(state, ownProps) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how you all feel about this. We made the changes in here to put more code in mapStateToProps because it's more testable, but in order to test it directly, we need to export it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More testing is always good. In this case it's testing a lot of implementation and plumbing details so not ideal, but the test is clearly marked so if it breaks during a refactor it should be clear why.

// from ownprops
const {
formConfig,
Expand All @@ -125,8 +123,7 @@ function mapStateToProps(state, ownProps) {
const chapters = chapterNames.map(chapterName => {
const pages = pagesByChapter[chapterName];

const activePages = getActivePages(pages, formData);
const expandedPages = expandArrayPages(activePages, formData);
const expandedPages = getActiveExpandedPages(pages, formData);
const chapterFormConfig = formConfig.chapters[chapterName];
const open = openChapters.includes(chapterName);
const pageKeys = getPageKeys(pages, formData);
Expand Down
6 changes: 2 additions & 4 deletions src/js/review/SubmitController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { PreSubmitSection } from '../components/PreSubmitSection';
import { isValidForm } from '../validation';
import {
createPageListByChapter,
expandArrayPages,
getActivePages
getActiveExpandedPages
} from '../helpers';
import {
setPreSubmit,
Expand Down Expand Up @@ -45,8 +44,7 @@ class SubmitController extends React.Component {
router
} = this.props;

const eligiblePageList = getActivePages(pageList, form.data);
const expandedPageList = expandArrayPages(eligiblePageList, this.props.form.data);
const expandedPageList = getActiveExpandedPages(pageList, form.data);

// TODO: Fix this bug that assumes there is a confirmation page.
// Actually, it assumes the app also doesn't add routes at the end!
Expand Down
7 changes: 2 additions & 5 deletions src/js/routing.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import _ from 'lodash/fp';
import { expandArrayPages, getActivePages } from './helpers';
import { getActiveExpandedPages } from './helpers';

/*
* Returns the page list without conditional pages that have not satisfied
* their dependencies and therefore should be skipped.
*/
function getEligiblePages(pageList, data, pathname) {
// Any `showPagePerItem` pages are expanded to create items for each array item.
// We update the `path` for each of those pages to replace `:index` with the current item index.
const expandedPageList = expandArrayPages(pageList, data);
const eligiblePageList = getActivePages(expandedPageList, data);
const eligiblePageList = getActiveExpandedPages(pageList, data);
const pageIndex = _.findIndex(item => item.path === pathname, eligiblePageList);
return { pages: eligiblePageList, pageIndex };
}
Expand Down
41 changes: 40 additions & 1 deletion test/js/review/ReviewChapters.unit.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { mount } from 'enzyme';
import sinon from 'sinon';

import { ReviewChapters } from '../../../src/js/review/ReviewChapters';
import { ReviewChapters, mapStateToProps } from '../../../src/js/review/ReviewChapters';

describe('Schemaform review: ReviewChapters', () => {
it('should handle editing', () => {
Expand Down Expand Up @@ -105,4 +105,43 @@ describe('Schemaform review: ReviewChapters', () => {
instance.handleToggleChapter({ name: 'chapter3', open: true, pageKeys: 0 });
expect(closeReviewChapter.calledWith('chapter3', 0)).to.be.true;
});

it('should pass index to depends for pagePerItem pages', () => {
const formData = {
testArray: [{}]
};

const dependsStub = sinon.stub();
dependsStub.withArgs(formData, 0).returns(true);

mapStateToProps({
form: {
pages: {},
submission: {},
reviewPageView: {
openChapters: [],
viewedPages: new Set()
},
data: formData
}
}, {
formConfig: {
chapters: {
test: {
pages: {
testPage: {
path: '/testing/:index',
pagePerItem: true,
arrayPath: 'testArray',
depends: dependsStub
}
}
}
}
},
pageList: [{}]
});

expect(dependsStub.calledWith(formData, 0));
});
});