@ukhomeoffice/cop-react-form-renderer 5.0.0 → 5.1.0-alpha

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.
@@ -111,7 +111,12 @@ var FormComponent = function FormComponent(_ref) {
111
111
  if (component.type === _models.ComponentTypes.FILE) {
112
112
  changeMetaDocuments([target.value]);
113
113
  } else if (component.type === _models.ComponentTypes.MULTI_FILE) {
114
- changeMetaDocuments(target.value);
114
+ var docsWithIndices = target.value.map(function (doc, index) {
115
+ return _objectSpread(_objectSpread({}, doc), {}, {
116
+ indexInField: index
117
+ });
118
+ });
119
+ changeMetaDocuments(docsWithIndices);
115
120
  }
116
121
  }
117
122
  };
@@ -22,7 +22,7 @@ var onTaskAction = function onTaskAction(currentTask, pages, setCurrentTask, hub
22
22
  });
23
23
  setCurrentTask(currentTask);
24
24
  var state = _objectSpread({}, task);
25
- if (task.state === _models.TaskStates.TYPES.COMPLETE) {
25
+ if (task.state === _models.TaskStates.TYPES.COMPLETE && !task.alwaysShowFirstPage) {
26
26
  if (hubDetails !== null && hubDetails !== void 0 && hubDetails.noTaskCYAs) {
27
27
  // If the task is complete and there are no CYA's then show user first page
28
28
  /* eslint-disable prefer-destructuring */
@@ -33,12 +33,12 @@ var onTaskAction = function onTaskAction(currentTask, pages, setCurrentTask, hub
33
33
  } else {
34
34
  onPageChange(_models.FormPages.CYA, state);
35
35
  }
36
- } else if (task.state === _models.TaskStates.TYPES.IN_PROGRESS) {
36
+ } else if (task.state === _models.TaskStates.TYPES.IN_PROGRESS && !task.alwaysShowFirstPage) {
37
37
  var _currentPage = data.formStatus.tasks[currentTask.name].currentPage;
38
38
  /* eslint-enable prefer-destructuring */
39
39
  onPageChange(_currentPage || currentTask.pages[0], state);
40
40
  } else if (task.firstPage) {
41
- onPageChange(currentTask.firstPage, state);
41
+ onPageChange(task.firstPage, state);
42
42
  } else {
43
43
  onPageChange(task.pages[0], state);
44
44
  }
@@ -168,6 +168,40 @@ describe('components.FormRenderer.onTaskAction', function () {
168
168
  expect(onPageChangeArgs[0].page).toEqual(CUSTOM_ARGS.currentTask.pages[0]);
169
169
  expect(onPageChangeArgs[0].state).toEqual(CUSTOM_ARGS.currentTask);
170
170
  });
171
+ it('should navigate to firstPage if alwaysShowFirstPage is set if state is in progress', function () {
172
+ var TASK = {
173
+ name: 'taskalwaysShowFirstPageInProgress',
174
+ state: _models.TaskStates.TYPES.IN_PROGRESS,
175
+ firstPage: 'page2',
176
+ alwaysShowFirstPage: true,
177
+ pages: ['page1', 'page2', 'page3']
178
+ };
179
+ var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
180
+ currentTask: TASK
181
+ });
182
+ _onTaskAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
183
+ genericChecks(CUSTOM_ARGS.currentTask);
184
+ expect(onPageChangeArgs.length).toEqual(1);
185
+ expect(onPageChangeArgs[0].page).toEqual(CUSTOM_ARGS.currentTask.pages[1]);
186
+ expect(onPageChangeArgs[0].state).toEqual(CUSTOM_ARGS.currentTask);
187
+ });
188
+ it('should navigate to firstPage if alwaysShowFirstPage is set if state is in progress', function () {
189
+ var TASK = {
190
+ name: 'taskalwaysShowFirstPageComplete',
191
+ state: _models.TaskStates.TYPES.COMPLETE,
192
+ firstPage: 'page2',
193
+ alwaysShowFirstPage: true,
194
+ pages: ['page1', 'page2', 'page3']
195
+ };
196
+ var CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
197
+ currentTask: TASK
198
+ });
199
+ _onTaskAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
200
+ genericChecks(CUSTOM_ARGS.currentTask);
201
+ expect(onPageChangeArgs.length).toEqual(1);
202
+ expect(onPageChangeArgs[0].page).toEqual(CUSTOM_ARGS.currentTask.pages[1]);
203
+ expect(onPageChangeArgs[0].state).toEqual(CUSTOM_ARGS.currentTask);
204
+ });
171
205
  });
