@ukhomeoffice/cop-react-form-renderer 6.16.2-alpha → 6.16.2-beta

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.
@@ -95,21 +95,21 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
95
95
  form.page.formData[collectionName] = collectionArray.filter((_data, idx) => idx <= index);
96
96
  }
97
97
  // Set addAnother field to yes if not reached last entry in collection
98
- if (index !== undefined && index + 1 < collectionArray.length) {
98
+ if (index !== undefined && index + 1 < form.page.formData[collectionName].length) {
99
99
  form.page.formData[addAnotherName] = 'Yes';
100
100
  }
101
101
  // Set addAnother field to no if reached last entry
102
- if (index !== undefined && index + 1 === collectionArray.length && form.page.formData[addAnotherName]) {
102
+ if (index !== undefined && index + 1 === form.page.formData[collectionName].length && form.page.formData[addAnotherName]) {
103
103
  form.page.formData[addAnotherName] = 'No';
104
104
  }
105
105
  }
106
106
  pageUpdate = () => _handlers.default.navigate(action, pageId, onPageChange, state);
107
107
  break;
108
108
  case _models.PageAction.TYPES.COLLECTION_ADD:
109
- // Only add item if the index is undefined,
109
+ // Only add entry if the index is undefined,
110
110
  // the array is empty
111
111
  // or the active ID is the last one in the collection,
112
- // otherwise move to next item.
112
+ // otherwise move to next entry.
113
113
  if (index === undefined || collectionArray.length === 0 || index + 1 === form.page.formData[collectionName].length) {
114
114
  _utils.default.CollectionPage.addEntry(action.collection, form.page.formData);
115
115
  } else {
@@ -380,6 +380,74 @@ describe('components.FormRenderer.onPageAction', () => {
380
380
  // that's called as part of the onSubmit hook's onSuccess callback.
381
381
  expect(_handlers.default.navigateCalls).toEqual(1);
382
382
  });
383
+ it("should handle the ".concat(_models.PageAction.TYPES.COLLECTION_ADD, " Page Action type where active ID is not the last entry in collection"), () => {
384
+ // If the active ID is not the last entry in the collection
385
+ // then a new entry is not added
386
+ // and the active ID moves to the next entry ID.
387
+ const FORM_STATE = {
388
+ page: {
389
+ formData: {
390
+ 'testCollection': [{
391
+ id: '1'
392
+ }, {
393
+ id: '2'
394
+ }],
395
+ testCollectionActiveId: '1'
396
+ }
397
+ }
398
+ };
399
+ const ACTION = {
400
+ type: _models.PageAction.TYPES.COLLECTION_ADD,
401
+ collection: 'testCollection'
402
+ };
403
+ const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
404
+ action: ACTION,
405
+ formState: FORM_STATE
406
+ });
407
+ _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
408
+ preActionChecks();
409
+ expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
410
+ expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
411
+ postActionChecks(CUSTOM_ARGS);
412
+
413
+ // This action type also sets up a call to handlers.navigate
414
+ // that's called as part of the onSubmit hook's onSuccess callback.
415
+ expect(_handlers.default.navigateCalls).toEqual(1);
416
+ });
417
+ it("should handle the ".concat(_models.PageAction.TYPES.COLLECTION_ADD, " Page Action type where active ID is the last entry in collection"), () => {
418
+ // If the active ID is the last entry in the collection
419
+ // then a new entry is added (although not picked up in this test)
420
+ // and the active ID becomes null.
421
+ const FORM_STATE = {
422
+ page: {
423
+ formData: {
424
+ 'testCollection': [{
425
+ id: '1'
426
+ }, {
427
+ id: '2'
428
+ }],
429
+ testCollectionActiveId: '2'
430
+ }
431
+ }
432
+ };
433
+ const ACTION = {
434
+ type: _models.PageAction.TYPES.COLLECTION_ADD,
435
+ collection: 'testCollection'
436
+ };
437
+ const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
438
+ action: ACTION,
439
+ formState: FORM_STATE
440
+ });
441
+ _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
442
+ preActionChecks();
443
+ expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual(null);
444
+ expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
445
+ postActionChecks(CUSTOM_ARGS);
446
+
447
+ // This action type also sets up a call to handlers.navigate
448
+ // that's called as part of the onSubmit hook's onSuccess callback.
449
+ expect(_handlers.default.navigateCalls).toEqual(1);
450
+ });
383
451
  it("should clean previous collection entry fields at top level of formData", () => {
384
452
  const FORM_STATE = {
385
453
  page: {
@@ -708,6 +776,116 @@ describe('components.FormRenderer.onPageAction', () => {
708
776
  expect(FORM_STATE.page.formData.parent[0].child[0].id).toEqual('12345');
709
777
  });
710
778
  });
779
+ it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to reset the active ID"), () => {
780
+ // In this test the action includes the resetActiveId field
781
+ // requiring that the active ID is pointed to the first entry in the collection
782
+ // so that user can cycle through the full collection again.
783
+ const FORM_STATE = {
784
+ page: {
785
+ formData: {
786
+ testCollection: [{
787
+ id: '1'
788
+ }, {
789
+ id: '2'
790
+ }, {
791
+ id: '3'
792
+ }],
793
+ testCollectionActiveId: '3'
794
+ }
795
+ }
796
+ };
797
+ const ACTION = {
798
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
799
+ collection: 'testCollection',
800
+ resetActiveId: true
801
+ };
802
+ const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
803
+ formState: FORM_STATE,
804
+ action: ACTION
805
+ });
806
+ _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
807
+ preActionChecks();
808
+ // All the actions does in this case is set a function
809
+ // to be called in the onSuccess callback for the onSubmit hook.
810
+ postActionChecks(CUSTOM_ARGS);
811
+ expect(_handlers.default.navigateCalls).toEqual(1);
812
+ expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('1');
813
+ });
814
+ it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to truncate the collection"), () => {
815
+ // In this test the action includes the truncateCollection field
816
+ // and if the user has selected no to more entries
817
+ // then all entries after current index (from active ID) are truncated.
818
+ const FORM_STATE = {
819
+ page: {
820
+ formData: {
821
+ tests: [{
822
+ id: '1'
823
+ }, {
824
+ id: '2'
825
+ }, {
826
+ id: '3'
827
+ }],
828
+ testsActiveId: '2',
829
+ addAnotherTest: 'No'
830
+ }
831
+ }
832
+ };
833
+ const ACTION = {
834
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
835
+ collection: 'tests',
836
+ truncateCollection: true
837
+ };
838
+ const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
839
+ formState: FORM_STATE,
840
+ action: ACTION
841
+ });
842
+ _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
843
+ preActionChecks();
844
+ // All the actions does in this case is set a function
845
+ // to be called in the onSuccess callback for the onSubmit hook.
846
+ postActionChecks(CUSTOM_ARGS);
847
+ expect(_handlers.default.navigateCalls).toEqual(1);
848
+ expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
849
+ expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
850
+ expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('No');
851
+ });
852
+ it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to set addAnother to yes"), () => {
853
+ // If the user is midway through the collection
854
+ // then the addAnother field is set to yes
855
+ // to allow the navigation to move to next entry.
856
+ const FORM_STATE = {
857
+ page: {
858
+ formData: {
859
+ tests: [{
860
+ id: '1'
861
+ }, {
862
+ id: '2'
863
+ }, {
864
+ id: '3'
865
+ }],
866
+ testsActiveId: '2',
867
+ addAnotherTest: 'No'
868
+ }
869
+ }
870
+ };
871
+ const ACTION = {
872
+ type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
873
+ collection: 'tests'
874
+ };
875
+ const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
876
+ formState: FORM_STATE,
877
+ action: ACTION
878
+ });
879
+ _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
880
+ preActionChecks();
881
+ // All the actions does in this case is set a function
882
+ // to be called in the onSuccess callback for the onSubmit hook.
883
+ postActionChecks(CUSTOM_ARGS);
884
+ expect(_handlers.default.navigateCalls).toEqual(1);
885
+ expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
886
+ expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(3);
887
+ expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('Yes');
888
+ });
711
889
  it("should work for the ".concat(_models.PageAction.TYPES.NAVIGATE, " action type"), () => {
712
890
  const FORM_STATE = {
713
891
  page: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.16.2-alpha",
3
+ "version": "6.16.2-beta",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",