@ukhomeoffice/cop-react-form-renderer 4.46.0 → 4.47.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.
@@ -311,7 +311,10 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
311
311
  if (action.type === _models.PageAction.TYPES.CANCEL) {
312
312
  hooks.onCancel();
313
313
  return;
314
- } // Re-apply the patch to the page's formData.
314
+ } // Save a copy of data in case submit errors and we need to revert
315
+
316
+
317
+ var preSubmitData = _objectSpread({}, data); // Re-apply the patch to the page's formData.
315
318
  // This should normally have no effect but will prevent issues
316
319
  // with validation if formData happens to have been wiped.
317
320
 
@@ -419,6 +422,8 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
419
422
  pageUpdate(sData);
420
423
  }, function (errors) {
421
424
  _handlers.default.submissionError(errors, addErrors);
425
+
426
+ setData(_objectSpread({}, preSubmitData));
422
427
  }, changedFieldName, changedFieldValue);
423
428
  }
424
429
  }
@@ -262,12 +262,14 @@ describe('components', function () {
262
262
  case 0:
263
263
  ON_SUBMIT_CALLS = [];
264
264
 
265
- ON_SUBMIT = function ON_SUBMIT(type, payload, onSuccess, onError) {
265
+ ON_SUBMIT = function ON_SUBMIT(type, payload, onSuccess, onError, fieldName, fieldValue) {
266
266
  ON_SUBMIT_CALLS.push({
267
267
  type: type,
268
268
  payload: payload,
269
269
  onSuccess: onSuccess,
270
- onError: onError
270
+ onError: onError,
271
+ fieldName: fieldName,
272
+ fieldValue: fieldValue
271
273
  });
272
274
  onSuccess();
273
275
  };
@@ -283,7 +285,8 @@ describe('components', function () {
283
285
  case 0:
284
286
  (0, _reactDom.render)( /*#__PURE__*/_react2.default.createElement(_FormRenderer.default, _extends({}, _userProfile.default, {
285
287
  data: _userProfileData.default,
286
- hooks: HOOKS
288
+ hooks: HOOKS,
289
+ type: _models.FormTypes.HUB
287
290
  })), container);
288
291
 
289
292
  case 1:
@@ -302,9 +305,12 @@ describe('components', function () {
302
305
  _hub$childNodes2 = _slicedToArray(hub.childNodes, 3), civilServantList = _hub$childNodes2[2];
303
306
  link = getChangeLink(civilServantList);
304
307
 
305
- _react.fireEvent.click(link, {}); // Should already be answered "Yes", so simply click "Continue".
308
+ _react.fireEvent.click(link, {}); // Should already be answered "Yes", but select "No, I'm a contractor" option to generate patch
309
+ // and then click "Continue".
306
310
 
307
311
 
312
+ _react.fireEvent.click(_react.screen.getByText("No, I'm a contractor"));
313
+
308
314
  page = form.childNodes[0];
309
315
  continueButton = getContinueButton(page);
310
316
  expect(ON_SUBMIT_CALLS.length).toEqual(0);
@@ -313,7 +319,9 @@ describe('components', function () {
313
319
 
314
320
  expect(ON_SUBMIT_CALLS.length).toEqual(1);
315
321
  expect(ON_SUBMIT_CALLS[0].type).toEqual(_models.PageAction.TYPES.SUBMIT);
316
- expect(ON_SUBMIT_CALLS[0].payload.areYouACivilServant).toEqual('yes'); // And we should be back on the hub.
322
+ expect(ON_SUBMIT_CALLS[0].payload.areYouACivilServant).toEqual('no-contractor');
323
+ expect(ON_SUBMIT_CALLS[0].fieldName).toEqual('Are you a civil servant?');
324
+ expect(ON_SUBMIT_CALLS[0].fieldValue).toEqual("No, I'm a contractor"); // And we should be back on the hub.
317
325
 
318
326
  expect(form.childNodes.length).toEqual(2); // Title + Hub page (= CYA)
319
327
 
@@ -321,7 +329,7 @@ describe('components', function () {
321
329
  expect(title.tagName).toEqual('H1');
322
330
  expect(title.textContent).toEqual(_userProfile.default.title);
323
331
 
324
- case 21:
332
+ case 24:
325
333
  case "end":
326
334
  return _context8.stop();
327
335
  }
@@ -41,7 +41,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
41
41
  * @returns The first encountered error with the component.
42
42
  */
43
43
  var validateComponent = function validateComponent(component, outerData, formData) {
44
- var _fd$keycloakContext, _component$data$optio;
44
+ var _component$data$optio;
45
45
 
46
46
  var fd = formData || outerData;
47
47
 
@@ -68,8 +68,7 @@ var validateComponent = function validateComponent(component, outerData, formDat
68
68
  return (0, _validateCollection.default)(component, value, fd);
69
69
 
70
70
  case _models.ComponentTypes.EMAIL:
71
- var userEmail = fd === null ? '' : (_fd$keycloakContext = fd.keycloakContext) === null || _fd$keycloakContext === void 0 ? void 0 : _fd$keycloakContext.email;
72
- error = (0, _validateEmail.default)(value, component.label, userEmail, component.custom_errors);
71
+ error = (0, _validateEmail.default)(value, component.label, component.custom_errors);
73
72
  break;
74
73
 
75
74
  case _models.ComponentTypes.DATE:
@@ -15,24 +15,18 @@ var HODS_EMAIL_REGEX = /^[a-z0-9._-]+@(digital\.)?homeoffice.gov.uk$/i;
15
15
  * validateRequired (Validate.required) for that sort of validation.
16
16
  * @param {*} value The value to validate.
17
17
  * @param {string} label The label to use in any error message.
18
- * @param {string} userEmail The user email to use for validation.
18
+ * @param {array} customErrors The array of custom errors to use for format message.
19
19
  * @returns An error if the email address is invalid.
20
20
  */
21
21
 
22
22
  var validateEmail = function validateEmail(value) {
23
23
  var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
24
- var userEmail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
25
- var customErrors = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
24
+ var customErrors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
26
25
 
27
26
  if (!!value) {
28
27
  var name = label ? label.toLowerCase() : 'email address';
29
28
 
30
29
  if (typeof value === 'string') {
31
- // The following is an interim fix pending a mechanism for validating against specific values
32
- if (['Line manager email', 'Delegate email address'].includes(label) && value.toLowerCase() === userEmail) {
33
- return "".concat(label, " cannot be your own email");
34
- }
35
-
36
30
  if (HODS_EMAIL_REGEX.test(value)) {
37
31
  return undefined;
38
32
  }
@@ -58,15 +58,15 @@ describe('utils', function () {
58
58
  'message': 'Enter email address in the correct format'
59
59
  }];
60
60
  var CUSTOM_ERROR = 'Enter email address in the correct format';
61
- expect((0, _validateEmail.default)('bob', '', '', customErrors)).toEqual(CUSTOM_ERROR);
61
+ expect((0, _validateEmail.default)('bob', '', customErrors)).toEqual(CUSTOM_ERROR);
62
62
  });
63
- it('should return an error when line manager email is same as user email', function () {
64
- var USER_EMAIL_ERROR = 'Line manager email cannot be your own email';
65
- expect((0, _validateEmail.default)('joe.bloggs@digital.homeoffice.gov.uk', 'Line manager email', 'joe.bloggs@digital.homeoffice.gov.uk')).toEqual(USER_EMAIL_ERROR);
66
- });
67
- it('should return an error when delegate email is same as user email', function () {
68
- var USER_EMAIL_ERROR = 'Delegate email address cannot be your own email';
69
- expect((0, _validateEmail.default)('joe.bloggs@digital.homeoffice.gov.uk', 'Delegate email address', 'joe.bloggs@digital.homeoffice.gov.uk')).toEqual(USER_EMAIL_ERROR);
63
+ it('should use the default format error message when custom errors is not an array', function () {
64
+ var DEFAULT_ERROR = 'Enter email address in the correct format, like jane.doe@homeoffice.gov.uk';
65
+ var customErrors = {
66
+ 'type': 'format',
67
+ 'message': 'Enter email address in the correct format'
68
+ };
69
+ expect((0, _validateEmail.default)('bob', '', customErrors)).toEqual(DEFAULT_ERROR);
70
70
  });
71
71
  });
72
72
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.46.0",
3
+ "version": "4.47.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",