172
206
  var otherStates = Object.values(_models.TaskStates.TYPES).filter(function (t) {
173
207
  return t !== _models.TaskStates.TYPES.IN_PROGRESS && t !== _models.TaskStates.TYPES.COMPLETE;
@@ -136,7 +136,9 @@ TaskList.propTypes = {
136
136
  name: _propTypes.default.string.isRequired,
137
137
  pages: _propTypes.default.arrayOf(_propTypes.default.string).isRequired,
138
138
  state: _propTypes.default.string,
139
- displayName: _propTypes.default.string
139
+ displayName: _propTypes.default.string,
140
+ firstPage: _propTypes.default.string,
141
+ alwaysShowFirstPage: _propTypes.default.bool
140
142
  })).isRequired
141
143
  })).isRequired
142
144
  };
@@ -148,10 +150,10 @@ TaskList.defaultProps = {
148
150
  formData: {},
149
151
  id: '',
150
152
  incompleteTitle: DEFAULT_INCOMPLETE_TITLE,
151
- onTaskAction: undefined,
152
153
  notes: {},
153
- refTitle: undefined,
154
- refNumber: undefined
154
+ onTaskAction: undefined,
155
+ refNumber: undefined,
156
+ refTitle: undefined
155
157
  };
156
158
  var _default = TaskList;
157
159
  exports.default = _default;
@@ -97,6 +97,92 @@ describe('utils.Component.get', function () {
97
97
  expect(ON_CHANGE_CALLS.length).toEqual(2); // No change, so no new event should have fired.
98
98
  });
99
99
 
