@ukhomeoffice/cop-react-form-renderer 5.37.0 → 5.39.1

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.
@@ -113,13 +113,13 @@ var FormComponent = function FormComponent(_ref) {
113
113
  }));
114
114
  }
115
115
  if (component.type === _models.ComponentTypes.COLLECTION_SUMMARY) {
116
- return /*#__PURE__*/_react.default.createElement(_CollectionSummary.default, _extends({}, attrs, {
116
+ return (0, _wrapInFormGroup.default)(_objectSpread(_objectSpread({}, component), (0, _helpers.getComponentError)(component, validation === null || validation === void 0 ? void 0 : validation.errors)), /*#__PURE__*/_react.default.createElement(_CollectionSummary.default, _extends({}, attrs, {
117
117
  config: component,
118
118
  formData: formData,
119
119
  onAction: onAction,
120
120
  onChange: onChange,
121
121
  pages: pages
122
- }));
122
+ })));
123
123
  }
124
124
  var changeMetaDocuments = function changeMetaDocuments(document) {
125
125
  var componentPath = component.full_path || component.fieldId;
@@ -7,10 +7,6 @@ exports.default = void 0;
7
7
  var _models = require("../../../models");
8
8
  var SHOULD_HAVE_FIELDSET = [_models.ComponentTypes.RADIOS, _models.ComponentTypes.CHECKBOXES, _models.ComponentTypes.DATE];
9
9
  var _default = function _default(component) {
10
- // Check to see if this is explicitly set first, as this is efficient.
11
- if (component.fieldset === true) {
12
- return true;
13
- }
14
- return SHOULD_HAVE_FIELDSET.includes(component.type);
10
+ return component.fieldset || SHOULD_HAVE_FIELDSET.includes(component.type);
15
11
  };
16
12
  exports.default = _default;
@@ -82,15 +82,32 @@ var onCYAAction = function onCYAAction(setPagePoint, action, pages, validate, co
82
82
  });
83
83
  }
84
84
  }
