@ukhomeoffice/cop-react-form-renderer 5.20.1 → 5.23.2

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 (41) hide show
  1. package/dist/components/CheckYourAnswers/Answer.test.js +1 -1
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +1 -1
  3. package/dist/components/CollectionPage/CollectionPage.js +26 -10
  4. package/dist/components/CollectionPage/CollectionPage.test.js +256 -107
  5. package/dist/components/FormComponent/Collection.js +1 -1
  6. package/dist/components/FormComponent/Collection.test.js +1 -1
  7. package/dist/components/FormComponent/Container.test.js +1 -1
  8. package/dist/components/FormComponent/FormComponent.test.js +1 -1
  9. package/dist/components/FormPage/FormPage.test.js +1 -1
  10. package/dist/components/FormRenderer/FormRenderer.test.js +1 -1
  11. package/dist/components/FormRenderer/onCYAAction.js +59 -60
  12. package/dist/components/FormRenderer/onCYAAction.test.js +31 -0
  13. package/dist/components/FormRenderer/onPageAction.js +1 -1
  14. package/dist/components/FormRenderer/onPageAction.test.js +12 -0
  15. package/dist/components/PageActions/ActionButton.test.js +1 -1
  16. package/dist/components/PageActions/PageActions.test.js +1 -1
  17. package/dist/context/HooksContext/HooksContext.test.js +1 -1
  18. package/dist/context/ValidationContext/ValidationContext.js +1 -1
  19. package/dist/context/ValidationContext/ValidationContext.test.js +1 -1
  20. package/dist/hooks/useAxios.js +1 -1
  21. package/dist/hooks/useGetRequest.js +1 -1
  22. package/dist/utils/CollectionPage/addCollectionPageEntry.js +22 -0
  23. package/dist/utils/CollectionPage/addCollectionPageEntry.test.js +16 -0
  24. package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.js +9 -4
  25. package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.test.js +30 -0
  26. package/dist/utils/CollectionPage/getCollectionPageActiveId.js +24 -0
  27. package/dist/utils/CollectionPage/getCollectionPageActiveId.test.js +25 -0
  28. package/dist/utils/CollectionPage/getCollectionPageActiveIndex.js +35 -15
  29. package/dist/utils/CollectionPage/getCollectionPageActiveIndex.test.js +34 -0
  30. package/dist/utils/CollectionPage/getCollectionPageData.js +57 -0
  31. package/dist/utils/CollectionPage/getCollectionPageData.test.js +102 -0
  32. package/dist/utils/CollectionPage/index.js +9 -1
  33. package/dist/utils/CollectionPage/setCollectionPageData.js +52 -0
  34. package/dist/utils/CollectionPage/setCollectionPageData.test.js +112 -0
  35. package/dist/utils/Component/getComponentTests/getComponent.calculation.test.js +1 -1
  36. package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +1 -1
  37. package/dist/utils/Validate/additional/mustBeEarlierDateTime.js +8 -3
  38. package/dist/utils/Validate/additional/mustBeEarlierDateTime.test.js +21 -0
  39. package/dist/utils/Validate/validatePage.js +2 -2
  40. package/dist/utils/Validate/validatePage.test.js +268 -18
  41. package/package.json +2 -2
@@ -29,7 +29,7 @@ _dayjs.default.extend(_customParseFormat.default);
29
29
  * the date/time component can't be found.
30
30
  */