100
+ it('should work correctly with the returnFullData property on the radios component', function () {
101
+ var ID = 'test-id';
102
+ var FIELD_ID = 'field-id';
103
+ var LABEL = 'label';
104
+ var OPTIONS = [{
105
+ value: 'a',
106
+ label: 'Alpha'
107
+ }, {
108
+ value: 'b',
109
+ label: 'Bravo'
110
+ }];
111
+ var ON_CHANGE_CALLS = [];
112
+ var ON_CHANGE = function ON_CHANGE(e) {
113
+ ON_CHANGE_CALLS.push(e.target);
114
+ };
115
+ var COMPONENT = {
116
+ type: _models.ComponentTypes.RADIOS,
117
+ id: ID,
118
+ fieldId: FIELD_ID,
119
+ label: LABEL,
120
+ data: {
121
+ options: OPTIONS
122
+ },
123
+ returnFullData: true,
124
+ onChange: ON_CHANGE,
125
+ 'data-testid': ID
126
+ };
127
+ var _render2 = (0, _react.render)((0, _getComponent.default)(COMPONENT)),
128
+ container = _render2.container;
129
+ var _getAllByTestId3 = (0, _react.getAllByTestId)(container, ID),
130
+ _getAllByTestId4 = _slicedToArray(_getAllByTestId3, 2),
131
+ formGroup = _getAllByTestId4[0],
132
+ radios = _getAllByTestId4[1];
133
+ expect(formGroup.tagName).toEqual('DIV');
134
+ expect(formGroup.classList).toContain('govuk-form-group');
135
+ var label;
136
+ formGroup.childNodes.forEach(function (node) {
137
+ // Check if it's an element.
138
+ if (node instanceof Element) {
139
+ if (node.tagName === 'LABEL') {
140
+ label = node;
141
+ }
142
+ }
143
+ });
144
+ expect(label).toBeDefined();
145
+ expect(label.innerHTML).toContain(LABEL);
146
+ expect(label.getAttribute('for')).toEqual(ID);
147
+ expect(radios.tagName).toEqual('DIV');
148
+ expect(radios.classList).toContain('govuk-radios');
149
+ expect(radios.childNodes.length).toEqual(OPTIONS.length);
150
+ var radioItems = [];
151
+ OPTIONS.forEach(function (_, index) {
152
+ var radio = radios.childNodes[index];
153
+ expect(radio instanceof Element).toBeTruthy();
154
+ if (radio instanceof Element) {
155
+ radioItems.push(radio);
156
+ }
157
+ });
158
+ expect(radioItems.length).toEqual(OPTIONS.length);
159
+ OPTIONS.forEach(function (option, index) {
160
+ var radio = radioItems[index];
161
+ expect(radio.tagName).toEqual('DIV');
162
+ expect(radio.classList).toContain('govuk-radios__item');
163
+ var _radio$childNodes2 = _slicedToArray(radio.childNodes, 2),
164
+ input = _radio$childNodes2[0],
165
+ inputLabel = _radio$childNodes2[1];
166
+ expect(input.tagName).toEqual('INPUT');
167
+ expect(input.type).toEqual('radio');
168
+ expect(inputLabel.textContent).toEqual(option.label);
169
+ });
170
+ _react.fireEvent.click(radioItems[0].childNodes[0]); // alpha
171
+ expect(ON_CHANGE_CALLS.length).toEqual(1);
172
+ expect(ON_CHANGE_CALLS[0]).toMatchObject({
173
+ name: FIELD_ID,
174
+ value: OPTIONS[0]
175
+ });
176
+ _react.fireEvent.click(radioItems[1].childNodes[0]); // bravo
177
+ expect(ON_CHANGE_CALLS.length).toEqual(2);
178
+ expect(ON_CHANGE_CALLS[1]).toMatchObject({
179
+ name: FIELD_ID,
180
+ value: OPTIONS[1]
181
+ });
182
+ _react.fireEvent.click(radioItems[1].childNodes[0]); // bravo (already selected, above)
183
+ expect(ON_CHANGE_CALLS.length).toEqual(2); // No change, so no new event should have fired.
184
+ });
185
+
100
186
  it('should return an appropriately rendered radios component with nested components', function () {
101
187
  var ID = 'test-id';
102
188
  var FIELD_ID = 'field-id';
@@ -132,10 +218,10 @@ describe('utils.Component.get', function () {
132
218
  };
133
219
  var _renderWithValidation = (0, _setupTests.renderWithValidation)((0, _getComponent.default)(COMPONENT)),
134
220
  container = _renderWithValidation.container;
135
- var _getAllByTestId3 = (0, _react.getAllByTestId)(container, ID),
136
- _getAllByTestId4 = _slicedToArray(_getAllByTestId3, 2),
137
- formGroup = _getAllByTestId4[0],
138
- radios = _getAllByTestId4[1];
221
+ var _getAllByTestId5 = (0, _react.getAllByTestId)(container, ID),
222
+ _getAllByTestId6 = _slicedToArray(_getAllByTestId5, 2),
223
+ formGroup = _getAllByTestId6[0],
224
+ radios = _getAllByTestId6[1];
139
225
  expect(formGroup.tagName).toEqual('DIV');
140
226
  expect(formGroup.classList).toContain('govuk-form-group');
141
227
  expect(radios.tagName).toEqual('DIV');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.0.0",
3
+ "version": "5.1.0-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,7 +16,7 @@
16
16
  "post-compile": "rimraf dist/*.test.* dist/**/*.test.* dist/**/*.stories.* dist/docs dist/assets"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "^3.0.0",
19
+ "@ukhomeoffice/cop-react-components": "3.1.0-alpha",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",