85
- if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
85
+ if (action.type === _models.PageAction.TYPES.SAVE_AND_NAVIGATE) {
86
86
  if (canSubmit) {
87
87
  var _submissionData3 = _utils.default.Format.form({
88
88
  pages: pages,
89
89
  components: components
90
90
  }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
91
- _submissionData3.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData3, currentTask, false);
91
+ _submissionData3.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData3, currentTask, true);
92
92
  setData(_submissionData3);
93
93
  hooks.onSubmit(action.type, _submissionData3, function () {
94
+ setSubmitting(false);
95
+ onPageChange(action.page);
96
+ }, function (errors) {
97
+ setSubmitting(false);
98
+ _handlers.default.submissionError(errors, addErrors);
99
+ });
100
+ }
101
+ }
102
+ if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
103
+ if (canSubmit) {
104
+ var _submissionData4 = _utils.default.Format.form({
105
+ pages: pages,
106
+ components: components
107
+ }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
108
+ _submissionData4.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData4, currentTask, false);
109
+ setData(_submissionData4);
110
+ hooks.onSubmit(action.type, _submissionData4, function () {
94
111
  if (type === _models.FormTypes.TASK) {
95
112
  onPageChange(undefined);
96
113
  } else {
@@ -447,6 +447,123 @@ describe('components.FormRenderer.onCYAAction', function () {
447
447
  expect(addErrorsArgs[0]).toEqual(['an example error']);
448
448
  });
449
449
  });
450
+ describe('with a page action of PageAction.TYPES.SAVE_AND_NAVIGATE', function () {
451
+ it('should validate if action.validate is not defined', function () {
452
+ var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
453
+ action: {
454
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
455
+ page: 'testPage'
456
+ },
457
+ hub: _models.HubFormats.TASK
458
+ });
459
+ _onCYAAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
460
+ expect(_helpers.default.canCYASubmitCalls).toEqual(1); // Validating.
461
+ expect(_utils.default.Format.formCalls).toEqual(1);
462
+ expect(_helpers.default.getSubmissionStatusCalls).toEqual(1);
463
+ expect(setDataCalls).toEqual(1);
464
+ expect(setDataArgs[0]).toEqual({
465
+ id: 'formId',
466
+ formStatus: 'Good to go!'
467
+ });
468
+ expect(MOCK_HOOKS.onSubmitCalls).toEqual(1);
469
+ expect(MOCK_HOOKS.onSubmitArgs[0]).toEqual({
470
+ type: CUSTOM_ARGS.action.type,
471
+ payload: {
472
+ id: 'formId',
473
+ formStatus: 'Good to go!'
474
+ }
475
+ });
476
+
477
+ // Checking the onSuccess branch.
478
+ expect(onPageChangeCalls).toEqual(1);
479
+ expect(onPageChangeArgs[0]).toEqual('testPage');
480
+
481
+ // Checking the onError branch.
482
+ expect(_handlers.default.submissionErrorCalls).toEqual(1);
483
+ expect(_handlers.default.submissionErrorArgs[0]).toEqual(['an example error']);
484
+ expect(addErrorsCalls).toEqual(1);
485
+ expect(addErrorsArgs[0]).toEqual(['an example error']);
486
+ });
487
+ it('should validate if action.validate is true', function () {
488
+ var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
489
+ action: {
490
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
491
+ page: 'testPage',
492
+ validate: true
493
+ },
494
+ hub: _models.HubFormats.TASK
495
+ });
496
+ _onCYAAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
497
+ expect(_helpers.default.canCYASubmitCalls).toEqual(1); // Validating.
498
+ expect(_utils.default.Format.formCalls).toEqual(1);
499
+ expect(_helpers.default.getSubmissionStatusCalls).toEqual(1);
500
+ expect(setDataCalls).toEqual(1);
501
+ expect(setDataArgs[0]).toEqual({
502
+ id: 'formId',
503
+ formStatus: 'Good to go!'
504
+ });
505
+ expect(MOCK_HOOKS.onSubmitCalls).toEqual(1);
506
+ expect(MOCK_HOOKS.onSubmitArgs[0]).toEqual({
507
+ type: CUSTOM_ARGS.action.type,
508
+ payload: {
509
+ id: 'formId',
510
+ formStatus: 'Good to go!'
511
+ }
512
+ });
513
+
514
+ // Checking the onSuccess branch.
515
+ expect(onPageChangeCalls).toEqual(1);
516
+ expect(onPageChangeArgs[0]).toEqual('testPage');
517
+
518
+ // Checking the onError branch.
519
+ expect(_handlers.default.submissionErrorCalls).toEqual(1);
520
+ expect(_handlers.default.submissionErrorArgs[0]).toEqual(['an example error']);
521
+ expect(addErrorsCalls).toEqual(1);
522
+ expect(addErrorsArgs[0]).toEqual(['an example error']);
523
+ });
524
+ it('should not validate if action.validate is false', function () {
525
+ var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
526
+ action: {
527
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
528
+ page: 'testPage',
529
+ validate: false
530
+ },
531
+ hub: _models.HubFormats.TASK
532
+ });
533
+ _onCYAAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
534
+
535
+ // Validation.
536
+ expect(_helpers.default.canCYASubmitCalls).toEqual(0);
537
+
538
+ // Everything after this point should be normal, we just
539
+ // haven't validated this time round.
540
+ expect(_utils.default.Format.formCalls).toEqual(1);
541
+ expect(_helpers.default.getSubmissionStatusCalls).toEqual(1);
542
+ expect(setDataCalls).toEqual(1);
543
+ expect(setDataArgs[0]).toEqual({
544
+ id: 'formId',
545
+ formStatus: 'Good to go!'
546
+ });
547
+ expect(MOCK_HOOKS.onSubmitCalls).toEqual(1);
548
+ expect(MOCK_HOOKS.onSubmitArgs[0]).toEqual({
549
+ type: CUSTOM_ARGS.action.type,
550
+ payload: {
551
+ id: 'formId',
552
+ formStatus: 'Good to go!'
553
+ }
554
+ });
555
+
556
+ // Checking the onSuccess branch.
557
+ expect(onPageChangeCalls).toEqual(1);
558
+ expect(onPageChangeArgs[0]).toEqual('testPage');
559
+
560
+ // Checking the onError branch.
561
+ expect(_handlers.default.submissionErrorCalls).toEqual(1);
562
+ expect(_handlers.default.submissionErrorArgs[0]).toEqual(['an example error']);
563
+ expect(addErrorsCalls).toEqual(1);
564
+ expect(addErrorsArgs[0]).toEqual(['an example error']);
565
+ });
566
+ });
450
567
  describe('with an action type of PageAction.TYPES.SAVE_AND_RETURN', function () {
451
568
  it('should validate if action.validate is not defined', function () {
452
569
  var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
@@ -45,12 +45,11 @@ var getComponentToUse = function getComponentToUse(toUse, formComponents) {
45
45
  _loop();
46
46
  }
47
47
  if (component) {
48
- component = _objectSpread(_objectSpread(_objectSpread({}, component), toUse), {}, {
49
- cya_label: toUse.cya_label || component.cya_label || component.label,
50
- fieldId: toUse.fieldId || component.fieldId
51
- });
48
+ component = _objectSpread(_objectSpread({
49
+ cya_label: component.label
50
+ }, component), toUse);
52
51
  if (parent && parent.id !== component.id) {
53
- parent.components[0] = component;
52
+ parent.components = [component];
54
53
  } else {
55
54
  wrapper = component;
56
55
  }
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _models = require("../../models");
8
+ var _getCollectionPageData = _interopRequireDefault(require("../CollectionPage/getCollectionPageData"));
8
9
  var _optionIsSelected = _interopRequireDefault(require("../Component/optionIsSelected"));
9
10
  var _showComponent = _interopRequireDefault(require("../Component/showComponent"));
10
11
  var _additional = _interopRequireDefault(require("./additional"));
@@ -38,6 +39,9 @@ var validateComponent = function validateComponent(component, outerData, formDat
38
39
  var properties;
39
40
  var data = outerData && _typeof(outerData) === 'object' ? outerData : {};
40
41
  var value = data[component.fieldId];
42
+ if (component.type === _models.ComponentTypes.COLLECTION_SUMMARY) {
43
+ value = (0, _getCollectionPageData.default)(component.collectionName, fd);
44
+ }
41
45
  if (component.required) {
42
46
  error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
43
47
  }
@@ -4,6 +4,8 @@ var _models = require("../../models");
4
4
  var _validateComponent = _interopRequireDefault(require("./validateComponent"));
5
5
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
6
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
8
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
9
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
10
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
9
11
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } // Local imports
@@ -46,6 +48,17 @@ describe('utils.Validate.Component', function () {
46
48
  error: "".concat(LABEL, " is required")
47
49
  });
48
50
  });
51
+ it('should return a required error when the component is required and of type collection summary', function () {
52
+ var ID = 'field';
53
+ var LABEL = 'Field';
54
+ var COMPONENT = _objectSpread(_objectSpread({}, setup(ID, _models.ComponentTypes.COLLECTION_SUMMARY, LABEL, true)), {}, {
55
+ collectionName: 'testCollection'
56
+ });
57
+ expect((0, _validateComponent.default)(COMPONENT, {})).toEqual({
58
+ id: ID,
59
+ error: "".concat(LABEL, " is required")
60
+ });
61
+ });
49
62
  it('should return no error when the component is of type email but not required', function () {
50
63
  var ID = 'field';
51
64
  var LABEL = 'Field';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.37.0",
3
+ "version": "5.39.1",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",