@ukhomeoffice/cop-react-form-renderer 2.8.4 → 2.10.0

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 (68) hide show
  1. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +8 -12
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +11 -9
  3. package/dist/components/FormComponent/Collection.js +244 -0
  4. package/dist/components/FormComponent/Collection.scss +23 -0
  5. package/dist/components/FormComponent/Container.js +110 -0
  6. package/dist/components/FormComponent/Container.test.js +26 -26
  7. package/dist/components/FormComponent/FormComponent.js +65 -128
  8. package/dist/components/FormComponent/FormComponent.test.js +19 -19
  9. package/dist/components/FormComponent/helpers/getComponentError.js +31 -0
  10. package/dist/components/FormComponent/helpers/getComponentError.test.js +52 -0
  11. package/dist/components/FormComponent/helpers/index.js +15 -0
  12. package/dist/components/FormPage/FormPage.js +7 -11
  13. package/dist/components/FormPage/FormPage.test.js +12 -76
  14. package/dist/components/FormRenderer/FormRenderer.js +103 -44
  15. package/dist/components/FormRenderer/helpers/canActionProceed.js +3 -12
  16. package/dist/components/FormRenderer/helpers/canActionProceed.test.js +41 -65
  17. package/dist/components/FormRenderer/helpers/canCYASubmit.js +3 -24
  18. package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +72 -145
  19. package/dist/components/SummaryList/GroupAction.js +6 -2
  20. package/dist/components/SummaryList/SummaryList.js +33 -18
  21. package/dist/components/SummaryList/SummaryList.scss +12 -8
  22. package/dist/components/SummaryList/SummaryList.test.js +94 -27
  23. package/dist/components/SummaryList/SummaryListRow.js +51 -0
  24. package/dist/components/SummaryList/SummaryListTitleRow.js +30 -0
  25. package/dist/context/ValidationContext/ValidationContext.js +122 -0
  26. package/dist/context/ValidationContext/ValidationContext.test.js +98 -0
  27. package/dist/context/ValidationContext/index.js +23 -0
  28. package/dist/context/index.js +15 -0
  29. package/dist/hooks/index.js +8 -0
  30. package/dist/hooks/useValidation.js +19 -0
  31. package/dist/index.js +11 -5
  32. package/dist/json/group.json +1 -17
  33. package/dist/json/port.json +346 -0
  34. package/dist/json/sublocation.json +859 -0
  35. package/dist/json/terminal.json +81 -0
  36. package/dist/models/CollectionLabels.js +14 -0
  37. package/dist/models/ComponentTypes.js +2 -0
  38. package/dist/models/index.js +8 -0
  39. package/dist/setupTests.js +32 -0
  40. package/dist/utils/CheckYourAnswers/getCYARow.js +14 -0
  41. package/dist/utils/CheckYourAnswers/getCYARow.test.js +34 -11
  42. package/dist/utils/CheckYourAnswers/getCYARowForGroup.js +1 -0
  43. package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +89 -0
  44. package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +194 -0
  45. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +9 -13
  46. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -4
  47. package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +58 -14
  48. package/dist/utils/Component/cleanAttributes.js +1 -1
  49. package/dist/utils/Component/getComponent.js +36 -3
  50. package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +92 -0
  51. package/dist/utils/Component/getDefaultValue.js +25 -0
  52. package/dist/utils/Component/getDefaultValue.test.js +53 -0
  53. package/dist/utils/Component/index.js +4 -1
  54. package/dist/utils/Meta/constants.js +10 -0
  55. package/dist/utils/Meta/documents/getDocuments.js +18 -0
  56. package/dist/utils/Meta/documents/getDocuments.test.js +43 -0
  57. package/dist/utils/Meta/documents/index.js +23 -0
  58. package/dist/utils/Meta/documents/setDocumentForField.js +36 -0
  59. package/dist/utils/Meta/documents/setDocumentForField.test.js +49 -0
  60. package/dist/utils/Meta/index.js +21 -0
  61. package/dist/utils/Validate/validateCollection.js +42 -0
  62. package/dist/utils/Validate/validateCollection.test.js +74 -0
  63. package/dist/utils/Validate/validateComponent.js +67 -48
  64. package/dist/utils/Validate/validateComponent.test.js +247 -188
  65. package/dist/utils/Validate/validateContainer.js +29 -0
  66. package/dist/utils/Validate/validateContainer.test.js +68 -0
  67. package/dist/utils/index.js +4 -1
  68. package/package.json +2 -2
