@ukhomeoffice/cop-react-form-renderer 4.22.4 → 4.22.6

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.
@@ -103,7 +103,7 @@ var DEFAULT_CLASS = 'hods-form';
103
103
  exports.DEFAULT_CLASS = DEFAULT_CLASS;
104
104
 
105
105
  var InternalFormRenderer = function InternalFormRenderer(_ref2) {
106
- var _formState$page4;
106
+ var _formState$page5;
107
107
 
108
108
  var title = _ref2.title,
109
109
  type = _ref2.type,
@@ -261,7 +261,11 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
261
261
 
262
262
  if (pages) {
263
263
  pages.forEach(function (page) {
264
- page.formData = data;
264
+ if (page) {
265
+ page.formData = data;
266
+ }
267
+
268
+ ;
265
269
  });
266
270
  setCurrentTask(function (prev) {
267
271
  return _objectSpread(_objectSpread({}, prev), {}, {
@@ -413,11 +417,13 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
413
417
  }
414
418
  }; // Handle navigation from "Check your answers".
415
419
  // Passing a dataCallback allows changes to formData from the
416
- // CYA screen. This is necessary to alow changing the active
420
+ // CYA screen. This is necessary to allow changing the active
417
421
  // entry in page collections.
418
422
 
419
423
 
420
424
  var onCYARowAction = function onCYARowAction(page, dataCallback) {
425
+ var _formState$page3;
426
+
421
427
  if (typeof dataCallback === 'function') {
422
428
  var newData = dataCallback(_objectSpread({}, data));
423
429
 
@@ -426,6 +432,19 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
426
432
  }
427
433
  }
428
434
 
435
+ if (((_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.type) === _models.FormPages.PARTIAL_CYA) {
436
+ hubDetails.sections.every(function (section) {
437
+ return section.tasks.every(function (task) {
438
+ if (task.pages.includes(page.pageId) && task.name !== currentTask.name) {
439
+ onTaskAction(task);
440
+ return false;
441
+ }
442
+
443
+ return true;
444
+ });
445
+ });
446
+ }
447
+
429
448
  if (page) {
430
449
  _handlers.default.cyaAction(page, pageId, onPageChange);
431
450
  }
@@ -501,16 +520,37 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
501
520
  }
502
521
  }
503
522
 
504
- if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
523
+ if (action.type === _models.PageAction.TYPES.SUBMIT && hub === _models.HubFormats.TASK) {
524
+ setPagePoint('submit');
525
+
505
526
  if (_helpers.default.canCYASubmit(currentTask.fullPages, validate.pages)) {
527
+ // Submit.
506
528
  var _submissionData2 = _utils.default.Format.form({
507
529
  pages: pages,
508
530
  components: components
509
531
  }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
510
532
 
511
- _submissionData2.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData2, currentTask);
512
- setData(_submissionData2);
533
+ _submissionData2.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData2, currentTask, true);
534
+ setData(_submissionData2); // Now submit the data to the backend...
535
+
513
536
  hooks.onSubmit(action.type, _submissionData2, function () {
537
+ return hooks.onFormComplete();
538
+ }, function (errors) {
539
+ return _handlers.default.submissionError(errors, addErrors);
540
+ });
541
+ }
542
+ }
543
+
544
+ if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
545
+ if (_helpers.default.canCYASubmit(currentTask.fullPages, validate.pages)) {
546
+ var _submissionData3 = _utils.default.Format.form({
547
+ pages: pages,
548
+ components: components
549
+ }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
550
+
551
+ _submissionData3.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData3, currentTask);
552
+ setData(_submissionData3);
553
+ hooks.onSubmit(action.type, _submissionData3, function () {
514
554
  if (type === _models.FormTypes.TASK) {
515
555
  onPageChange(undefined);
516
556
  } else {
@@ -523,9 +563,9 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
523
563
  }
524
564
 
525
565
  if (action.type === _models.PageAction.TYPES.NAVIGATE) {
526
- var _formState$page3;
566
+ var _formState$page4;
527
567
 
528
- onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.formData));
568
+ onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page4 = formState.page) === null || _formState$page4 === void 0 ? void 0 : _formState$page4.formData));
529
569
  }