31
31
  var mustBeEarlierDateTime = function mustBeEarlierDateTime(data, config, container) {
32
- var _container$components, _container$components2, _data$container$field, _data$container$field2;
32
+ var _container$components, _container$components2, _data$container$field, _data$container$field2, _data$container$field3, _data$container$field4;
33
33
  var date = (_container$components = container.components) === null || _container$components === void 0 ? void 0 : _container$components.find(function (c) {
34
34
  return c.fieldId === config.date;
35
35
  });
@@ -39,6 +39,11 @@ var mustBeEarlierDateTime = function mustBeEarlierDateTime(data, config, contain
39
39
  if (!date || !time || !data) {
40
40
  return false;
41
41
  }
42
+ var dateFromData = (_data$container$field = data[container.fieldId]) === null || _data$container$field === void 0 ? void 0 : _data$container$field[config.date];
43
+ var timeFromData = (_data$container$field2 = data[container.fieldId]) === null || _data$container$field2 === void 0 ? void 0 : _data$container$field2[config.time];
44
+ if (!dateFromData || !timeFromData) {
45
+ return true;
46
+ }
42
47
  // Here we return true if there are errors with the date & time components
43
48
  // individually. This is to allow them to process their errors, and to prevent
44
49
  // validation from stopping at the container level.
@@ -48,8 +53,8 @@ var mustBeEarlierDateTime = function mustBeEarlierDateTime(data, config, contain
48
53
  if ((0, _validateComponent.default)(time, data[container.fieldId], data[container.fieldId])) {
49
54
  return true;
50
55
  }
51
- var dateStr = (0, _utils.formatString)((_data$container$field = data[container.fieldId]) === null || _data$container$field === void 0 ? void 0 : _data$container$field[config.date]);
52
- var timeStr = (0, _utils.formattedTime)((_data$container$field2 = data[container.fieldId]) === null || _data$container$field2 === void 0 ? void 0 : _data$container$field2[config.time]);
56
+ var dateStr = (0, _utils.formatString)((_data$container$field3 = data[container.fieldId]) === null || _data$container$field3 === void 0 ? void 0 : _data$container$field3[config.date]);
57
+ var timeStr = (0, _utils.formattedTime)((_data$container$field4 = data[container.fieldId]) === null || _data$container$field4 === void 0 ? void 0 : _data$container$field4[config.time]);
53
58
  return (0, _dayjs.default)("".concat(dateStr, " ").concat(timeStr), "".concat(_utils.DATE_FORMAT, " HH:mm")).isBefore((0, _dayjs.default)());
54
59
  };
55
60
  var _default = mustBeEarlierDateTime;
@@ -70,6 +70,27 @@ describe('utils.Validate.additional.mustBeEarlierDateTime', function () {
70
70
  var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, CONTAINER);
71
71
  expect(result).toEqual(true);
72
72
  });
73
+ test('should return true if time value is missing and optional', function () {
74
+ var EDITED_DATA = {
75
+ testContainer: {
76
+ testDate: '01-01-2000'
77
+ }
78
+ };
79
+ var EDITED_CONTAINER = {
80
+ fieldId: 'testContainer',
81
+ type: 'container',
82
+ components: [{
83
+ fieldId: 'testDate',
84
+ type: 'date',
85
+ required: true
86
+ }, {
87
+ fieldId: 'testTime',
88
+ type: 'time'
89
+ }]
90
+ };
91
+ var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, EDITED_CONTAINER);
92
+ expect(result).toEqual(true);
93
+ });
73
94
  test('should return true if date has errors', function () {
74
95
  var EDITED_DATA = {
75
96
  testContainer: {
@@ -26,8 +26,8 @@ var validatePage = function validatePage(page) {
26
26
  if (page.collection) {
27
27
  var activeIndex = _CollectionPage.default.getActiveIndex(page.collection.name, page.formData);
28
28
  if (activeIndex !== null) {
29
- var _page$formData$page$c;
30
- 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]);
29
+ var _CollectionPage$getDa;
30
+ data = _objectSpread(_objectSpread({}, page.formData), (_CollectionPage$getDa = _CollectionPage.default.getData(page.collection.name, page.formData)) === null || _CollectionPage$getDa === void 0 ? void 0 : _CollectionPage$getDa[activeIndex]);
31
31
  }
32
32
  }
33
33
  if ((0, _showFormPage.default)(page, data) && Array.isArray(page.components)) {
@@ -100,6 +100,33 @@ describe('utils.Validate.Page', function () {
100
100
  error: 'Echo is required'
101
101
  });
102
102
  });
103
+ it('should return an error for each required component on a nested collection page', function () {
104
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
105
+ // The only unrequired one
106
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
107
+ var PAGE = {
108
+ components: COMPONENTS,
109
+ collection: {
110
+ name: 'parent.child'
111
+ },
112
+ formData: null
113
+ };
114
+ var RESULT = (0, _validatePage.default)(PAGE);
115
+ expect(RESULT.length).toEqual(4);
116
+ expect(RESULT).toEqual([{
117
+ id: 'a',
118
+ error: 'Alpha is required'
119
+ }, {
120
+ id: 'b',
121
+ error: 'Bravo is required'
122
+ }, {
123
+ id: 'd',
124
+ error: 'Delta is required'
125
+ }, {
126
+ id: 'e',
127
+ error: 'Echo is required'
128
+ }]);
129
+ });
103
130
  it('should return an error for each required component with interpolated label', function () {
104
131
  var COMPONENTS = [
105
132
  // eslint-disable-next-line no-template-curly-in-string
@@ -124,26 +151,22 @@ describe('utils.Validate.Page', function () {
124
151
  };
125
152
  var RESULT = (0, _validatePage.default)(PAGE);
126
153
  expect(RESULT.length).toEqual(5);
127
- expect(RESULT[0]).toEqual({
154
+ expect(RESULT).toEqual([{
128
155
  id: 'a',
129
156
  error: "Alpha ".concat(PAGE.formData.tiger, " is required")
130
- });
131
- expect(RESULT[1]).toEqual({
157
+ }, {
132
158
  id: 'b',
133
159
  error: "Bravo ".concat(PAGE.formData.panther, " is required")
134
- });
135
- expect(RESULT[2]).toEqual({
160
+ }, {
136
161
  id: 'c',
137
162
  error: "Charlie ".concat(PAGE.formData.eagle, " is required")
138
- });
139
- expect(RESULT[3]).toEqual({
163
+ }, {
140
164
  id: 'd',
141
165
  error: "Delta ".concat(PAGE.formData.lion, " is required")
142
- });
143
- expect(RESULT[4]).toEqual({
166
+ }, {
144
167
  id: 'e',
145
168
  error: "Echo ".concat(PAGE.formData.zoo, " is required")
146
- });
169
+ }]);
147
170
  });
148
171
  });
149
172
  describe('when the form data is fully valid', function () {
@@ -337,22 +360,19 @@ describe('utils.Validate.Page', function () {
337
360
  };
338
361
  var RESULT = (0, _validatePage.default)(PAGE);
339
362
  expect(RESULT.length).toEqual(4);
340
- expect(RESULT[0]).toEqual({
363
+ expect(RESULT).toEqual([{
341
364
  id: 'a',
342
365
  error: 'Alpha is required'
343
- });
344
- expect(RESULT[1]).toEqual({
366
+ }, {
345
367
  id: 'b',
346
368
  error: 'Bravo is required'
347
- });
348
- expect(RESULT[2]).toEqual({
369
+ }, {
349
370
  id: 'd',
350
371
  error: 'Delta is required'
351
- });
352
- expect(RESULT[3]).toEqual({
372
+ }, {
353
373
  id: 'e',
354
374
  error: 'Echo is required'
355
- });
375
+ }]);
356
376
  });
357
377
  it('should return no errors on a collection page with a show_when that is false', function () {
358
378
  var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
@@ -469,4 +489,234 @@ describe('utils.Validate.Page', function () {
469
489
  });
470
490
  });
471
491
  });
492
+ describe('with a nested CollectionPage', function () {
493
+ var COLLECTION = {
494
+ name: 'parent.child'
495
+ };
496
+ describe('when the form data is fully valid', function () {
497
+ var DATA = {
498
+ parentActiveId: '01',
499
+ childActiveId: '01',
500
+ parent: [{
501
+ id: '01',
502
+ child: [{
503
+ id: '01',
504
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
505
+ bravo: 'bravo.jones@digital.homeoffice.gov.uk'
506
+ }]
507
+ }]
508
+ };
509
+ it('should return no errors when none of the components are required or email types', function () {
510
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false)];
511
+ var PAGE = {
512
+ collection: COLLECTION,
513
+ components: COMPONENTS,
514
+ formData: DATA
515
+ };
516
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
517
+ });
518
+ it('should return no errors when all of the components are required but not email types', function () {
519
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true)];
520
+ var PAGE = {
521
+ collection: COLLECTION,
522
+ components: COMPONENTS,
523
+ formData: DATA
524
+ };
525
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
526
+ });
527
+ it('should return no errors when none of the components are required but are all email types', function () {
528
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false)];
529
+ var PAGE = {
530
+ collection: COLLECTION,
531
+ components: COMPONENTS,
532
+ formData: DATA
533
+ };
534
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
535
+ });
536
+ it('should return no errors when all of the components are required and email types', function () {
537
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true)];
538
+ var PAGE = {
539
+ collection: COLLECTION,
540
+ components: COMPONENTS,
541
+ formData: DATA
542
+ };
543
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
544
+ });
545
+ it('should return an error for each required component on a collection page with a show_when that is true', function () {
546
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
547
+ // The only unrequired one
548
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
549
+ var PAGE = {
550
+ components: COMPONENTS,
551
+ collection: {
552
+ name: 'parent.child'
553
+ },
554
+ show_when: {
555
+ field: 'isShown',
556
+ op: '=',
557
+ value: true
558
+ },
559
+ formData: {
560
+ parentActiveId: '01',
561
+ childActiveId: '01',
562
+ parent: [{
563
+ id: '01',
564
+ child: [{
565
+ id: '01',
566
+ isShown: true
567
+ }]
568
+ }]
569
+ }
570
+ };
571
+ var RESULT = (0, _validatePage.default)(PAGE);
572
+ expect(RESULT.length).toEqual(4);
573
+ expect(RESULT).toEqual([{
574
+ id: 'a',
575
+ error: 'Alpha is required'
576
+ }, {
577
+ id: 'b',
578
+ error: 'Bravo is required'
579
+ }, {
580
+ id: 'd',
581
+ error: 'Delta is required'
582
+ }, {
583
+ id: 'e',
584
+ error: 'Echo is required'
585
+ }]);
586
+ });
587
+ it('should return no errors on a collection page with a show_when that is false', function () {
588
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
589
+ // The only unrequired one
590
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
591
+ var PAGE = {
592
+ components: COMPONENTS,
593
+ collection: {
594
+ name: 'parent.child'
595
+ },
596
+ show_when: {
597
+ field: 'isShown',
598
+ op: '=',
599
+ value: true
600
+ },
601
+ formData: {
602
+ parentActiveId: '01',
603
+ childActiveId: '01',
604
+ parent: [{
605
+ id: '01',
606
+ child: [{
607
+ id: '01',
608
+ isShown: false
609
+ }]
610
+ }]
611
+ }
612
+ };
613
+ var RESULT = (0, _validatePage.default)(PAGE);
614
+ expect(RESULT.length).toEqual(0);
615
+ });
616
+ });
617
+ describe('when the form data has one field missing and includes an invalid email', function () {
618
+ var DATA = {
619
+ parentActiveId: '01',
620
+ childActiveId: '01',
621
+ parent: [{
622
+ id: '01',
623
+ child: [{
624
+ id: '01',
625
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
626
+ bravo: 'bravo.jones@hotmail.com'
627
+ }]
628
+ }]
629
+ };
630
+ it('should return no errors when none of the components are required or email types', function () {
631
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', false)];
632
+ var PAGE = {
633
+ collection: COLLECTION,
634
+ components: COMPONENTS,
635
+ formData: null
636
+ };
637
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
638
+ });
639
+ it('should return an error for the missing field when all are required but not email types', function () {
640
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', true)];
641
+ var PAGE = {
642
+ collection: COLLECTION,
643
+ components: COMPONENTS,
644
+ formData: DATA
645
+ };
646
+ var RESULT = (0, _validatePage.default)(PAGE);
647
+ expect(RESULT.length).toEqual(1);
648
+ expect(RESULT[0]).toEqual({
649
+ id: 'charlie',
650
+ error: 'Charlie is required'
651
+ });
652
+ });
653
+ it('should return an error for the invalid email field when none are required but all email types', function () {
654
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', false)];
655
+ var PAGE = {
656
+ collection: COLLECTION,
657
+ components: COMPONENTS,
658
+ formData: DATA
659
+ };
660
+ var RESULT = (0, _validatePage.default)(PAGE);
661
+ expect(RESULT.length).toEqual(1);
662
+ expect(RESULT[0]).toEqual({
663
+ id: 'bravo',
664
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
665
+ });
666
+ });
667
+ it('should return an error for both invalid fields when all are required and email types', function () {
668
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', true)];
669
+ var PAGE = {
670
+ collection: COLLECTION,
671
+ components: COMPONENTS,
672
+ formData: DATA
673
+ };
674
+ var RESULT = (0, _validatePage.default)(PAGE);
675
+ expect(RESULT.length).toEqual(2);
676
+ expect(RESULT[0]).toEqual({
677
+ id: 'bravo',
678
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
679
+ });
680
+ expect(RESULT[1]).toEqual({
681
+ id: 'charlie',
682
+ error: 'Charlie is required'
683
+ });
684
+ });
685
+ it('should return an interpolated error for both invalid fields when all are required and email types', function () {
686
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true),
687
+ // eslint-disable-next-line no-template-curly-in-string
688
+ setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo ${lion}', true),
689
+ // eslint-disable-next-line no-template-curly-in-string
690
+ setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie ${panther}', true)];
691
+ var PAGE = {
692
+ collection: COLLECTION,
693
+ components: COMPONENTS,
694
+ formData: {
695
+ parentActiveId: '01',
696
+ childActiveId: '01',
697
+ parent: [{
698
+ id: '01',
699
+ child: [{
700
+ id: '01',
701
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
702
+ bravo: 'bravo.jones@hotmail.com',
703
+ lion: 'Lion',
704
+ panther: 'Panther'
705
+ }]
706
+ }]
707
+ }
708
+ };
709
+ var RESULT = (0, _validatePage.default)(PAGE);
710
+ expect(RESULT.length).toEqual(2);
711
+ expect(RESULT[0]).toEqual({
712
+ id: 'bravo',
713
+ error: "Enter bravo Lion in the correct format, like jane.doe@homeoffice.gov.uk"
714
+ });
715
+ expect(RESULT[1]).toEqual({
716
+ id: 'charlie',
717
+ error: "Charlie Panther is required"
718
+ });
719
+ });
720
+ });
721
+ });
472
722
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.20.1",
3
+ "version": "5.23.2",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -27,7 +27,7 @@
27
27
  "@babel/eslint-parser": "^7.19.1",
28
28
  "@babel/preset-env": "^7.15.6",
29
29
  "@babel/preset-react": "^7.14.5",
30
- "@monaco-editor/react": "^4.3.1",
30
+ "@monaco-editor/react": "4.4.6",
31
31
  "@storybook/addon-a11y": "^6.3.8",
32
32
  "@storybook/addon-actions": "^6.3.8",
33
33
  "@storybook/addon-docs": "^6.3.8",