@@ -13,6 +13,8 @@ var _getCYARow = _interopRequireDefault(require("./getCYARow"));
13
13
 
14
14
  var _getCYARowForGroup2 = _interopRequireDefault(require("./getCYARowForGroup"));
15
15
 
16
+ var _getCYARowsForCollection = _interopRequireDefault(require("./getCYARowsForCollection"));
17
+
16
18
  var _getCYARowsForContainer = _interopRequireDefault(require("./getCYARowsForContainer"));
17
19
 
18
20
  var _showComponentCYA = _interopRequireDefault(require("./showComponentCYA"));
@@ -36,11 +38,16 @@ var getCYARowsForPage = function getCYARowsForPage(page, onAction) {
36
38
  var rows = page.components.filter(function (c) {
37
39
  return (0, _showComponentCYA.default)(c, page.formData);
38
40
  }).flatMap(function (component) {
39
- if (component.type === _models.ComponentTypes.CONTAINER) {
40
- return (0, _getCYARowsForContainer.default)(page, component, page.formData[component.fieldId], onAction);
41
- }
41
+ switch (component.type) {
42
+ case _models.ComponentTypes.CONTAINER:
43
+ return (0, _getCYARowsForContainer.default)(page, component, page.formData[component.fieldId], onAction);
42
44
 
43
- return (0, _getCYARow.default)(page, component, onAction);
45
+ case _models.ComponentTypes.COLLECTION:
46
+ return (0, _getCYARowsForCollection.default)(page, component, page.formData[component.fieldId], onAction);
47
+
48
+ default:
49
+ return (0, _getCYARow.default)(page, component, onAction);
50
+ }
44
51
  });
45
52
 
46
53
  if (((_page$groups = page.groups) === null || _page$groups === void 0 ? void 0 : _page$groups.length) > 0) {
@@ -2,23 +2,23 @@
2
2
 
3
3
  var _react = require("@testing-library/react");
4
4
 
5
+ var _setupTests = require("../../setupTests");
6
+
5
7
  var _models = require("../../models");
6
8
 
7
9
  var _getCYARowsForPage = _interopRequireDefault(require("./getCYARowsForPage"));
8
10
 
9
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
12
 
11
- // Global imports
12
- // Local imports
13
+ 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; }
14
+
15
+ 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; }
16
+
17
+ 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; }
18
+
13
19
  describe('utils', function () {
14
20
  describe('CheckYourAnswers', function () {
15
21
  describe('getCYARowsForPage', function () {
16
- var expectObjectLike = function expectObjectLike(received, expected) {
17
- Object.keys(expected).forEach(function (key) {
18
- expect(received[key]).toEqual(expected[key]);
19
- });
20
- };
21
-
22
22
  it('should get a appropriate row for a page with a single readonly text component', function () {
23
23
  var COMPONENT = {
24
24
  type: 'text',
@@ -40,7 +40,7 @@ describe('utils', function () {
40
40
  var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
41
41
  expect(ROWS.length).toEqual(1);
42
42
  ROWS.forEach(function (row, index) {
43
- expectObjectLike(row, {
43
+ (0, _setupTests.expectObjectLike)(row, {
44
44
  pageId: PAGE.id,
45
45
  fieldId: PAGE.components[index].fieldId,
46
46
  key: PAGE.components[index].label,
@@ -78,14 +78,14 @@ describe('utils', function () {
78
78
  var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
79
79
  expect(ROWS.length).toEqual(2);
80
80
  ROWS.forEach(function (row, index) {
81
- expectObjectLike(row, {
81
+ (0, _setupTests.expectObjectLike)(row, {
82
82
  pageId: PAGE.id,
83
83
  fieldId: PAGE.components[index].fieldId,
84
84
  key: PAGE.components[index].label,
85
85
  component: PAGE.components[index],
86
86
  value: "".concat(PAGE.components[index].label, " Charlie")
87
87
  });
88
- expectObjectLike(row.action, {
88
+ (0, _setupTests.expectObjectLike)(row.action, {
89
89
  onAction: ON_ACTION
90
90
  });
91
91
  });
@@ -122,14 +122,14 @@ describe('utils', function () {
122
122
  var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
123
123
  expect(ROWS.length).toEqual(2);
124
124
  ROWS.forEach(function (row, index) {
125
- expectObjectLike(row, {
125
+ (0, _setupTests.expectObjectLike)(row, {
126
126
  pageId: PAGE.id,
127
127
  fieldId: PAGE.components[index].fieldId,
128
128
  key: PAGE.components[index].label,
129
129
  component: PAGE.components[index],
130
130
  value: "".concat(PAGE.components[index].label, " Charlie")
131
131
  });
132
- expectObjectLike(row.action, {
132
+ (0, _setupTests.expectObjectLike)(row.action, {
133
133
  onAction: ON_ACTION
134
134
  });
135
135
  });
@@ -166,7 +166,7 @@ describe('utils', function () {
166
166
  var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
167
167
  expect(ROWS.length).toEqual(1);
168
168
  ROWS.forEach(function (row, index) {
169
- expectObjectLike(row, {
169
+ (0, _setupTests.expectObjectLike)(row, {
170
170
  pageId: PAGE.id,
171
171
  fieldId: CONTAINER.components[index].fieldId,
172
172
  key: CONTAINER.components[index].label,
@@ -234,6 +234,50 @@ describe('utils', function () {
234
234
  expect(addressValues[2].childNodes[0].textContent).toEqual('London');
235
235
  expect(addressValues[3].childNodes[0].textContent).toEqual('SW1A 2AA');
236
236
  });
237
+ it('should get appropriate rows for a page with a single readonly text component within a collection', function () {
238
+ var FORM_DATA = {
239
+ collection: [{
240
+ a: 'Bravo'
241
+ }]
242
+ };
243
+ var COMPONENT = {
244
+ type: 'text',
245
+ readonly: true,
246
+ id: 'a',
247
+ fieldId: 'a',
248
+ label: 'Alpha'
249
+ };
250
+ var COLLECTION = {
251
+ id: 'collection',
252
+ fieldId: 'collection',
253
+ type: _models.ComponentTypes.COLLECTION,
254
+ item: [COMPONENT],
255
+ value: FORM_DATA.collection,
256
+ formData: FORM_DATA
257
+ };
258
+ var PAGE = {
259
+ id: 'page',
260
+ components: [COLLECTION],
261
+ formData: FORM_DATA
262
+ };
263
+
264
+ var ON_ACTION = function ON_ACTION() {};
265
+
266
+ var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
267
+ expect(ROWS.length).toEqual(2); // Title and item row
268
+
269
+ (0, _setupTests.expectObjectLike)(ROWS[1], {
270
+ pageId: PAGE.id,
271
+ fieldId: COMPONENT.fieldId,
272
+ full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
273
+ key: COMPONENT.label,
274
+ action: null,
275
+ component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
276
+ full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId)
277
+ }),
278
+ value: 'Bravo'
279
+ });
280
+ });
237
281
  });
238
282
  });
239
283
  });
@@ -19,7 +19,7 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
19
19
 
20
20
  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; }
21
21
 
22
- var JSON_ONLY_PROPERTIES = ['source', 'use', 'show_when', 'options', 'additionalValidation', 'full_path'];
22
+ var JSON_ONLY_PROPERTIES = ['source', 'use', 'show_when', 'options', 'additionalValidation', 'full_path', 'shown', 'formData'];
23
23
  /**
24
24
  * This method removes and properties that are entirely specific to the JSON
25
25
  * configuration and that should not be used as HTML attributes.
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.getNestedComponent = exports.default = void 0;
7
7
 
8
8
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
9
 
@@ -84,6 +84,13 @@ var getRadios = function getRadios(config) {
84
84
  options = val;
85
85
  });
86
86
 
87
+ options.forEach(function (option) {
88
+ if (!option.nested) {
89
+ return;
90
+ }
91
+
92
+ option.nestedJSX = getNestedComponent(config, option);
93
+ });
87
94
  var attrs = (0, _cleanAttributes.default)(config);
88
95
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.Radios, _extends({}, attrs, {
89
96
  options: options
@@ -148,6 +155,34 @@ var getComponentByType = function getComponentByType(config) {
148
155
  }
149
156
  }
150
157
  };
158
+ /**
159
+ * Get nested component with form data
160
+ * @param {*} parentConfig
161
+ * @param {*} nestedConfig
162
+ */
163
+
164
+
165
+ var getNestedComponent = function getNestedComponent(parentConfig, nestedConfig) {
166
+ nestedConfig.nested.onChange = parentConfig.onChange;
167
+
168
+ if (parentConfig.formData) {
169
+ var _parentConfig$formDat;
170
+
171
+ nestedConfig.nested.value = parentConfig !== null && parentConfig !== void 0 && (_parentConfig$formDat = parentConfig.formData) !== null && _parentConfig$formDat !== void 0 && _parentConfig$formDat[nestedConfig.nested.fieldId] ? parentConfig.formData[nestedConfig.nested.fieldId] : '';
172
+ }
173
+
174
+ if ('readonly' in nestedConfig.nested) delete nestedConfig.nested.readonly;
175
+
176
+ if (parentConfig.readonly) {
177
+ nestedConfig.nested.readonly = parentConfig.readonly;
178
+ return getComponent(nestedConfig.nested, false);
179
+ }
180
+
181
+ return getComponent(nestedConfig.nested);
182
+ };
183
+
184
+ exports.getNestedComponent = getNestedComponent;
185
+
151
186
  /**
152
187
  * Get a renderable component, based on a configuration object.
153
188
  * @param {object} config The configuration object for the component.
@@ -155,8 +190,6 @@ var getComponentByType = function getComponentByType(config) {
155
190
  * @param {Function} fnOverride An optional override function for component rendering.
156
191
  * @returns A renderable component.
157
192
  */
158
-
159
-
160
193
  var getComponent = function getComponent(config) {
161
194
  var wrap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
162
195
  var fnOverride = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ var _react = require("@testing-library/react");
4
+
5
+ var _getComponent = require("../getComponent");
6
+
7
+ 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; }
8
+
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
10
+
11
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
12
+
13
+ describe('utils.Component.get', function () {
14
+ it('should return a component that is nested within another component', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
15
+ var ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG, _render, container, child, label, input;
16
+
17
+ return regeneratorRuntime.wrap(function _callee$(_context) {
18
+ while (1) {
19
+ switch (_context.prev = _context.next) {
20
+ case 0:
21
+ ID = 'testId';
22
+ FIELD_ID = 'fieldId';
23
+ LABEL = 'Test label';
24
+ VALUE = 'nestedValue';
25
+ PARENT_CONFIG = {
26
+ onChange: function onChange() {},
27
+ formData: _defineProperty({}, FIELD_ID, VALUE)
28
+ };
29
+ NESTED_CONFIG = {
30
+ nested: {
31
+ id: ID,
32
+ fieldId: FIELD_ID,
33
+ label: LABEL,
34
+ type: 'text'
35
+ }
36
+ };
37
+ _render = (0, _react.render)((0, _getComponent.getNestedComponent)(PARENT_CONFIG, NESTED_CONFIG)), container = _render.container;
38
+ child = container.childNodes[0];
39
+ expect(child.classList).toContain('govuk-form-group');
40
+ label = child.childNodes[0];
41
+ expect(label).toBeDefined();
42
+ expect(label.innerHTML).toContain(LABEL);
43
+ input = child.childNodes[1];
44
+ expect(input.tagName).toEqual('INPUT');
45
+ expect(input.classList).toContain('govuk-input');
46
+ expect(input.id).toEqual(ID);
47
+ expect(input.value).toEqual(VALUE);
48
+
49
+ case 17:
50
+ case "end":
51
+ return _context.stop();
52
+ }
53
+ }
54
+ }, _callee);
55
+ })));
56
+ it('should return a read only component that is nested within another readonly component', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
57
+ var ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG, _render2, container, child;
58
+
59
+ return regeneratorRuntime.wrap(function _callee2$(_context2) {
60
+ while (1) {
61
+ switch (_context2.prev = _context2.next) {
62
+ case 0:
63
+ ID = 'testId';
64
+ FIELD_ID = 'fieldId';
65
+ LABEL = 'Test label';
66
+ VALUE = 'nestedValue';
67
+ PARENT_CONFIG = {
68
+ onChange: function onChange() {},
69
+ formData: _defineProperty({}, FIELD_ID, VALUE),
70
+ readonly: true
71
+ };
72
+ NESTED_CONFIG = {
73
+ nested: {
74
+ id: ID,
75
+ fieldId: FIELD_ID,
76
+ label: LABEL,
77
+ type: 'text',
78
+ readonly: true
79
+ }
80
+ };
81
+ _render2 = (0, _react.render)((0, _getComponent.getNestedComponent)(PARENT_CONFIG, NESTED_CONFIG)), container = _render2.container;
82
+ child = container.childNodes[0];
83
+ expect(child.classList).toContain('hods-readonly');
84
+
85
+ case 9:
86
+ case "end":
87
+ return _context2.stop();
88
+ }
89
+ }
90
+ }, _callee2);
91
+ })));
92
+ });
@@ -0,0 +1,25 @@
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
+ var _DEFAULT_VALUES;
11
+
12
+ 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; }
13
+
14
+ var DEFAULT_VALUES = (_DEFAULT_VALUES = {}, _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.COLLECTION, []), _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.CONTAINER, {}), _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.FILE, {}), _DEFAULT_VALUES);
15
+
16
+ var getDefaultValue = function getDefaultValue(component) {
17
+ if (component) {
18
+ return DEFAULT_VALUES[component.type] || '';
19
+ }
20
+
21
+ return '';
22
+ };
23
+
24
+ var _default = getDefaultValue;
25
+ exports.default = _default;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _getDefaultValue = _interopRequireDefault(require("./getDefaultValue"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ describe('utils.Component.defaultValue', function () {
10
+ it('should return an empty string if the component is undefined', function () {
11
+ expect((0, _getDefaultValue.default)(undefined)).toEqual('');
12
+ });
13
+ it('should return an empty string if the component is null', function () {
14
+ expect((0, _getDefaultValue.default)(null)).toEqual('');
15
+ });
16
+ it('should return an empty string if the component has no type', function () {
17
+ expect((0, _getDefaultValue.default)({})).toEqual('');
18
+ });
19
+ it('should return an empty string if the component type is null', function () {
20
+ expect((0, _getDefaultValue.default)({
21
+ type: null
22
+ })).toEqual('');
23
+ });
24
+ it('should return an empty string if the component type is undefined', function () {
25
+ expect((0, _getDefaultValue.default)({
26
+ type: undefined
27
+ })).toEqual('');
28
+ });
29
+ it("should return an empty array if the component type is 'collection'", function () {
30
+ expect((0, _getDefaultValue.default)({
31
+ type: _models.ComponentTypes.COLLECTION
32
+ })).toEqual([]);
33
+ });
34
+ it("should return an empty object if the component type is 'container'", function () {
35
+ expect((0, _getDefaultValue.default)({
36
+ type: _models.ComponentTypes.CONTAINER
37
+ })).toEqual({});
38
+ });
39
+ it("should return an empty object if the component type is 'file'", function () {
40
+ expect((0, _getDefaultValue.default)({
41
+ type: _models.ComponentTypes.FILE
42
+ })).toEqual({});
43
+ });
44
+ Object.values(_models.ComponentTypes).forEach(function (value) {
45
+ if (![_models.ComponentTypes.COLLECTION, _models.ComponentTypes.CONTAINER, _models.ComponentTypes.FILE].includes(value)) {
46
+ it("should return an empty object if the component type is '".concat(value, "'"), function () {
47
+ expect((0, _getDefaultValue.default)({
48
+ type: _models.ComponentTypes.TEXT
49
+ })).toEqual('');
50
+ });
51
+ }
52
+ });
53
+ });
@@ -9,6 +9,8 @@ var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
9
9
 
10
10
  var _getComponent = _interopRequireDefault(require("./getComponent"));
11
11
 
12
+ var _getDefaultValue = _interopRequireDefault(require("./getDefaultValue"));
13
+
12
14
  var _isEditable = _interopRequireDefault(require("./isEditable"));
13
15
 
14
16
  var _showComponent = _interopRequireDefault(require("./showComponent"));
@@ -19,8 +21,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
21
 
20
22
  var Component = {
21
23
  clean: _cleanAttributes.default,
22
- get: _getComponent.default,
24
+ defaultValue: _getDefaultValue.default,
23
25
  editable: _isEditable.default,
26
+ get: _getComponent.default,
24
27
  show: _showComponent.default,
25
28
  wrap: _wrapInFormGroup.default
26
29
  };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.META_PROPERTY = exports.META_DOCUMENTS_PROPERTY = void 0;
7
+ var META_PROPERTY = 'meta';
8
+ exports.META_PROPERTY = META_PROPERTY;
9
+ var META_DOCUMENTS_PROPERTY = 'documents';
10
+ exports.META_DOCUMENTS_PROPERTY = META_DOCUMENTS_PROPERTY;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ // Local imports
11
+ var getDocuments = function getDocuments(formData) {
12
+ var _formData$META_PROPER;
13
+
14
+ return (formData === null || formData === void 0 ? void 0 : (_formData$META_PROPER = formData[_constants.META_PROPERTY]) === null || _formData$META_PROPER === void 0 ? void 0 : _formData$META_PROPER[_constants.META_DOCUMENTS_PROPERTY]) || [];
15
+ };
16
+
17
+ var _default = getDocuments;
18
+ exports.default = _default;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ var _constants = require("../constants");
4
+
5
+ var _getDocuments = _interopRequireDefault(require("./getDocuments"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ 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; }
10
+
11
+ describe('Utils.Meta.documents.get', function () {
12
+ it('should return an empty array if formData is null', function () {
13
+ expect((0, _getDocuments.default)(null)).toEqual([]);
14
+ });
15
+ it('should return an empty array if formData does not contain a meta property', function () {
16
+ expect((0, _getDocuments.default)({})).toEqual([]);
17
+ });
18
+ it('should return an empty array if the meta property is undefined', function () {
19
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, undefined);
20
+
21
+ expect((0, _getDocuments.default)(FORM_DATA)).toEqual([]);
22
+ });
23
+ it('should return an empty array if the meta property contains no documents property', function () {
24
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, {});
25
+
26
+ expect((0, _getDocuments.default)(FORM_DATA)).toEqual([]);
27
+ });
28
+ it('should return an empty array if the meta.documents property is undefined', function () {
29
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, undefined));
30
+
31
+ expect((0, _getDocuments.default)(FORM_DATA)).toEqual([]);
32
+ });
33
+ it('should return the meta.documents property if it exists', function () {
34
+ var DOCUMENTS = [{
35
+ field: 'alpha',
36
+ url: 'http://alpha-bravo.com/files/charlie'
37
+ }];
38
+
39
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, DOCUMENTS));
40
+
41
+ expect((0, _getDocuments.default)(FORM_DATA)).toEqual(DOCUMENTS);
42
+ });
43
+ });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ var _setDocumentForField = _interopRequireDefault(require("./setDocumentForField"));
11
+
12
+ var _getDocuments = _interopRequireDefault(require("./getDocuments"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ // Local imports
17
+ var documents = {
18
+ setForField: _setDocumentForField.default,
19
+ get: _getDocuments.default,
20
+ name: _constants.META_DOCUMENTS_PROPERTY
21
+ };
22
+ var _default = documents;
23
+ exports.default = _default;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ var _getDocuments = _interopRequireDefault(require("./getDocuments"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ 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; }
15
+
16
+ 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; }
17
+
18
+ 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; }
19
+
20
+ var setDocumentForField = function setDocumentForField(document, formData, field) {
21
+ var documents = (0, _getDocuments.default)(formData).filter(function (d) {
22
+ return d.field !== field;
23
+ });
24
+
25
+ if (document) {
26
+ documents.push(_objectSpread(_objectSpread({}, document), {}, {
27
+ field: field
28
+ }));
29
+ }
30
+
31
+ var existing = formData ? formData[_constants.META_PROPERTY] : undefined;
32
+ return _objectSpread(_objectSpread({}, existing), {}, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, documents));
33
+ };
34
+
35
+ var _default = setDocumentForField;
36
+ exports.default = _default;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ var _constants = require("../constants");
4
+
5
+ var _setDocumentForField = _interopRequireDefault(require("./setDocumentForField"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ 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; }
10
+
11
+ describe('Utils.Meta.documents.add', function () {
12
+ var ALPHA_DOCUMENT = {
13
+ field: 'alpha',
14
+ url: 'http://alpha-bravo.com/files/alpha'
15
+ };
16
+ var BRAVO_DOCUMENT = {
17
+ field: 'bravo',
18
+ url: 'http://alpha-bravo.com/files/bravo'
19
+ };
20
+ it('should return an appropriately set up object if formData is null', function () {
21
+ expect((0, _setDocumentForField.default)(ALPHA_DOCUMENT, null, ALPHA_DOCUMENT.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
22
+ });
23
+ it('should add a new document to the array', function () {
24
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
25
+
26
+ expect((0, _setDocumentForField.default)(BRAVO_DOCUMENT, FORM_DATA, BRAVO_DOCUMENT.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT, BRAVO_DOCUMENT]));
27
+ });
28
+ it('should replace an existing document with the same field', function () {
29
+ var NEW_URL = 'http://replacement.com/files/alpha';
30
+ var NEW_ALPHA = {
31
+ field: ALPHA_DOCUMENT.field,
32
+ url: NEW_URL
33
+ };
34
+
35
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
36
+
37
+ expect((0, _setDocumentForField.default)(NEW_ALPHA, FORM_DATA, NEW_ALPHA.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [NEW_ALPHA]));
38
+ });
39
+ it('should handle a null document being passed where a document with that field already exists', function () {
40
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
41
+
42
+ expect((0, _setDocumentForField.default)(null, FORM_DATA, ALPHA_DOCUMENT.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, []));
43
+ });
44
+ it('should handle a null document being passed where a document with that field does not already exist', function () {
45
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
46
+
47
+ expect((0, _setDocumentForField.default)(null, FORM_DATA, BRAVO_DOCUMENT.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
48
+ });
49
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.Meta = void 0;
7
+
8
+ var _constants = require("./constants");
9
+
10
+ var _documents = _interopRequireDefault(require("./documents"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // Local imports
15
+ var Meta = {
16
+ documents: _documents.default,
17
+ name: _constants.META_PROPERTY
18
+ };
19
+ exports.Meta = Meta;
20
+ var _default = Meta;
21
+ exports.default = _default;