530
570
 
531
571
  if (action.type === _models.PageAction.TYPES.CANCEL) {
@@ -543,7 +583,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
543
583
  className: classes()
544
584
  }, title && !hide_title && pageId === _models.FormPages.HUB && /*#__PURE__*/_react.default.createElement(_copReactComponents.LargeHeading, null, title), formState.cya && /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, _extends({
545
585
  pages: _helpers.default.getRelevantPages(formState, pages, currentTask.fullPages)
546
- }, cya, ((_formState$page4 = formState.page) === null || _formState$page4 === void 0 ? void 0 : _formState$page4.type) === _models.FormPages.PARTIAL_CYA && {
586
+ }, cya, ((_formState$page5 = formState.page) === null || _formState$page5 === void 0 ? void 0 : _formState$page5.type) === _models.FormPages.PARTIAL_CYA && {
547
587
  actions: formState.page.actions
548
588
  }, formState.cya, {
549
589
  onAction: onCYAAction,
@@ -9,7 +9,7 @@ var _models = require("../../../models");
9
9
 
10
10
  var slicePages = function slicePages(pages, currentPageId) {
11
11
  return pages.slice(0, pages.findIndex(function (p) {
12
- return p.id === currentPageId;
12
+ return (p === null || p === void 0 ? void 0 : p.id) === currentPageId ? true : false;
13
13
  }));
14
14
  };
15
15
 
@@ -29,12 +29,12 @@ var getSubmissionStatus = function getSubmissionStatus(formType, pages, currentP
29
29
  section.tasks.forEach(function (task) {
30
30
  var lastPage = task.pages[task.pages.length - 1];
31
31
 
32
- if ((lastPage === currentPageId || lastPage.id === currentPageId) && isCompleted) {
32
+ if ((lastPage === currentPageId || lastPage.id === currentPageId) && isCompleted && (task === null || task === void 0 ? void 0 : task.type) === 'pre-task-list') {
33
33
  formStatus.tasks[task.name] = {
34
34
  complete: isCompleted,
35
35
  currentPage: currentPageId
36
36
  };
37
- } else if (task.pages.includes(currentPageId)) {
37
+ } else if (task.pages.includes(currentPageId) && (task === null || task === void 0 ? void 0 : task.type) === 'pre-task-list') {
38
38
  formStatus.tasks[task.name] = {
39
39
  complete: false,
40
40
  currentPage: currentPageId
@@ -255,7 +255,7 @@ describe('components', function () {
255
255
  })
256
256
  });
257
257
  });
258
- it("should mark the current task as complete if the current task is 'undefined' i.e. it is a pre-task-list task", function () {
258
+ it("should mark the current task as complete if the current task is a pre-task-list task", function () {
259
259
  var CURRENT_PAGE_ID = PAGES[PAGES.length - 1].id;
260
260
  var TASK_NAME = undefined;
261
261
  var CURRENT_TASK = {
@@ -265,6 +265,7 @@ describe('components', function () {
265
265
  complete: true
266
266
  };
267
267
  var TASKS = [{
268
+ type: 'pre-task-list',
268
269
  pages: PAGES
269
270
  }];
270
271
  var SECTIONS = [{
@@ -24,15 +24,15 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
24
24
  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; }
25
25
 
26
26
  var getCYARowsForContainer = function getCYARowsForContainer(page, container, formData, onAction) {
27
- if ((0, _showComponentCYA.default)(container, page.formData)) {
27
+ if ((0, _showComponentCYA.default)(container, _objectSpread(_objectSpread({}, page.formData), formData))) {
28
28
  var allComponents = (0, _elevateNestedComponents.default)(container.components, formData);
29
29
  return allComponents.filter(function (c) {
30
- return (0, _showComponentCYA.default)(c, page.formData);
30
+ return (0, _showComponentCYA.default)(c, _objectSpread(_objectSpread({}, page.formData), formData));
31
31
  }).flatMap(function (component) {
32
32
  var fd = formData ? formData[component.fieldId] : undefined;
33
33
 
34
34
  if (component.type === _models.ComponentTypes.CONTAINER) {
35
- return getCYARowsForContainer(page, component, fd, onAction);
35
+ return getCYARowsForContainer(page, component, _objectSpread(_objectSpread({}, fd), formData), onAction);
36
36
  }
37
37
 
38
38
  if (component.type === _models.ComponentTypes.COLLECTION) {
@@ -320,6 +320,44 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
320
320
  checkTitleRow(1, ROWS[2]);
321
321
  checkValueRow(1, ROWS[3], 'Charlie');
322
322
  });
323
+ it('should conditionally show an appropriate row for a single text component inside a container belonging to a partial-collection', function () {
324
+ var OUTER_ID = 'a';
325
+ var INNER_ID = 'b';
326
+ var FORM_DATA = {
327
+ collectionName: [{
328
+ container: _defineProperty({}, INNER_ID, 'Bravo'),
329
+ other_data: true
330
+ }, {
331
+ container: _defineProperty({}, INNER_ID, 'Charlie'),
332
+ other_data: false
333
+ }]
334
+ };
335
+ var PAGE = {
336
+ id: 'page',
337
+ formData: FORM_DATA,
338
+ cya_link: {},
339
+ collection: 'collectionName'
340
+ };
341
+ var CONTAINER = {
342
+ id: 'container',
343
+ fieldId: 'container',
344
+ type: _models.ComponentTypes.CONTAINER,
345
+ components: [{
346
+ id: INNER_ID,
347
+ fieldId: INNER_ID,
348
+ label: INNER_ID,
349
+ type: 'text'
350
+ }],
351
+ formData: FORM_DATA,
352
+ show_when: {
353
+ field: 'other_data',
354
+ op: '=',
355
+ value: true
356
+ }
357
+ };
358
+ expect((0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.collectionName[0], function () {}).length).toEqual(1);
359
+ expect((0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.collectionName[1], function () {})).toBeFalsy();
360
+ });
323
361
  it('should get the appropriate number for rows components with nested components inside a container', function () {
324
362
  var FORM_DATA = {
325
363
  container: {
@@ -44,7 +44,7 @@ var mergeCollectionPages = function mergeCollectionPages(pages) {
44
44
  return pages.map(function (page) {
45
45
  var _page$collection;
46
46
 
47
- if ((_page$collection = page.collection) !== null && _page$collection !== void 0 && _page$collection.name && (0, _showFormPageCYA.default)(page, page.formData)) {
47
+ if (page !== null && page !== void 0 && (_page$collection = page.collection) !== null && _page$collection !== void 0 && _page$collection.name && (0, _showFormPageCYA.default)(page, page.formData)) {
48
48
  if (!masterPages[page.collection.name]) {
49
49
  // If no master page exists for this collection.name, then
50
50
  // we kick one off using this page as a template.
@@ -72,6 +72,13 @@ var meetsCondition = function meetsCondition(condition, value) {
72
72
  return value === null || value === void 0 ? void 0 : value.toString().toLowerCase().includes(compare); // If no value is provided, the field cannot contain it, so it must fail the condition.
73
73
  }
74
74
 
75
+ case '!contains':
76
+ {
77
+ var _value$toString$toLow;
78
+
79
+ return (_value$toString$toLow = !(value !== null && value !== void 0 && value.toString().toLowerCase().includes(compare))) !== null && _value$toString$toLow !== void 0 ? _value$toString$toLow : true; // If no value is provided, the field cannot contain it, so it must meet the condition.
80
+ }
81
+
75
82
  default:
76
83
  return false;
77
84
  }
@@ -350,6 +350,95 @@ describe('utils.Condition.meetsCondition', function () {
350
350
  expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
351
351
  });
352
352
  });
353
+ describe('operator !contains', function () {
354
+ var op = '!contains'; // Should match...
355
+
356
+ it('should match a string that is not in the field array', function () {
357
+ var FIELD = ['alpha', 'bravo', 'charlie'];
358
+ var VALUE = 'delta';
359
+ var CONDITION = getCondition(op, VALUE);
360
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
361
+ });
362
+ it('should match any value if the field array is empty', function () {
363
+ var FIELD = [];
364
+ var VALUE = 'alpha';
365
+ var CONDITION = getCondition(op, VALUE);
366
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
367
+ });
368
+ it('should match any field when the value is null', function () {
369
+ var FIELD = ['alpha', 'bravo', 'charlie'];
370
+ var VALUE = null;
371
+ var CONDITION = getCondition(op, VALUE);
372
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
373
+ });
374
+ it('should match any field when the value is undefined', function () {
375
+ var FIELD = ['alpha', 'bravo', 'charlie'];
376
+ var VALUE = undefined;
377
+ var CONDITION = getCondition(op, VALUE);
378
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
379
+ });
380
+ it('should match any value when the field is an empty string', function () {
381
+ var FIELD = '';
382
+ var VALUE = 'alpha';
383
+ var CONDITION = getCondition(op, VALUE);
384
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
385
+ });
386
+ it('should match a number that is missing from the field array', function () {
387
+ var FIELD = [1, 2, 3];
388
+ var VALUE = 4;
389
+ var CONDITION = getCondition(op, VALUE);
390
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
391
+ });
392
+ it('should match a substring that is missing from the field string', function () {
393
+ var FIELD = 'alphabravocharlie';
394
+ var VALUE = 'delta';
395
+ var CONDITION = getCondition(op, VALUE);
396
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
397
+ });
398
+ it('should match any value when the field is null', function () {
399
+ var FIELD = null;
400
+ var VALUE = 'alpha';
401
+ var CONDITION = getCondition(op, VALUE);
402
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
403
+ });
404
+ it('should match any value when the field is undefined', function () {
405
+ var FIELD = undefined;
406
+ var VALUE = 'alpha';
407
+ var CONDITION = getCondition(op, VALUE);
408
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
409
+ }); // Should reject...
410
+
411
+ it('should reject a string that is in the field array', function () {
412
+ var FIELD = ['alpha', 'bravo', 'charlie'];
413
+ var VALUE = 'alpha';
414
+ var CONDITION = getCondition(op, VALUE);
415
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
416
+ });
417
+ it('should reject a sub-string that is in the field array', function () {
418
+ var FIELD = ['alpha', 'bravo', 'charlie'];
419
+ var VALUE = 'alp';
420
+ var CONDITION = getCondition(op, VALUE);
421
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
422
+ });
423
+ it('should reject a number that is in the field array', function () {
424
+ var FIELD = [1, 2, 3];
425
+ var VALUE = 1;
426
+ var CONDITION = getCondition(op, VALUE);
427
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
428
+ });
429
+ it('should reject a sub-string that is in the field string', function () {
430
+ var FIELD = 'alphabravocharlie';
431
+ var VALUE = 'alpha';
432
+ var CONDITION = getCondition(op, VALUE);
433
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
434
+ });
435
+ it('should reject a string that is in the field array regardless of case', function () {
436
+ var FIELD = ['Alpha', 'bravo', 'charlie'];
437
+ var VALUE = 'alpha';
438
+ var CONDITION = getCondition(op, VALUE);
439
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
440
+ });
441
+ });
353
442
  describe('unknown operator', function () {
354
443
  var op = 'definitely_not_a_real_operator';
355
444
  it('should reject anything regardless of the value', function () {
@@ -35,7 +35,7 @@ var validatePage = function validatePage(page) {
35
35
  if (activeIndex !== null) {
36
36
  var _page$formData$page$c;
37
37
 
38
- data = (_page$formData$page$c = page.formData[page.collection.name]) === null || _page$formData$page$c === void 0 ? void 0 : _page$formData$page$c[activeIndex];
38
+ data = _objectSpread(_objectSpread({}, page.formData), (_page$formData$page$c = page.formData[page.collection.name]) === null || _page$formData$page$c === void 0 ? void 0 : _page$formData$page$c[activeIndex]);
39
39
  }
40
40
  }
41
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.22.4",
3
+ "version": "4.22.6",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",