@ukhomeoffice/cop-react-form-renderer 0.1.0-beta → 1.0.0-gamma

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.
Files changed (92) hide show
  1. package/dist/components/CheckYourAnswers/Answer.js +48 -0
  2. package/dist/components/CheckYourAnswers/Answer.test.js +94 -0
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +94 -23
  4. package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +0 -7
  5. package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +149 -0
  6. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +332 -0
  7. package/dist/components/FormComponent/FormComponent.js +7 -7
  8. package/dist/components/FormComponent/FormComponent.stories.mdx +167 -0
  9. package/dist/components/FormComponent/FormComponent.test.js +137 -0
  10. package/dist/components/FormPage/FormPage.js +12 -11
  11. package/dist/components/FormPage/FormPage.stories.mdx +118 -0
  12. package/dist/components/FormPage/FormPage.test.js +255 -0
  13. package/dist/components/FormRenderer/FormRenderer.js +95 -54
  14. package/dist/components/FormRenderer/FormRenderer.stories.mdx +115 -0
  15. package/dist/components/FormRenderer/FormRenderer.test.js +471 -0
  16. package/dist/components/FormRenderer/handlers/cyaAction.js +28 -0
  17. package/dist/components/FormRenderer/handlers/getPageId.js +28 -0
  18. package/dist/components/FormRenderer/handlers/getPageId.test.js +61 -0
  19. package/dist/components/FormRenderer/handlers/handlers.test.js +97 -0
  20. package/dist/components/FormRenderer/handlers/index.js +23 -0
  21. package/dist/components/FormRenderer/handlers/navigate.js +28 -0
  22. package/dist/components/FormRenderer/handlers/submissionError.js +20 -0
  23. package/dist/components/FormRenderer/helpers/canActionProceed.js +35 -0
  24. package/dist/components/FormRenderer/helpers/canActionProceed.test.js +73 -0
  25. package/dist/components/FormRenderer/helpers/canCYASubmit.js +42 -0
  26. package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +155 -0
  27. package/dist/components/FormRenderer/helpers/getCYA.js +31 -0
  28. package/dist/components/FormRenderer/helpers/getCYA.test.js +38 -0
  29. package/dist/components/FormRenderer/helpers/getFormState.js +32 -0
  30. package/dist/components/FormRenderer/helpers/getFormState.test.js +61 -0
  31. package/dist/components/FormRenderer/helpers/getNextPageId.js +65 -0
  32. package/dist/components/FormRenderer/helpers/getNextPageId.test.js +90 -0
  33. package/dist/components/FormRenderer/helpers/getPage.js +34 -0
  34. package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
  35. package/dist/components/FormRenderer/helpers/index.js +27 -0
  36. package/dist/components/PageActions/ActionButton.js +58 -0
  37. package/dist/components/PageActions/ActionButton.test.js +116 -0
  38. package/dist/components/PageActions/PageActions.js +18 -25
  39. package/dist/components/PageActions/PageActions.stories.mdx +74 -0
  40. package/dist/components/PageActions/PageActions.test.js +157 -0
  41. package/dist/components/SummaryList/RowAction.js +13 -22
  42. package/dist/components/SummaryList/RowAction.test.js +94 -0
  43. package/dist/components/SummaryList/SummaryList.js +6 -20
  44. package/dist/components/SummaryList/SummaryList.scss +7 -0
  45. package/dist/components/SummaryList/SummaryList.stories.mdx +36 -0
  46. package/dist/components/SummaryList/SummaryList.test.js +197 -0
  47. package/dist/components/SummaryList/helpers/getRowActionAttributes.js +27 -0
  48. package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +77 -0
  49. package/dist/components/SummaryList/helpers/index.js +15 -0
  50. package/dist/components/index.js +8 -0
  51. package/dist/hooks/useGetRequest.js +16 -1
  52. package/dist/hooks/useHooks.js +16 -5
  53. package/dist/index.js +3 -1
  54. package/dist/index.test.js +18 -0
  55. package/dist/models/EventTypes.js +14 -0
  56. package/dist/models/FormPages.js +14 -0
  57. package/dist/models/PageAction.js +40 -0
  58. package/dist/models/index.js +24 -0
  59. package/dist/utils/CheckYourAnswers/getCYAAction.js +9 -0
  60. package/dist/utils/CheckYourAnswers/getCYARow.js +10 -0
  61. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -0
  62. package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -5
  63. package/dist/utils/Component/getComponent.js +2 -2
  64. package/dist/utils/Component/getComponent.test.js +1 -1
  65. package/dist/utils/Data/setupFormData.js +3 -3
  66. package/dist/utils/Data/setupFormData.test.js +6 -6
  67. package/dist/utils/Data/setupRefDataUrlForComponent.test.js +4 -4
  68. package/dist/utils/FormPage/getFormPage.js +12 -1
  69. package/dist/utils/FormPage/getFormPage.test.js +178 -0
  70. package/dist/utils/FormPage/getFormPages.js +11 -6
  71. package/dist/utils/FormPage/getFormPages.test.js +95 -0
  72. package/dist/utils/FormPage/getParagraphFromText.js +6 -0
  73. package/dist/utils/FormPage/getParagraphFromText.test.js +29 -0
  74. package/dist/utils/FormPage/useComponent.js +9 -2
  75. package/dist/utils/FormPage/useComponent.test.js +82 -0
  76. package/dist/utils/Format/formatData.js +32 -0
  77. package/dist/utils/Format/formatData.test.js +46 -0
  78. package/dist/utils/Format/formatDataForComponent.js +41 -0
  79. package/dist/utils/Format/formatDataForComponent.test.js +161 -0
  80. package/dist/utils/Format/formatDataForForm.js +33 -0
  81. package/dist/utils/Format/formatDataForForm.test.js +78 -0
  82. package/dist/utils/Format/formatDataForPage.js +37 -0
  83. package/dist/utils/Format/formatDataForPage.test.js +96 -0
  84. package/dist/utils/Format/index.js +26 -0
  85. package/dist/utils/Hub/getFormHub.js +18 -4
  86. package/dist/utils/Hub/getFormHub.test.js +96 -0
  87. package/dist/utils/Hub/index.js +0 -3
  88. package/dist/utils/Validate/validatePage.test.js +1 -1
  89. package/dist/utils/index.js +3 -0
  90. package/package.json +6 -3
  91. package/dist/components/FormRenderer/helpers.js +0 -66
  92. package/dist/components/FormRenderer/helpers.test.js +0 -158
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ var _models = require("../../models");
6
+
7
+ var _formatDataForPage = _interopRequireWildcard(require("./formatDataForPage"));
8
+
9
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
+
11
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
+
13
+ // Local imports
14
+ describe('utils', function () {
15
+ describe('Format', function () {
16
+ var setupField = function setupField(fieldId, format) {
17
+ return {
18
+ fieldId: fieldId,
19
+ format: format
20
+ };
21
+ };
22
+
23
+ describe('formatDataForPage', function () {
24
+ it('should appropriately format a page of components', function () {
25
+ var EVENT_TYPE = _models.EventTypes.SUBMIT;
26
+ var COMPONENTS = [setupField('alpha', {
27
+ type: 'lowercase',
28
+ on: _models.EventTypes.SUBMIT
29
+ }), // Should be formatted
30
+ setupField('bravo', null), // Nothing to format
31
+ setupField('charlie', {
32
+ type: 'uppercase',
33
+ on: _models.EventTypes.SUBMIT
34
+ }), // Should be formatted
35
+ setupField('delta', {
36
+ type: 'uppercase',
37
+ on: _models.EventTypes.BLUR
38
+ }) // Should not be formatted because of event type
39
+ ];
40
+ var DATA = {
41
+ alpha: 'AlPhA',
42
+ bravo: 'bRaVo',
43
+ charlie: 'CHArlie',
44
+ delta: 'deLTA'
45
+ };
46
+ var PAGE = {
47
+ components: COMPONENTS
48
+ };
49
+ (0, _formatDataForPage.default)(PAGE, DATA, EVENT_TYPE);
50
+ expect(DATA).toEqual({
51
+ alpha: 'alpha',
52
+ bravo: 'bRaVo',
53
+ charlie: 'CHARLIE',
54
+ delta: 'deLTA'
55
+ });
56
+ });
57
+ });
58
+ describe('formatDataForPages', function () {
59
+ it('should appropriately format multiple pages', function () {
60
+ var EVENT_TYPE = _models.EventTypes.SUBMIT;
61
+ var COMPONENTS = [setupField('alpha', {
62
+ type: 'lowercase',
63
+ on: _models.EventTypes.SUBMIT
64
+ }), // Should be formatted
65
+ setupField('bravo', null), // Nothing to format
66
+ setupField('charlie', {
67
+ type: 'uppercase',
68
+ on: _models.EventTypes.SUBMIT
69
+ }), // Should be formatted
70
+ setupField('delta', {
71
+ type: 'uppercase',
72
+ on: _models.EventTypes.BLUR
73
+ }) // Should not be formatted because of event type
74
+ ];
75
+ var DATA = {
76
+ alpha: 'AlPhA',
77
+ bravo: 'bRaVo',
78
+ charlie: 'CHArlie',
79
+ delta: 'deLTA'
80
+ };
81
+ var PAGES = [{
82
+ components: COMPONENTS.slice(0, 1)
83
+ }, {
84
+ components: COMPONENTS.slice(2)
85
+ }];
86
+ (0, _formatDataForPage.formatDataForPages)(PAGES, DATA, EVENT_TYPE);
87
+ expect(DATA).toEqual({
88
+ alpha: 'alpha',
89
+ bravo: 'bRaVo',
90
+ charlie: 'CHARLIE',
91
+ delta: 'deLTA'
92
+ });
93
+ });
94
+ });
95
+ });
96
+ });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _formatData = _interopRequireDefault(require("./formatData"));
9
+
10
+ var _formatDataForComponent = _interopRequireDefault(require("./formatDataForComponent"));
11
+
12
+ var _formatDataForForm = _interopRequireDefault(require("./formatDataForForm"));
13
+
14
+ var _formatDataForPage = _interopRequireDefault(require("./formatDataForPage"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ // Local imports
19
+ var Format = {
20
+ component: _formatDataForComponent.default,
21
+ data: _formatData.default,
22
+ form: _formatDataForForm.default,
23
+ page: _formatDataForPage.default
24
+ };
25
+ var _default = Format;
26
+ exports.default = _default;
@@ -5,21 +5,35 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _FormPage = _interopRequireDefault(require("../FormPage"));
9
-
10
8
  var _models = require("../../models");
11
9
 
10
+ var _FormPage = _interopRequireDefault(require("../FormPage"));
11
+
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
14
  // Local imports
15
- var getFormHub = function getFormHub(type, hub, components) {
15
+
16
+ /**
17
+ * Gets a configuration object (or a string) for the hub.
18
+ *
19
+ * @param {string} type The type of the form.
20
+ * @param {object} hub The configuration direct from the JSON.
21
+ * @param {Array} components The top-level form components.
22
+ * @param {object} formData The top-level form data.
23
+ *
24
+ * @returns:
25
+ * undefined if the form type is not "hub-and-spoke", or;
26
+ * The string "CYA" if the CYA screen should be used, or;
27
+ * A page configuration object if the CYA screen should not be used and components are defined.
28
+ */
29
+ var getFormHub = function getFormHub(type, hub, components, formData) {
16
30
  if (type === _models.FormTypes.HUB && hub) {
17
31
  if (hub.format === _models.HubFormats.CYA) {
18
32
  return _models.HubFormats.CYA;
19
33
  }
20
34
 
21
35
  if (hub.components) {
22
- return _FormPage.default.get(hub, components);
36
+ return _FormPage.default.get(hub, components, formData);
23
37
  }
24
38
  }
25
39
 
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _getFormHub = _interopRequireDefault(require("./getFormHub"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
10
+
11
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12
+
13
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
+
15
+ describe('utils', function () {
16
+ describe('Hub', function () {
17
+ describe('getFormHub', function () {
18
+ var FORM_COMPONENTS = [{
19
+ id: 'a',
20
+ fieldId: 'a',
21
+ label: 'Alpha',
22
+ type: 'text'
23
+ }, {
24
+ id: 'b',
25
+ fieldId: 'b',
26
+ label: 'Bravo',
27
+ type: 'text'
28
+ }, // eslint-disable-next-line no-template-curly-in-string
29
+ {
30
+ id: 'c',
31
+ fieldId: 'c',
32
+ label: 'Charlie',
33
+ type: 'radios',
34
+ data: {
35
+ url: '${urls.refData}/v3/charlies'
36
+ }
37
+ }];
38
+ var FORM_DATA = {
39
+ urls: {
40
+ refData: 'https://ho.gov.uk/ref-data/'
41
+ }
42
+ };
43
+ [_models.FormTypes.CYA, _models.FormTypes.FORM, _models.FormTypes.WIZARD].forEach(function (formType) {
44
+ describe("when the FormType is \"".concat(formType, "\""), function () {
45
+ it('should return undefined', function () {
46
+ expect((0, _getFormHub.default)(formType, {}, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
47
+ });
48
+ });
49
+ });
50
+ describe("when the FormType is \"".concat(_models.FormTypes.HUB, "\""), function () {
51
+ it('should return undefined when there is no hub', function () {
52
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, null, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
53
+ });
54
+ it("should return undefined when the format is not \"".concat(_models.HubFormats.CYA, "\" but hub also has no components"), function () {
55
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, {}, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
56
+ });
57
+ it("should return the string \"".concat(_models.HubFormats.CYA, "\" when the format is set to \"").concat(_models.HubFormats.CYA, "\""), function () {
58
+ var HUB = {
59
+ format: _models.HubFormats.CYA
60
+ };
61
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual(_models.HubFormats.CYA);
62
+ });
63
+ it("should return an appropriate page when the format is not \"".concat(_models.HubFormats.CYA, "\" and hub contains components"), function () {
64
+ var HUB = {
65
+ title: 'Hub title',
66
+ components: ["Opening paragraph", {
67
+ type: 'heading',
68
+ size: 'l',
69
+ content: 'Hub heading'
70
+ }, "Closing paragraph", {
71
+ use: 'a'
72
+ }]
73
+ };
74
+ var A = FORM_COMPONENTS[0];
75
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual({
76
+ title: HUB.title,
77
+ components: [{
78
+ type: 'html',
79
+ tagName: 'p',
80
+ content: HUB.components[0]
81
+ }, HUB.components[1], {
82
+ type: 'html',
83
+ tagName: 'p',
84
+ content: HUB.components[2]
85
+ }, _objectSpread(_objectSpread({
86
+ use: 'a'
87
+ }, A), {}, {
88
+ cya_label: A.label
89
+ })],
90
+ formData: FORM_DATA
91
+ });
92
+ });
93
+ });
94
+ });
95
+ });
96
+ });
@@ -7,13 +7,10 @@ exports.default = void 0;
7
7
 
8
8
  var _getFormHub = _interopRequireDefault(require("./getFormHub"));
9
9
 
10
- var _models = require("../../models");
11
-
12
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
 
14
12
  // Local imports
15
13
  var Hub = {
16
- formats: _models.HubFormats,
17
14
  get: _getFormHub.default
18
15
  };
19
16
  var _default = Hub;
@@ -27,7 +27,7 @@ describe('utils', function () {
27
27
  expect((0, _validatePage.default)([], {}).length).toEqual(0);
28
28
  });
29
29
  describe('when there is no form data', function () {
30
- it('should return a single error when no components are required', function () {
30
+ it('should return no errors when no components are required', function () {
31
31
  var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', false), setup('b', _models.ComponentTypes.TEXT, 'Bravo', false)];
32
32
  expect((0, _validatePage.default)(COMPONENTS, null).length).toEqual(0);
33
33
  });
@@ -13,6 +13,8 @@ var _Component = _interopRequireDefault(require("./Component"));
13
13
 
14
14
  var _Data = _interopRequireDefault(require("./Data"));
15
15
 
16
+ var _Format = _interopRequireDefault(require("./Format"));
17
+
16
18
  var _FormPage = _interopRequireDefault(require("./FormPage"));
17
19
 
18
20
  var _Hub = _interopRequireDefault(require("./Hub"));
@@ -33,6 +35,7 @@ var Utils = _objectSpread({
33
35
  CheckYourAnswers: _CheckYourAnswers.default,
34
36
  Component: _Component.default,
35
37
  Data: _Data.default,
38
+ Format: _Format.default,
36
39
  FormPage: _FormPage.default,
37
40
  Hub: _Hub.default,
38
41
  meetsCondition: _meetsCondition.default,
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "0.1.0-beta",
3
+ "version": "1.0.0-gamma",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
7
7
  "test": "react-scripts test",
8
8
  "lint": "eslint --ext .js,.jsx src",
9
9
  "storybook:start": "start-storybook --docs --no-manager-cache -p 6007",
10
- "storybook:build": "build-storybook --docs",
10
+ "storybook:build": "build-storybook --docs -s src/assets",
11
+ "storybook:deploy": "yarn deploy-storybook",
11
12
  "storybook": "yarn storybook:start",
13
+ "deploy-storybook": "storybook-to-ghpages -e storybook-static",
12
14
  "compile": "yarn clean && cross-env NODE_ENV=production babel src --out-dir dist --copy-files && yarn post-compile",
13
15
  "post-compile": "rimraf dist/**/*.test.* dist/**/*.stories.* dist/json dist/assets"
14
16
  },
15
17
  "dependencies": {
16
- "@ukhomeoffice/cop-react-components": "^0.5.0",
18
+ "@ukhomeoffice/cop-react-components": "^0.6.0",
17
19
  "axios": "^0.21.1",
18
20
  "govuk-frontend": "^3.13.0",
19
21
  "web-vitals": "^1.0.1"
@@ -22,6 +24,7 @@
22
24
  "@babel/cli": "^7.15.4",
23
25
  "@babel/preset-env": "^7.15.6",
24
26
  "@babel/preset-react": "^7.14.5",
27
+ "@monaco-editor/react": "^4.3.1",
25
28
  "@storybook/addon-a11y": "^6.3.8",
26
29
  "@storybook/addon-actions": "^6.3.8",
27
30
  "@storybook/addon-docs": "^6.3.8",
@@ -1,66 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getPage = exports.getFormState = exports.getCYA = exports.canActionProceed = void 0;
7
-
8
- var _models = require("../../models");
9
-
10
- var _utils = _interopRequireDefault(require("../../utils"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- // Local imports
15
- var getCYA = function getCYA(pageId, hub) {
16
- if (pageId === 'hub' && hub === _models.HubFormats.CYA) {
17
- return {
18
- title: ''
19
- };
20
- } else if (pageId === _models.HubFormats.CYA) {
21
- return {};
22
- }
23
-
24
- return undefined;
25
- };
26
-
27
- exports.getCYA = getCYA;
28
-
29
- var getPage = function getPage(pageId, pages, hub) {
30
- if (pageId) {
31
- if (pageId === 'hub') {
32
- return hub === _models.HubFormats.CYA ? undefined : hub;
33
- }
34
-
35
- return pages.find(function (p) {
36
- return p.id === pageId;
37
- });
38
- }
39
-
40
- return undefined;
41
- };
42
-
43
- exports.getPage = getPage;
44
-
45
- var getFormState = function getFormState(pageId, pages, hub) {
46
- return {
47
- pageId: pageId,
48
- cya: getCYA(pageId, hub),
49
- page: getPage(pageId, pages, hub)
50
- };
51
- };
52
-
53
- exports.getFormState = getFormState;
54
-
55
- var canActionProceed = function canActionProceed(action, page, onError) {
56
- if (action.validate) {
57
- var errors = _utils.default.Validate.page(page.components, page.formData);
58
-
59
- onError(errors);
60
- return errors.length === 0;
61
- }
62
-
63
- return true;
64
- };
65
-
66
- exports.canActionProceed = canActionProceed;
@@ -1,158 +0,0 @@
1
- "use strict";
2
-
3
- var _helpers = require("./helpers");
4
-
5
- var _models = require("../../models");
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
- var HUB = {
23
- id: 'hub',
24
- components: []
25
- };
26
- var PAGES = [{
27
- id: 'alpha',
28
- components: ['x']
29
- }, {
30
- id: 'bravo',
31
- components: ['y', 'z']
32
- }, HUB];
33
- describe('getCYA', function () {
34
- it('should give an empty object if the pageId is "CYA"', function () {
35
- expect((0, _helpers.getCYA)(_models.HubFormats.CYA)).toEqual({});
36
- });
37
- it('should give an object with a blank title if the pageId is "hub" and the hub is the CYA', function () {
38
- expect((0, _helpers.getCYA)('hub', _models.HubFormats.CYA)).toEqual({
39
- title: ''
40
- });
41
- });
42
- it('should give undefined if pageId is "hub" and the hub is NOT the CYA', function () {
43
- expect((0, _helpers.getCYA)('hub', HUB)).toBeUndefined();
44
- });
45
- it('should give undefined if pageId is NOT "hub" and the hub is NOT the CYA', function () {
46
- expect((0, _helpers.getCYA)('bob', HUB)).toBeUndefined();
47
- });
48
- it('should give undefined if pageId is NOT "hub" and the hub is the CYA', function () {
49
- expect((0, _helpers.getCYA)('bob', _models.HubFormats.CYA)).toBeUndefined();
50
- });
51
- });
52
- describe('getPage', function () {
53
- it('should give undefined if the pageId is undefined', function () {
54
- expect((0, _helpers.getPage)(undefined, PAGES, _models.HubFormats.CYA)).toBeUndefined();
55
- });
56
- it('should give undefined if the pageId is "hub" but the hub is the CYA', function () {
57
- expect((0, _helpers.getPage)('hub', PAGES, _models.HubFormats.CYA)).toBeUndefined();
58
- });
59
- it('should give hub if the pageId is "hub" and the hub is NOT the CYA', function () {
60
- expect((0, _helpers.getPage)('hub', PAGES, HUB)).toEqual(HUB);
61
- });
62
- it('should give find the appropriate page if the pageId is not "hub"', function () {
63
- expect((0, _helpers.getPage)('alpha', PAGES, HUB)).toEqual({
64
- id: 'alpha',
65
- components: ['x']
66
- });
67
- });
68
- it('should give undefined if the pageId is not "hub" and not found in the pages collection', function () {
69
- expect((0, _helpers.getPage)('charlie', PAGES, HUB)).toBeUndefined();
70
- });
71
- });
72
- describe('getFormState', function () {
73
- it('should set up accordingly when viewing a hub that is the CYA', function () {
74
- expect((0, _helpers.getFormState)('hub', PAGES, _models.HubFormats.CYA)).toEqual({
75
- pageId: 'hub',
76
- cya: {
77
- title: ''
78
- },
79
- page: undefined
80
- });
81
- });
82
- it('should set up accordingly when viewing a hub that is NOT the CYA', function () {
83
- expect((0, _helpers.getFormState)('hub', PAGES, HUB)).toEqual({
84
- pageId: 'hub',
85
- cya: undefined,
86
- page: HUB
87
- });
88
- });
89
- it('should set up accordingly when viewing the CYA', function () {
90
- expect((0, _helpers.getFormState)(_models.HubFormats.CYA, PAGES, HUB)).toEqual({
91
- pageId: _models.HubFormats.CYA,
92
- cya: {},
93
- page: undefined
94
- });
95
- });
96
- it('should set up accordingly when viewing a standard page', function () {
97
- expect((0, _helpers.getFormState)('bravo', PAGES, HUB)).toEqual({
98
- pageId: 'bravo',
99
- cya: undefined,
100
- page: {
101
- id: 'bravo',
102
- components: ['y', 'z']
103
- }
104
- });
105
- });
106
- });
107
- describe('canActionProceed', function () {
108
- it('should return true when the action does not require validation', function () {
109
- var ACTION = {
110
- validate: false
111
- };
112
- expect((0, _helpers.canActionProceed)(ACTION, {}, function () {})).toBeTruthy();
113
- });
114
- it('should return true when the page is valid', function () {
115
- var ACTION = {
116
- validate: true
117
- };
118
- var PAGE = {
119
- components: [{
120
- id: 'a',
121
- fieldId: 'a',
122
- label: 'Alpha',
123
- required: true
124
- }],
125
- formData: {
126
- a: 'Bravo'
127
- }
128
- };
129
- expect((0, _helpers.canActionProceed)(ACTION, PAGE, function () {})).toBeTruthy();
130
- });
131
- it('should return false when the page is invalid and should have called the onErrors method appropriately', function () {
132
- var ACTION = {
133
- validate: true
134
- };
135
- var PAGE = {
136
- components: [{
137
- id: 'a',
138
- fieldId: 'a',
139
- label: 'Alpha',
140
- required: true
141
- }],
142
- formData: {}
143
- };
144
- var ERRORS = [];
145
-
146
- var ON_ERROR = function ON_ERROR(errors) {
147
- ERRORS.push.apply(ERRORS, _toConsumableArray(errors));
148
- };
149
-
150
- expect((0, _helpers.canActionProceed)(ACTION, PAGE, ON_ERROR)).toBeFalsy();
151
- expect(ERRORS.length).toEqual(1);
152
- expect(ERRORS[0].id).toEqual('a');
153
- expect(ERRORS[0].error).toEqual('Alpha is required');
154
- });
155
- });
156
- });
157
- });
158
- });