@ukhomeoffice/cop-react-form-renderer 0.1.0-beta → 1.0.0-delta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/CheckYourAnswers/Answer.js +48 -0
- package/dist/components/CheckYourAnswers/Answer.test.js +94 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +64 -19
- package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +0 -7
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +100 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +317 -0
- package/dist/components/FormComponent/FormComponent.js +7 -7
- package/dist/components/FormComponent/FormComponent.stories.mdx +167 -0
- package/dist/components/FormComponent/FormComponent.test.js +137 -0
- package/dist/components/FormPage/FormPage.js +12 -11
- package/dist/components/FormPage/FormPage.stories.mdx +118 -0
- package/dist/components/FormPage/FormPage.test.js +255 -0
- package/dist/components/FormRenderer/FormRenderer.js +70 -58
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +90 -0
- package/dist/components/FormRenderer/FormRenderer.test.js +469 -0
- package/dist/components/FormRenderer/handlers/cyaAction.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.test.js +61 -0
- package/dist/components/FormRenderer/handlers/handlers.test.js +97 -0
- package/dist/components/FormRenderer/handlers/index.js +23 -0
- package/dist/components/FormRenderer/handlers/navigate.js +28 -0
- package/dist/components/FormRenderer/handlers/submissionError.js +20 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.js +35 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.test.js +73 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.js +42 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +155 -0
- package/dist/components/FormRenderer/helpers/getCYA.js +31 -0
- package/dist/components/FormRenderer/helpers/getCYA.test.js +38 -0
- package/dist/components/FormRenderer/helpers/getFormState.js +32 -0
- package/dist/components/FormRenderer/helpers/getFormState.test.js +61 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.js +65 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.test.js +90 -0
- package/dist/components/FormRenderer/helpers/getPage.js +34 -0
- package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
- package/dist/components/FormRenderer/helpers/index.js +27 -0
- package/dist/components/PageActions/ActionButton.js +58 -0
- package/dist/components/PageActions/ActionButton.test.js +116 -0
- package/dist/components/PageActions/PageActions.js +18 -25
- package/dist/components/PageActions/PageActions.stories.mdx +74 -0
- package/dist/components/PageActions/PageActions.test.js +157 -0
- package/dist/components/SummaryList/RowAction.js +13 -22
- package/dist/components/SummaryList/RowAction.test.js +94 -0
- package/dist/components/SummaryList/SummaryList.js +6 -20
- package/dist/components/SummaryList/SummaryList.scss +7 -0
- package/dist/components/SummaryList/SummaryList.stories.mdx +36 -0
- package/dist/components/SummaryList/SummaryList.test.js +197 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.js +27 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +77 -0
- package/dist/components/SummaryList/helpers/index.js +15 -0
- package/dist/components/index.js +8 -0
- package/dist/hooks/useGetRequest.js +16 -1
- package/dist/hooks/useHooks.js +16 -5
- package/dist/index.js +3 -1
- package/dist/models/EventTypes.js +14 -0
- package/dist/models/FormPages.js +14 -0
- package/dist/models/PageAction.js +40 -0
- package/dist/models/index.js +24 -0
- package/dist/utils/CheckYourAnswers/getCYAAction.js +9 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +10 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -0
- package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -5
- package/dist/utils/Component/getComponent.js +2 -2
- package/dist/utils/Component/getComponent.test.js +1 -1
- package/dist/utils/Data/setupFormData.js +3 -3
- package/dist/utils/Data/setupFormData.test.js +6 -6
- package/dist/utils/Data/setupRefDataUrlForComponent.test.js +4 -4
- package/dist/utils/FormPage/getFormPage.js +12 -1
- package/dist/utils/FormPage/getFormPage.test.js +178 -0
- package/dist/utils/FormPage/getFormPages.js +11 -6
- package/dist/utils/FormPage/getFormPages.test.js +95 -0
- package/dist/utils/FormPage/getParagraphFromText.js +6 -0
- package/dist/utils/FormPage/getParagraphFromText.test.js +29 -0
- package/dist/utils/FormPage/useComponent.js +9 -2
- package/dist/utils/FormPage/useComponent.test.js +82 -0
- package/dist/utils/Format/formatData.js +32 -0
- package/dist/utils/Format/formatData.test.js +46 -0
- package/dist/utils/Format/formatDataForComponent.js +41 -0
- package/dist/utils/Format/formatDataForComponent.test.js +161 -0
- package/dist/utils/Format/formatDataForForm.js +33 -0
- package/dist/utils/Format/formatDataForForm.test.js +78 -0
- package/dist/utils/Format/formatDataForPage.js +37 -0
- package/dist/utils/Format/formatDataForPage.test.js +96 -0
- package/dist/utils/Format/index.js +26 -0
- package/dist/utils/Hub/getFormHub.js +18 -4
- package/dist/utils/Hub/getFormHub.test.js +96 -0
- package/dist/utils/Hub/index.js +0 -3
- package/dist/utils/Validate/validatePage.test.js +1 -1
- package/dist/utils/index.js +3 -0
- package/package.json +13 -9
- package/dist/components/FormRenderer/helpers.js +0 -66
- package/dist/components/FormRenderer/helpers.test.js +0 -158
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _ = _interopRequireDefault(require("."));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
// Local imports
|
|
8
|
+
describe('components', function () {
|
|
9
|
+
describe('FormRenderer', function () {
|
|
10
|
+
describe('handlers', function () {
|
|
11
|
+
describe('cyaAction', function () {
|
|
12
|
+
it('should not call the onChange method when the page is unchanged', function () {
|
|
13
|
+
var CURRENT_PAGE_ID = 'alpha';
|
|
14
|
+
var PAGE = {
|
|
15
|
+
pageId: CURRENT_PAGE_ID
|
|
16
|
+
};
|
|
17
|
+
var ON_CHANGE_CALLS = [];
|
|
18
|
+
|
|
19
|
+
var ON_CHANGE = function ON_CHANGE(pageId) {
|
|
20
|
+
ON_CHANGE_CALLS.push(pageId);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
_.default.cyaAction(PAGE, CURRENT_PAGE_ID, ON_CHANGE);
|
|
24
|
+
|
|
25
|
+
expect(ON_CHANGE_CALLS.length).toEqual(0);
|
|
26
|
+
});
|
|
27
|
+
it('should call the onChange method when the page has changed', function () {
|
|
28
|
+
var CURRENT_PAGE_ID = 'alpha';
|
|
29
|
+
var NEW_PAGE_ID = 'bravo';
|
|
30
|
+
var PAGE = {
|
|
31
|
+
pageId: NEW_PAGE_ID
|
|
32
|
+
};
|
|
33
|
+
var ON_CHANGE_CALLS = [];
|
|
34
|
+
|
|
35
|
+
var ON_CHANGE = function ON_CHANGE(pageId) {
|
|
36
|
+
ON_CHANGE_CALLS.push(pageId);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
_.default.cyaAction(PAGE, CURRENT_PAGE_ID, ON_CHANGE);
|
|
40
|
+
|
|
41
|
+
expect(ON_CHANGE_CALLS.length).toEqual(1);
|
|
42
|
+
expect(ON_CHANGE_CALLS[0]).toEqual(NEW_PAGE_ID);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('navigate', function () {
|
|
46
|
+
it('should not call the onNavigate method when the page is unchanged', function () {
|
|
47
|
+
var CURRENT_PAGE_ID = 'alpha';
|
|
48
|
+
var ACTION = {
|
|
49
|
+
href: "/".concat(CURRENT_PAGE_ID)
|
|
50
|
+
};
|
|
51
|
+
var ON_NAVIGATE_CALLS = [];
|
|
52
|
+
|
|
53
|
+
var ON_NAVIGATE = function ON_NAVIGATE(pageId) {
|
|
54
|
+
ON_NAVIGATE_CALLS.push(pageId);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
_.default.navigate(ACTION, CURRENT_PAGE_ID, ON_NAVIGATE);
|
|
58
|
+
|
|
59
|
+
expect(ON_NAVIGATE_CALLS.length).toEqual(0);
|
|
60
|
+
});
|
|
61
|
+
it('should call the onNavigate method when the page has changed', function () {
|
|
62
|
+
var CURRENT_PAGE_ID = 'alpha';
|
|
63
|
+
var NEW_PAGE_ID = 'bravo';
|
|
64
|
+
var ACTION = {
|
|
65
|
+
href: "/".concat(NEW_PAGE_ID)
|
|
66
|
+
};
|
|
67
|
+
var ON_NAVIGATE_CALLS = [];
|
|
68
|
+
|
|
69
|
+
var ON_NAVIGATE = function ON_NAVIGATE(pageId) {
|
|
70
|
+
ON_NAVIGATE_CALLS.push(pageId);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
_.default.navigate(ACTION, CURRENT_PAGE_ID, ON_NAVIGATE);
|
|
74
|
+
|
|
75
|
+
expect(ON_NAVIGATE_CALLS.length).toEqual(1);
|
|
76
|
+
expect(ON_NAVIGATE_CALLS[0]).toEqual(NEW_PAGE_ID);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
describe('submissionError', function () {
|
|
80
|
+
it('should call the onError method with the same errors', function () {
|
|
81
|
+
var ON_ERROR_CALLS = [];
|
|
82
|
+
|
|
83
|
+
var ON_ERROR = function ON_ERROR(errors) {
|
|
84
|
+
ON_ERROR_CALLS.push(errors);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
var ERRORS = ['alpha', 'bravo', 'charlie'];
|
|
88
|
+
|
|
89
|
+
_.default.submissionError(ERRORS, ON_ERROR);
|
|
90
|
+
|
|
91
|
+
expect(ON_ERROR_CALLS.length).toEqual(1);
|
|
92
|
+
expect(ON_ERROR_CALLS[0]).toEqual(ERRORS);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.handlers = exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _cyaAction = _interopRequireDefault(require("./cyaAction"));
|
|
9
|
+
|
|
10
|
+
var _navigate = _interopRequireDefault(require("./navigate"));
|
|
11
|
+
|
|
12
|
+
var _submissionError = _interopRequireDefault(require("./submissionError"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
var handlers = {
|
|
17
|
+
cyaAction: _cyaAction.default,
|
|
18
|
+
navigate: _navigate.default,
|
|
19
|
+
submissionError: _submissionError.default
|
|
20
|
+
};
|
|
21
|
+
exports.handlers = handlers;
|
|
22
|
+
var _default = handlers;
|
|
23
|
+
exports.default = _default;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _getPageId = _interopRequireDefault(require("./getPageId"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets the pageId from the invoked page action and calls onNavigate if it is
|
|
14
|
+
* different to the currentPageId.
|
|
15
|
+
* @param {object} action The invoked action.
|
|
16
|
+
* @param {string} currentPageId The current pageId.
|
|
17
|
+
* @param {Function} onNavigate The handler to call if the pageId is different to the currentPageId.
|
|
18
|
+
*/
|
|
19
|
+
var navigate = function navigate(action, currentPageId, onNavigate) {
|
|
20
|
+
var pageId = (0, _getPageId.default)(action, currentPageId);
|
|
21
|
+
|
|
22
|
+
if (pageId !== currentPageId) {
|
|
23
|
+
onNavigate(pageId);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
var _default = navigate;
|
|
28
|
+
exports.default = _default;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Formats back-end errors and invokes an onError handler.
|
|
10
|
+
* @param {Array} errors The errors returned by the backend when submitting.
|
|
11
|
+
* @param {Function} onError The handler to call with the formatted errors.
|
|
12
|
+
*/
|
|
13
|
+
var submissionError = function submissionError(errors, onError) {
|
|
14
|
+
// For now, simply pass them straight through as we don't yet know
|
|
15
|
+
// what the errors will look like.
|
|
16
|
+
onError(errors);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
var _default = submissionError;
|
|
20
|
+
exports.default = _default;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = _interopRequireDefault(require("../../../utils"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
// Local imports
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Assesses whether the action can proceed when navigating, submitting, etc.
|
|
16
|
+
* Presently, this will validate a page when submitting and, only if the page is
|
|
17
|
+
* valid will it proceed. If it's simple navigation, it simply returns true.
|
|
18
|
+
* @param {object} action The action object we're assessing.
|
|
19
|
+
* @param {object} page The page configuration object this action relates to.
|
|
20
|
+
* @param {Function} onError A function to call with any validation errors.
|
|
21
|
+
* @returns A boolean where `true` means the action can proceed and `false` means it cannot.
|
|
22
|
+
*/
|
|
23
|
+
var canActionProceed = function canActionProceed(action, page, onError) {
|
|
24
|
+
if (action.validate) {
|
|
25
|
+
var errors = _utils.default.Validate.page(page.components, page.formData);
|
|
26
|
+
|
|
27
|
+
onError(errors);
|
|
28
|
+
return errors.length === 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var _default = canActionProceed;
|
|
35
|
+
exports.default = _default;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _canActionProceed = _interopRequireDefault(require("./canActionProceed"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
8
|
+
|
|
9
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
+
|
|
11
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
12
|
+
|
|
13
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
14
|
+
|
|
15
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
16
|
+
|
|
17
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
18
|
+
|
|
19
|
+
describe('components', function () {
|
|
20
|
+
describe('FormRenderer', function () {
|
|
21
|
+
describe('helpers', function () {
|
|
22
|
+
describe('canActionProceed', function () {
|
|
23
|
+
it('should return true when the action does not require validation', function () {
|
|
24
|
+
var ACTION = {
|
|
25
|
+
validate: false
|
|
26
|
+
};
|
|
27
|
+
expect((0, _canActionProceed.default)(ACTION, {}, function () {})).toBeTruthy();
|
|
28
|
+
});
|
|
29
|
+
it('should return true when the page is valid', function () {
|
|
30
|
+
var ACTION = {
|
|
31
|
+
validate: true
|
|
32
|
+
};
|
|
33
|
+
var PAGE = {
|
|
34
|
+
components: [{
|
|
35
|
+
id: 'a',
|
|
36
|
+
fieldId: 'a',
|
|
37
|
+
label: 'Alpha',
|
|
38
|
+
required: true
|
|
39
|
+
}],
|
|
40
|
+
formData: {
|
|
41
|
+
a: 'Bravo'
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
expect((0, _canActionProceed.default)(ACTION, PAGE, function () {})).toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
it('should return false when the page is invalid and should have called the onErrors method appropriately', function () {
|
|
47
|
+
var ACTION = {
|
|
48
|
+
validate: true
|
|
49
|
+
};
|
|
50
|
+
var PAGE = {
|
|
51
|
+
components: [{
|
|
52
|
+
id: 'a',
|
|
53
|
+
fieldId: 'a',
|
|
54
|
+
label: 'Alpha',
|
|
55
|
+
required: true
|
|
56
|
+
}],
|
|
57
|
+
formData: {}
|
|
58
|
+
};
|
|
59
|
+
var ERRORS = [];
|
|
60
|
+
|
|
61
|
+
var ON_ERROR = function ON_ERROR(errors) {
|
|
62
|
+
ERRORS.push.apply(ERRORS, _toConsumableArray(errors));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
expect((0, _canActionProceed.default)(ACTION, PAGE, ON_ERROR)).toBeFalsy();
|
|
66
|
+
expect(ERRORS.length).toEqual(1);
|
|
67
|
+
expect(ERRORS[0].id).toEqual('a');
|
|
68
|
+
expect(ERRORS[0].error).toEqual('Alpha is required');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = _interopRequireDefault(require("../../../utils"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
13
|
+
|
|
14
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
15
|
+
|
|
16
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
17
|
+
|
|
18
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
19
|
+
|
|
20
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
21
|
+
|
|
22
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Assesses whether the CYA screen can submit.
|
|
26
|
+
* Presently, this will validate all of the form pages and, only if all pages are
|
|
27
|
+
* valid will it proceed.
|
|
28
|
+
* @param {Array} pages All of the pages contained within the form.
|
|
29
|
+
* @param {Function} onError A function to call with any validation errors.
|
|
30
|
+
* @returns A boolean where `true` means the CYA can submit and `false` means it cannot.
|
|
31
|
+
*/
|
|
32
|
+
var canCYASubmit = function canCYASubmit(pages, onError) {
|
|
33
|
+
var errors = [];
|
|
34
|
+
pages.forEach(function (p) {
|
|
35
|
+
errors.push.apply(errors, _toConsumableArray(_utils.default.Validate.page(p.components, p.formData)));
|
|
36
|
+
});
|
|
37
|
+
onError(errors);
|
|
38
|
+
return errors.length === 0;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
var _default = canCYASubmit;
|
|
42
|
+
exports.default = _default;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _canCYASubmit = _interopRequireDefault(require("./canCYASubmit"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
8
|
+
|
|
9
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
+
|
|
11
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
12
|
+
|
|
13
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
14
|
+
|
|
15
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
16
|
+
|
|
17
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
18
|
+
|
|
19
|
+
describe('components', function () {
|
|
20
|
+
describe('FormRenderer', function () {
|
|
21
|
+
describe('helpers', function () {
|
|
22
|
+
describe('canCYASubmit', function () {
|
|
23
|
+
it('should return true when all pages are valid', function () {
|
|
24
|
+
var PAGE_1 = {
|
|
25
|
+
components: [{
|
|
26
|
+
id: 'a',
|
|
27
|
+
fieldId: 'a',
|
|
28
|
+
label: 'Alpha',
|
|
29
|
+
required: true
|
|
30
|
+
}],
|
|
31
|
+
formData: {
|
|
32
|
+
a: 'Bravo'
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var PAGE_2 = {
|
|
36
|
+
components: [{
|
|
37
|
+
id: 'c',
|
|
38
|
+
fieldId: 'c',
|
|
39
|
+
label: 'Charlie',
|
|
40
|
+
required: true
|
|
41
|
+
}],
|
|
42
|
+
formData: {
|
|
43
|
+
c: 'Delta'
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
expect((0, _canCYASubmit.default)([PAGE_1, PAGE_2], function () {})).toBeTruthy();
|
|
47
|
+
});
|
|
48
|
+
it('should return false when the first page is invalid and should have called the onErrors method appropriately', function () {
|
|
49
|
+
var PAGE_1 = {
|
|
50
|
+
components: [{
|
|
51
|
+
id: 'a',
|
|
52
|
+
fieldId: 'a',
|
|
53
|
+
label: 'Alpha',
|
|
54
|
+
required: true
|
|
55
|
+
}],
|
|
56
|
+
formData: {}
|
|
57
|
+
};
|
|
58
|
+
var PAGE_2 = {
|
|
59
|
+
components: [{
|
|
60
|
+
id: 'c',
|
|
61
|
+
fieldId: 'c',
|
|
62
|
+
label: 'Charlie',
|
|
63
|
+
required: true
|
|
64
|
+
}],
|
|
65
|
+
formData: {
|
|
66
|
+
c: 'Delta'
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var ERRORS = [];
|
|
70
|
+
|
|
71
|
+
var ON_ERROR = function ON_ERROR(errors) {
|
|
72
|
+
ERRORS.push.apply(ERRORS, _toConsumableArray(errors));
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
expect((0, _canCYASubmit.default)([PAGE_1, PAGE_2], ON_ERROR)).toBeFalsy();
|
|
76
|
+
expect(ERRORS.length).toEqual(1);
|
|
77
|
+
expect(ERRORS[0]).toEqual({
|
|
78
|
+
id: 'a',
|
|
79
|
+
error: 'Alpha is required'
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
it('should return false when the second page is invalid and should have called the onErrors method appropriately', function () {
|
|
83
|
+
var PAGE_1 = {
|
|
84
|
+
components: [{
|
|
85
|
+
id: 'a',
|
|
86
|
+
fieldId: 'a',
|
|
87
|
+
label: 'Alpha',
|
|
88
|
+
required: true
|
|
89
|
+
}],
|
|
90
|
+
formData: {
|
|
91
|
+
a: 'Bravo'
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var PAGE_2 = {
|
|
95
|
+
components: [{
|
|
96
|
+
id: 'c',
|
|
97
|
+
fieldId: 'c',
|
|
98
|
+
label: 'Charlie',
|
|
99
|
+
required: true
|
|
100
|
+
}],
|
|
101
|
+
formData: {}
|
|
102
|
+
};
|
|
103
|
+
var ERRORS = [];
|
|
104
|
+
|
|
105
|
+
var ON_ERROR = function ON_ERROR(errors) {
|
|
106
|
+
ERRORS.push.apply(ERRORS, _toConsumableArray(errors));
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
expect((0, _canCYASubmit.default)([PAGE_1, PAGE_2], ON_ERROR)).toBeFalsy();
|
|
110
|
+
expect(ERRORS.length).toEqual(1);
|
|
111
|
+
expect(ERRORS[0]).toEqual({
|
|
112
|
+
id: 'c',
|
|
113
|
+
error: 'Charlie is required'
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
it('should return false when the both pages are invalid and should have called the onErrors method appropriately', function () {
|
|
117
|
+
var PAGE_1 = {
|
|
118
|
+
components: [{
|
|
119
|
+
id: 'a',
|
|
120
|
+
fieldId: 'a',
|
|
121
|
+
label: 'Alpha',
|
|
122
|
+
required: true
|
|
123
|
+
}],
|
|
124
|
+
formData: {}
|
|
125
|
+
};
|
|
126
|
+
var PAGE_2 = {
|
|
127
|
+
components: [{
|
|
128
|
+
id: 'c',
|
|
129
|
+
fieldId: 'c',
|
|
130
|
+
label: 'Charlie',
|
|
131
|
+
required: true
|
|
132
|
+
}],
|
|
133
|
+
formData: {}
|
|
134
|
+
};
|
|
135
|
+
var ERRORS = [];
|
|
136
|
+
|
|
137
|
+
var ON_ERROR = function ON_ERROR(errors) {
|
|
138
|
+
ERRORS.push.apply(ERRORS, _toConsumableArray(errors));
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
expect((0, _canCYASubmit.default)([PAGE_1, PAGE_2], ON_ERROR)).toBeFalsy();
|
|
142
|
+
expect(ERRORS.length).toEqual(2);
|
|
143
|
+
expect(ERRORS[0]).toEqual({
|
|
144
|
+
id: 'a',
|
|
145
|
+
error: 'Alpha is required'
|
|
146
|
+
});
|
|
147
|
+
expect(ERRORS[1]).toEqual({
|
|
148
|
+
id: 'c',
|
|
149
|
+
error: 'Charlie is required'
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _models = require("../../../models");
|
|
9
|
+
|
|
10
|
+
// Local imports
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets a configuration object for the Check your answers screen.
|
|
14
|
+
* @param {string} pageId The current page identifier.
|
|
15
|
+
* @param {object} hub The hub, if there is one.
|
|
16
|
+
* @returns A configuration object for the Check your answers screen.
|
|
17
|
+
*/
|
|
18
|
+
var getCYA = function getCYA(pageId, hub) {
|
|
19
|
+
if (pageId === _models.FormPages.HUB && hub === _models.HubFormats.CYA) {
|
|
20
|
+
return {
|
|
21
|
+
title: ''
|
|
22
|
+
};
|
|
23
|
+
} else if (pageId === _models.FormPages.CYA) {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return undefined;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
var _default = getCYA;
|
|
31
|
+
exports.default = _default;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../../models");
|
|
4
|
+
|
|
5
|
+
var _getCYA = _interopRequireDefault(require("./getCYA"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
+
|
|
9
|
+
// Local imports
|
|
10
|
+
describe('components', function () {
|
|
11
|
+
describe('FormRenderer', function () {
|
|
12
|
+
describe('helpers', function () {
|
|
13
|
+
var HUB = {
|
|
14
|
+
id: _models.FormPages.HUB,
|
|
15
|
+
components: []
|
|
16
|
+
};
|
|
17
|
+
describe('getCYA', function () {
|
|
18
|
+
it('should give an empty object if the pageId is "CYA"', function () {
|
|
19
|
+
expect((0, _getCYA.default)(_models.FormPages.CYA)).toEqual({});
|
|
20
|
+
});
|
|
21
|
+
it('should give an object with a blank title if the pageId is "hub" and the hub is the CYA', function () {
|
|
22
|
+
expect((0, _getCYA.default)(_models.FormPages.HUB, _models.HubFormats.CYA)).toEqual({
|
|
23
|
+
title: ''
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it('should give undefined if pageId is "hub" and the hub is NOT the CYA', function () {
|
|
27
|
+
expect((0, _getCYA.default)(_models.FormPages.HUB, HUB)).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
it('should give undefined if pageId is NOT "hub" and the hub is NOT the CYA', function () {
|
|
30
|
+
expect((0, _getCYA.default)(_models.FormPages.HUB, HUB)).toBeUndefined();
|
|
31
|
+
});
|
|
32
|
+
it('should give undefined if pageId is NOT "hub" and the hub is the CYA', function () {
|
|
33
|
+
expect((0, _getCYA.default)('bob', _models.HubFormats.CYA)).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _getCYA = _interopRequireDefault(require("./getCYA"));
|
|
9
|
+
|
|
10
|
+
var _getPage = _interopRequireDefault(require("./getPage"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
// Local imports
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sets up the state of the form, including the current page and the Check your answers screen.
|
|
18
|
+
* @param {string} pageId The current page identifier.
|
|
19
|
+
* @param {Array} pages All of the pages in the form.
|
|
20
|
+
* @param {object} hub The hub, if there is one.
|
|
21
|
+
* @returns The current state of the form.
|
|
22
|
+
*/
|
|
23
|
+
var getFormState = function getFormState(pageId, pages, hub) {
|
|
24
|
+
return {
|
|
25
|
+
pageId: pageId,
|
|
26
|
+
cya: (0, _getCYA.default)(pageId, hub),
|
|
27
|
+
page: (0, _getPage.default)(pageId, pages, hub)
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
var _default = getFormState;
|
|
32
|
+
exports.default = _default;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../../models");
|
|
4
|
+
|
|
5
|
+
var _getFormState = _interopRequireDefault(require("./getFormState"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
+
|
|
9
|
+
// Local imports
|
|
10
|
+
describe('components', function () {
|
|
11
|
+
describe('FormRenderer', function () {
|
|
12
|
+
describe('helpers', function () {
|
|
13
|
+
var HUB = {
|
|
14
|
+
id: _models.FormPages.HUB,
|
|
15
|
+
components: []
|
|
16
|
+
};
|
|
17
|
+
var PAGES = [{
|
|
18
|
+
id: 'alpha',
|
|
19
|
+
components: ['x']
|
|
20
|
+
}, {
|
|
21
|
+
id: 'bravo',
|
|
22
|
+
components: ['y', 'z']
|
|
23
|
+
}, HUB];
|
|
24
|
+
describe('getFormState', function () {
|
|
25
|
+
it('should set up accordingly when viewing a hub that is the CYA', function () {
|
|
26
|
+
expect((0, _getFormState.default)(_models.FormPages.HUB, PAGES, _models.HubFormats.CYA)).toEqual({
|
|
27
|
+
pageId: _models.FormPages.HUB,
|
|
28
|
+
cya: {
|
|
29
|
+
title: ''
|
|
30
|
+
},
|
|
31
|
+
page: undefined
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
it('should set up accordingly when viewing a hub that is NOT the CYA', function () {
|
|
35
|
+
expect((0, _getFormState.default)(_models.FormPages.HUB, PAGES, HUB)).toEqual({
|
|
36
|
+
pageId: _models.FormPages.HUB,
|
|
37
|
+
cya: undefined,
|
|
38
|
+
page: HUB
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
it('should set up accordingly when viewing the CYA', function () {
|
|
42
|
+
expect((0, _getFormState.default)(_models.FormPages.CYA, PAGES, HUB)).toEqual({
|
|
43
|
+
pageId: _models.FormPages.CYA,
|
|
44
|
+
cya: {},
|
|
45
|
+
page: undefined
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it('should set up accordingly when viewing a standard page', function () {
|
|
49
|
+
expect((0, _getFormState.default)('bravo', PAGES, HUB)).toEqual({
|
|
50
|
+
pageId: 'bravo',
|
|
51
|
+
cya: undefined,
|
|
52
|
+
page: {
|
|
53
|
+
id: 'bravo',
|
|
54
|
+
components: ['y', 'z']
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|