@ukhomeoffice/cop-react-form-renderer 2.9.1 → 2.10.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.
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +8 -12
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +10 -8
- package/dist/components/FormComponent/Collection.js +244 -0
- package/dist/components/FormComponent/Collection.scss +23 -0
- package/dist/components/FormComponent/Container.js +110 -0
- package/dist/components/FormComponent/Container.test.js +26 -26
- package/dist/components/FormComponent/FormComponent.js +64 -128
- package/dist/components/FormComponent/FormComponent.test.js +19 -19
- package/dist/components/FormComponent/helpers/getComponentError.js +31 -0
- package/dist/components/FormComponent/helpers/getComponentError.test.js +52 -0
- package/dist/components/FormComponent/helpers/index.js +15 -0
- package/dist/components/FormPage/FormPage.js +7 -11
- package/dist/components/FormPage/FormPage.test.js +12 -76
- package/dist/components/FormRenderer/FormRenderer.js +103 -44
- package/dist/components/FormRenderer/helpers/canActionProceed.js +3 -12
- package/dist/components/FormRenderer/helpers/canActionProceed.test.js +41 -65
- package/dist/components/FormRenderer/helpers/canCYASubmit.js +3 -24
- package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +72 -145
- package/dist/components/SummaryList/GroupAction.js +1 -1
- package/dist/components/SummaryList/SummaryList.js +33 -18
- package/dist/components/SummaryList/SummaryList.scss +12 -8
- package/dist/components/SummaryList/SummaryList.test.js +94 -27
- package/dist/components/SummaryList/SummaryListRow.js +51 -0
- package/dist/components/SummaryList/SummaryListTitleRow.js +30 -0
- package/dist/context/ValidationContext/ValidationContext.js +128 -0
- package/dist/context/ValidationContext/ValidationContext.test.js +98 -0
- package/dist/context/ValidationContext/index.js +23 -0
- package/dist/context/index.js +15 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useValidation.js +19 -0
- package/dist/index.js +11 -5
- package/dist/models/CollectionLabels.js +14 -0
- package/dist/models/index.js +8 -0
- package/dist/setupTests.js +32 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +2 -0
- package/dist/utils/CheckYourAnswers/getCYARow.test.js +6 -10
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +89 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +194 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +9 -13
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -4
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +58 -14
- package/dist/utils/Component/getDefaultValue.js +25 -0
- package/dist/utils/Component/getDefaultValue.test.js +53 -0
- package/dist/utils/Component/index.js +4 -1
- package/dist/utils/Data/getSourceData.js +38 -0
- package/dist/utils/Data/getSourceData.test.js +16 -0
- package/package.json +2 -2
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../models");
|
|
4
|
+
|
|
5
|
+
var _setupTests = require("../../setupTests");
|
|
6
|
+
|
|
7
|
+
var _getCYARowsForCollection = _interopRequireDefault(require("./getCYARowsForCollection"));
|
|
8
|
+
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
|
|
11
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
12
|
+
|
|
13
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
14
|
+
|
|
15
|
+
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; }
|
|
16
|
+
|
|
17
|
+
describe('utils.CheckYourAnswers.getCYARowsForCollection', function () {
|
|
18
|
+
it('should get no rows when there are no items', function () {
|
|
19
|
+
var FORM_DATA = undefined;
|
|
20
|
+
var PAGE = {
|
|
21
|
+
id: 'page',
|
|
22
|
+
formData: FORM_DATA,
|
|
23
|
+
cya_link: {}
|
|
24
|
+
};
|
|
25
|
+
var COMPONENT = {
|
|
26
|
+
type: 'text',
|
|
27
|
+
readonly: true,
|
|
28
|
+
id: 'a',
|
|
29
|
+
fieldId: 'a',
|
|
30
|
+
label: 'Alpha'
|
|
31
|
+
};
|
|
32
|
+
var COLLECTION = {
|
|
33
|
+
id: 'collection',
|
|
34
|
+
fieldId: 'collection',
|
|
35
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
36
|
+
item: [COMPONENT]
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
40
|
+
|
|
41
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, undefined, ON_ACTION);
|
|
42
|
+
expect(ROWS.length).toEqual(0);
|
|
43
|
+
});
|
|
44
|
+
it('should get an appropriate row for a collection with a single readonly text component', function () {
|
|
45
|
+
var FORM_DATA = {
|
|
46
|
+
collection: [{
|
|
47
|
+
a: 'Bravo'
|
|
48
|
+
}]
|
|
49
|
+
};
|
|
50
|
+
var PAGE = {
|
|
51
|
+
id: 'page',
|
|
52
|
+
formData: FORM_DATA,
|
|
53
|
+
cya_link: {}
|
|
54
|
+
};
|
|
55
|
+
var COMPONENT = {
|
|
56
|
+
type: 'text',
|
|
57
|
+
readonly: true,
|
|
58
|
+
id: 'a',
|
|
59
|
+
fieldId: 'a',
|
|
60
|
+
label: 'Alpha'
|
|
61
|
+
};
|
|
62
|
+
var COLLECTION = {
|
|
63
|
+
id: 'collection',
|
|
64
|
+
fieldId: 'collection',
|
|
65
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
66
|
+
countOffset: 22,
|
|
67
|
+
item: [COMPONENT],
|
|
68
|
+
value: FORM_DATA.collection,
|
|
69
|
+
formData: FORM_DATA
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
73
|
+
|
|
74
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, FORM_DATA.collection, ON_ACTION);
|
|
75
|
+
expect(ROWS.length).toEqual(2); // Item title row + component row
|
|
76
|
+
|
|
77
|
+
(0, _setupTests.expectObjectLike)(ROWS[0], {
|
|
78
|
+
pageId: PAGE.id,
|
|
79
|
+
fieldId: COLLECTION.fieldId,
|
|
80
|
+
full_path: "".concat(COLLECTION.fieldId, "[0]"),
|
|
81
|
+
key: 'Item 23',
|
|
82
|
+
// includes countOffset
|
|
83
|
+
type: 'title',
|
|
84
|
+
action: null
|
|
85
|
+
});
|
|
86
|
+
(0, _setupTests.expectObjectLike)(ROWS[1], {
|
|
87
|
+
pageId: PAGE.id,
|
|
88
|
+
fieldId: COMPONENT.fieldId,
|
|
89
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
90
|
+
key: COMPONENT.label,
|
|
91
|
+
action: null,
|
|
92
|
+
component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
|
|
93
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId)
|
|
94
|
+
}),
|
|
95
|
+
value: 'Bravo'
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
it('should get an appropriate row for a collection with a single readonly text component where the item label is an empty string', function () {
|
|
99
|
+
var FORM_DATA = {
|
|
100
|
+
collection: [{
|
|
101
|
+
a: 'Bravo'
|
|
102
|
+
}]
|
|
103
|
+
};
|
|
104
|
+
var PAGE = {
|
|
105
|
+
id: 'page',
|
|
106
|
+
formData: FORM_DATA,
|
|
107
|
+
cya_link: {}
|
|
108
|
+
};
|
|
109
|
+
var COMPONENT = {
|
|
110
|
+
type: 'text',
|
|
111
|
+
readonly: true,
|
|
112
|
+
id: 'a',
|
|
113
|
+
fieldId: 'a',
|
|
114
|
+
label: 'Alpha'
|
|
115
|
+
};
|
|
116
|
+
var COLLECTION = {
|
|
117
|
+
id: 'collection',
|
|
118
|
+
fieldId: 'collection',
|
|
119
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
120
|
+
labels: {
|
|
121
|
+
item: ''
|
|
122
|
+
},
|
|
123
|
+
countOffset: 22,
|
|
124
|
+
item: [COMPONENT],
|
|
125
|
+
value: FORM_DATA.collection,
|
|
126
|
+
formData: FORM_DATA
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
130
|
+
|
|
131
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, FORM_DATA.collection, ON_ACTION);
|
|
132
|
+
expect(ROWS.length).toEqual(1); // Just the component row, no title row
|
|
133
|
+
|
|
134
|
+
(0, _setupTests.expectObjectLike)(ROWS[0], {
|
|
135
|
+
pageId: PAGE.id,
|
|
136
|
+
fieldId: COMPONENT.fieldId,
|
|
137
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
138
|
+
key: COMPONENT.label,
|
|
139
|
+
action: null,
|
|
140
|
+
component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
|
|
141
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId)
|
|
142
|
+
}),
|
|
143
|
+
value: 'Bravo'
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
it('should interpolate a field label appropriately', function () {
|
|
147
|
+
var FORM_DATA = {
|
|
148
|
+
collection: [{
|
|
149
|
+
a: 'Bravo'
|
|
150
|
+
}]
|
|
151
|
+
};
|
|
152
|
+
var PAGE = {
|
|
153
|
+
id: 'page',
|
|
154
|
+
formData: FORM_DATA,
|
|
155
|
+
cya_link: {}
|
|
156
|
+
}; // eslint-disable-next-line no-template-curly-in-string
|
|
157
|
+
|
|
158
|
+
var COMPONENT = {
|
|
159
|
+
type: 'text',
|
|
160
|
+
readonly: true,
|
|
161
|
+
id: 'a',
|
|
162
|
+
fieldId: 'a',
|
|
163
|
+
label: 'Alpha ${index}'
|
|
164
|
+
};
|
|
165
|
+
var COLLECTION = {
|
|
166
|
+
id: 'collection',
|
|
167
|
+
fieldId: 'collection',
|
|
168
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
169
|
+
item: [COMPONENT],
|
|
170
|
+
value: FORM_DATA.collection,
|
|
171
|
+
formData: FORM_DATA
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
175
|
+
|
|
176
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, FORM_DATA.collection, ON_ACTION);
|
|
177
|
+
expect(ROWS.length).toEqual(2); // Title and item row.
|
|
178
|
+
|
|
179
|
+
(0, _setupTests.expectObjectLike)(ROWS[1], {
|
|
180
|
+
pageId: PAGE.id,
|
|
181
|
+
fieldId: COMPONENT.fieldId,
|
|
182
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
183
|
+
key: 'Alpha 1',
|
|
184
|
+
// no countOffset
|
|
185
|
+
action: null,
|
|
186
|
+
component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
|
|
187
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
188
|
+
label: 'Alpha 1' // no countOffset
|
|
189
|
+
|
|
190
|
+
}),
|
|
191
|
+
value: 'Bravo'
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
});
|
|
@@ -2,18 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
var _models = require("../../models");
|
|
4
4
|
|
|
5
|
+
var _setupTests = require("../../setupTests");
|
|
6
|
+
|
|
5
7
|
var _getCYARowsForContainer = _interopRequireDefault(require("./getCYARowsForContainer"));
|
|
6
8
|
|
|
7
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
10
|
|
|
9
11
|
// Local imports
|
|
10
12
|
describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
11
|
-
var expectObjectLike = function expectObjectLike(received, expected) {
|
|
12
|
-
Object.keys(expected).forEach(function (key) {
|
|
13
|
-
expect(received[key]).toEqual(expected[key]);
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
|
|
17
13
|
it('should get an appropriate row for a container with a single readonly text component', function () {
|
|
18
14
|
var FORM_DATA = {
|
|
19
15
|
container: {
|
|
@@ -46,7 +42,7 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
46
42
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
|
|
47
43
|
expect(ROWS.length).toEqual(1);
|
|
48
44
|
ROWS.forEach(function (row, index) {
|
|
49
|
-
expectObjectLike(row, {
|
|
45
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
50
46
|
pageId: PAGE.id,
|
|
51
47
|
fieldId: CONTAINER.components[index].fieldId,
|
|
52
48
|
key: CONTAINER.components[index].label,
|
|
@@ -94,14 +90,14 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
94
90
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
|
|
95
91
|
expect(ROWS.length).toEqual(2);
|
|
96
92
|
ROWS.forEach(function (row, index) {
|
|
97
|
-
expectObjectLike(row, {
|
|
93
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
98
94
|
pageId: PAGE.id,
|
|
99
95
|
fieldId: CONTAINER.components[index].fieldId,
|
|
100
96
|
key: CONTAINER.components[index].label,
|
|
101
97
|
component: CONTAINER.components[index],
|
|
102
98
|
value: "".concat(CONTAINER.components[index].label, " Charlie")
|
|
103
99
|
});
|
|
104
|
-
expectObjectLike(row.action, {
|
|
100
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
105
101
|
onAction: ON_ACTION
|
|
106
102
|
});
|
|
107
103
|
});
|
|
@@ -148,14 +144,14 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
148
144
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
|
|
149
145
|
expect(ROWS.length).toEqual(2);
|
|
150
146
|
ROWS.forEach(function (row, index) {
|
|
151
|
-
expectObjectLike(row, {
|
|
147
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
152
148
|
pageId: PAGE.id,
|
|
153
149
|
fieldId: CONTAINER.components[index].fieldId,
|
|
154
150
|
key: CONTAINER.components[index].label,
|
|
155
151
|
component: CONTAINER.components[index],
|
|
156
152
|
value: "".concat(CONTAINER.components[index].label, " Charlie")
|
|
157
153
|
});
|
|
158
|
-
expectObjectLike(row.action, {
|
|
154
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
159
155
|
onAction: ON_ACTION
|
|
160
156
|
});
|
|
161
157
|
});
|
|
@@ -202,7 +198,7 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
202
198
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
|
|
203
199
|
expect(ROWS.length).toEqual(1);
|
|
204
200
|
ROWS.forEach(function (row, index) {
|
|
205
|
-
expectObjectLike(row, {
|
|
201
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
206
202
|
pageId: PAGE.id,
|
|
207
203
|
fieldId: NESTED_CONTAINER.components[index].fieldId,
|
|
208
204
|
key: NESTED_CONTAINER.components[index].label,
|
|
@@ -244,7 +240,7 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
244
240
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, undefined, ON_ACTION);
|
|
245
241
|
expect(ROWS.length).toEqual(1);
|
|
246
242
|
ROWS.forEach(function (row, index) {
|
|
247
|
-
expectObjectLike(row, {
|
|
243
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
248
244
|
pageId: PAGE.id,
|
|
249
245
|
fieldId: NESTED_CONTAINER.components[index].fieldId,
|
|
250
246
|
key: NESTED_CONTAINER.components[index].label,
|
|
@@ -13,6 +13,8 @@ var _getCYARow = _interopRequireDefault(require("./getCYARow"));
|
|
|
13
13
|
|
|
14
14
|
var _getCYARowForGroup2 = _interopRequireDefault(require("./getCYARowForGroup"));
|
|
15
15
|
|
|
16
|
+
var _getCYARowsForCollection = _interopRequireDefault(require("./getCYARowsForCollection"));
|
|
17
|
+
|
|
16
18
|
var _getCYARowsForContainer = _interopRequireDefault(require("./getCYARowsForContainer"));
|
|
17
19
|
|
|
18
20
|
var _showComponentCYA = _interopRequireDefault(require("./showComponentCYA"));
|
|
@@ -36,11 +38,16 @@ var getCYARowsForPage = function getCYARowsForPage(page, onAction) {
|
|
|
36
38
|
var rows = page.components.filter(function (c) {
|
|
37
39
|
return (0, _showComponentCYA.default)(c, page.formData);
|
|
38
40
|
}).flatMap(function (component) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
switch (component.type) {
|
|
42
|
+
case _models.ComponentTypes.CONTAINER:
|
|
43
|
+
return (0, _getCYARowsForContainer.default)(page, component, page.formData[component.fieldId], onAction);
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
case _models.ComponentTypes.COLLECTION:
|
|
46
|
+
return (0, _getCYARowsForCollection.default)(page, component, page.formData[component.fieldId], onAction);
|
|
47
|
+
|
|
48
|
+
default:
|
|
49
|
+
return (0, _getCYARow.default)(page, component, onAction);
|
|
50
|
+
}
|
|
44
51
|
});
|
|
45
52
|
|
|
46
53
|
if (((_page$groups = page.groups) === null || _page$groups === void 0 ? void 0 : _page$groups.length) > 0) {
|
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
var _react = require("@testing-library/react");
|
|
4
4
|
|
|
5
|
+
var _setupTests = require("../../setupTests");
|
|
6
|
+
|
|
5
7
|
var _models = require("../../models");
|
|
6
8
|
|
|
7
9
|
var _getCYARowsForPage = _interopRequireDefault(require("./getCYARowsForPage"));
|
|
8
10
|
|
|
9
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
14
|
+
|
|
15
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
16
|
+
|
|
17
|
+
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; }
|
|
18
|
+
|
|
13
19
|
describe('utils', function () {
|
|
14
20
|
describe('CheckYourAnswers', function () {
|
|
15
21
|
describe('getCYARowsForPage', function () {
|
|
16
|
-
var expectObjectLike = function expectObjectLike(received, expected) {
|
|
17
|
-
Object.keys(expected).forEach(function (key) {
|
|
18
|
-
expect(received[key]).toEqual(expected[key]);
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
22
|
it('should get a appropriate row for a page with a single readonly text component', function () {
|
|
23
23
|
var COMPONENT = {
|
|
24
24
|
type: 'text',
|
|
@@ -40,7 +40,7 @@ describe('utils', function () {
|
|
|
40
40
|
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
|
|
41
41
|
expect(ROWS.length).toEqual(1);
|
|
42
42
|
ROWS.forEach(function (row, index) {
|
|
43
|
-
expectObjectLike(row, {
|
|
43
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
44
44
|
pageId: PAGE.id,
|
|
45
45
|
fieldId: PAGE.components[index].fieldId,
|
|
46
46
|
key: PAGE.components[index].label,
|
|
@@ -78,14 +78,14 @@ describe('utils', function () {
|
|
|
78
78
|
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
|
|
79
79
|
expect(ROWS.length).toEqual(2);
|
|
80
80
|
ROWS.forEach(function (row, index) {
|
|
81
|
-
expectObjectLike(row, {
|
|
81
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
82
82
|
pageId: PAGE.id,
|
|
83
83
|
fieldId: PAGE.components[index].fieldId,
|
|
84
84
|
key: PAGE.components[index].label,
|
|
85
85
|
component: PAGE.components[index],
|
|
86
86
|
value: "".concat(PAGE.components[index].label, " Charlie")
|
|
87
87
|
});
|
|
88
|
-
expectObjectLike(row.action, {
|
|
88
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
89
89
|
onAction: ON_ACTION
|
|
90
90
|
});
|
|
91
91
|
});
|
|
@@ -122,14 +122,14 @@ describe('utils', function () {
|
|
|
122
122
|
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
|
|
123
123
|
expect(ROWS.length).toEqual(2);
|
|
124
124
|
ROWS.forEach(function (row, index) {
|
|
125
|
-
expectObjectLike(row, {
|
|
125
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
126
126
|
pageId: PAGE.id,
|
|
127
127
|
fieldId: PAGE.components[index].fieldId,
|
|
128
128
|
key: PAGE.components[index].label,
|
|
129
129
|
component: PAGE.components[index],
|
|
130
130
|
value: "".concat(PAGE.components[index].label, " Charlie")
|
|
131
131
|
});
|
|
132
|
-
expectObjectLike(row.action, {
|
|
132
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
133
133
|
onAction: ON_ACTION
|
|
134
134
|
});
|
|
135
135
|
});
|
|
@@ -166,7 +166,7 @@ describe('utils', function () {
|
|
|
166
166
|
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
|
|
167
167
|
expect(ROWS.length).toEqual(1);
|
|
168
168
|
ROWS.forEach(function (row, index) {
|
|
169
|
-
expectObjectLike(row, {
|
|
169
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
170
170
|
pageId: PAGE.id,
|
|
171
171
|
fieldId: CONTAINER.components[index].fieldId,
|
|
172
172
|
key: CONTAINER.components[index].label,
|
|
@@ -234,6 +234,50 @@ describe('utils', function () {
|
|
|
234
234
|
expect(addressValues[2].childNodes[0].textContent).toEqual('London');
|
|
235
235
|
expect(addressValues[3].childNodes[0].textContent).toEqual('SW1A 2AA');
|
|
236
236
|
});
|
|
237
|
+
it('should get appropriate rows for a page with a single readonly text component within a collection', function () {
|
|
238
|
+
var FORM_DATA = {
|
|
239
|
+
collection: [{
|
|
240
|
+
a: 'Bravo'
|
|
241
|
+
}]
|
|
242
|
+
};
|
|
243
|
+
var COMPONENT = {
|
|
244
|
+
type: 'text',
|
|
245
|
+
readonly: true,
|
|
246
|
+
id: 'a',
|
|
247
|
+
fieldId: 'a',
|
|
248
|
+
label: 'Alpha'
|
|
249
|
+
};
|
|
250
|
+
var COLLECTION = {
|
|
251
|
+
id: 'collection',
|
|
252
|
+
fieldId: 'collection',
|
|
253
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
254
|
+
item: [COMPONENT],
|
|
255
|
+
value: FORM_DATA.collection,
|
|
256
|
+
formData: FORM_DATA
|
|
257
|
+
};
|
|
258
|
+
var PAGE = {
|
|
259
|
+
id: 'page',
|
|
260
|
+
components: [COLLECTION],
|
|
261
|
+
formData: FORM_DATA
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
265
|
+
|
|
266
|
+
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
|
|
267
|
+
expect(ROWS.length).toEqual(2); // Title and item row
|
|
268
|
+
|
|
269
|
+
(0, _setupTests.expectObjectLike)(ROWS[1], {
|
|
270
|
+
pageId: PAGE.id,
|
|
271
|
+
fieldId: COMPONENT.fieldId,
|
|
272
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
273
|
+
key: COMPONENT.label,
|
|
274
|
+
action: null,
|
|
275
|
+
component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
|
|
276
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId)
|
|
277
|
+
}),
|
|
278
|
+
value: 'Bravo'
|
|
279
|
+
});
|
|
280
|
+
});
|
|
237
281
|
});
|
|
238
282
|
});
|
|
239
283
|
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _models = require("../../models");
|
|
9
|
+
|
|
10
|
+
var _DEFAULT_VALUES;
|
|
11
|
+
|
|
12
|
+
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; }
|
|
13
|
+
|
|
14
|
+
var DEFAULT_VALUES = (_DEFAULT_VALUES = {}, _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.COLLECTION, []), _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.CONTAINER, {}), _defineProperty(_DEFAULT_VALUES, _models.ComponentTypes.FILE, {}), _DEFAULT_VALUES);
|
|
15
|
+
|
|
16
|
+
var getDefaultValue = function getDefaultValue(component) {
|
|
17
|
+
if (component) {
|
|
18
|
+
return DEFAULT_VALUES[component.type] || '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return '';
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var _default = getDefaultValue;
|
|
25
|
+
exports.default = _default;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../models");
|
|
4
|
+
|
|
5
|
+
var _getDefaultValue = _interopRequireDefault(require("./getDefaultValue"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
+
|
|
9
|
+
describe('utils.Component.defaultValue', function () {
|
|
10
|
+
it('should return an empty string if the component is undefined', function () {
|
|
11
|
+
expect((0, _getDefaultValue.default)(undefined)).toEqual('');
|
|
12
|
+
});
|
|
13
|
+
it('should return an empty string if the component is null', function () {
|
|
14
|
+
expect((0, _getDefaultValue.default)(null)).toEqual('');
|
|
15
|
+
});
|
|
16
|
+
it('should return an empty string if the component has no type', function () {
|
|
17
|
+
expect((0, _getDefaultValue.default)({})).toEqual('');
|
|
18
|
+
});
|
|
19
|
+
it('should return an empty string if the component type is null', function () {
|
|
20
|
+
expect((0, _getDefaultValue.default)({
|
|
21
|
+
type: null
|
|
22
|
+
})).toEqual('');
|
|
23
|
+
});
|
|
24
|
+
it('should return an empty string if the component type is undefined', function () {
|
|
25
|
+
expect((0, _getDefaultValue.default)({
|
|
26
|
+
type: undefined
|
|
27
|
+
})).toEqual('');
|
|
28
|
+
});
|
|
29
|
+
it("should return an empty array if the component type is 'collection'", function () {
|
|
30
|
+
expect((0, _getDefaultValue.default)({
|
|
31
|
+
type: _models.ComponentTypes.COLLECTION
|
|
32
|
+
})).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
it("should return an empty object if the component type is 'container'", function () {
|
|
35
|
+
expect((0, _getDefaultValue.default)({
|
|
36
|
+
type: _models.ComponentTypes.CONTAINER
|
|
37
|
+
})).toEqual({});
|
|
38
|
+
});
|
|
39
|
+
it("should return an empty object if the component type is 'file'", function () {
|
|
40
|
+
expect((0, _getDefaultValue.default)({
|
|
41
|
+
type: _models.ComponentTypes.FILE
|
|
42
|
+
})).toEqual({});
|
|
43
|
+
});
|
|
44
|
+
Object.values(_models.ComponentTypes).forEach(function (value) {
|
|
45
|
+
if (![_models.ComponentTypes.COLLECTION, _models.ComponentTypes.CONTAINER, _models.ComponentTypes.FILE].includes(value)) {
|
|
46
|
+
it("should return an empty object if the component type is '".concat(value, "'"), function () {
|
|
47
|
+
expect((0, _getDefaultValue.default)({
|
|
48
|
+
type: _models.ComponentTypes.TEXT
|
|
49
|
+
})).toEqual('');
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -9,6 +9,8 @@ var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
|
|
|
9
9
|
|
|
10
10
|
var _getComponent = _interopRequireDefault(require("./getComponent"));
|
|
11
11
|
|
|
12
|
+
var _getDefaultValue = _interopRequireDefault(require("./getDefaultValue"));
|
|
13
|
+
|
|
12
14
|
var _isEditable = _interopRequireDefault(require("./isEditable"));
|
|
13
15
|
|
|
14
16
|
var _showComponent = _interopRequireDefault(require("./showComponent"));
|
|
@@ -19,8 +21,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
19
21
|
|
|
20
22
|
var Component = {
|
|
21
23
|
clean: _cleanAttributes.default,
|
|
22
|
-
|
|
24
|
+
defaultValue: _getDefaultValue.default,
|
|
23
25
|
editable: _isEditable.default,
|
|
26
|
+
get: _getComponent.default,
|
|
24
27
|
show: _showComponent.default,
|
|
25
28
|
wrap: _wrapInFormGroup.default
|
|
26
29
|
};
|
|
@@ -5,6 +5,33 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
9
|
+
|
|
10
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
11
|
+
|
|
12
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
13
|
+
|
|
14
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
15
|
+
|
|
16
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
17
|
+
|
|
18
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
19
|
+
|
|
20
|
+
var getItemInArray = function getItemInArray(arr, index) {
|
|
21
|
+
if (Array.isArray(arr)) {
|
|
22
|
+
return arr[parseInt(index, 10)];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var isArrayProp = function isArrayProp(prop) {
|
|
29
|
+
return prop.includes('[');
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
var getPropertyAndIndex = function getPropertyAndIndex(prop) {
|
|
33
|
+
return prop.replace(']', '').split('[');
|
|
34
|
+
};
|
|
8
35
|
/**
|
|
9
36
|
* Gets the value of a field within the top-level JSON form data,
|
|
10
37
|
* based on a dot-separated field identifier.
|
|
@@ -20,12 +47,23 @@ exports.default = void 0;
|
|
|
20
47
|
* @param {string} fieldId The dot-separated path to the field.
|
|
21
48
|
* @returns The value of the field specified.
|
|
22
49
|
*/
|
|
50
|
+
|
|
51
|
+
|
|
23
52
|
var getSourceData = function getSourceData(data, fieldId) {
|
|
24
53
|
if (!fieldId) {
|
|
25
54
|
return undefined;
|
|
26
55
|
}
|
|
27
56
|
|
|
28
57
|
return fieldId.split('.').reduce(function (obj, prop) {
|
|
58
|
+
if (obj && isArrayProp(prop)) {
|
|
59
|
+
var _getPropertyAndIndex = getPropertyAndIndex(prop),
|
|
60
|
+
_getPropertyAndIndex2 = _slicedToArray(_getPropertyAndIndex, 2),
|
|
61
|
+
actualProp = _getPropertyAndIndex2[0],
|
|
62
|
+
index = _getPropertyAndIndex2[1];
|
|
63
|
+
|
|
64
|
+
return getItemInArray(obj[actualProp], index);
|
|
65
|
+
}
|
|
66
|
+
|
|
29
67
|
return obj ? obj[prop] : undefined;
|
|
30
68
|
}, data);
|
|
31
69
|
};
|
|
@@ -120,6 +120,22 @@ describe('utils', function () {
|
|
|
120
120
|
var value = (0, _getSourceData.default)(DATA, FIELD_ID);
|
|
121
121
|
expect(value).toBeUndefined();
|
|
122
122
|
});
|
|
123
|
+
it('should handle a reference to an array item', function () {
|
|
124
|
+
var FIELD_ID = 'items[1]';
|
|
125
|
+
var ALPHA = 'alpha';
|
|
126
|
+
var BETA = 'beta';
|
|
127
|
+
var DATA = {
|
|
128
|
+
items: [ALPHA, BETA]
|
|
129
|
+
};
|
|
130
|
+
var value = (0, _getSourceData.default)(DATA, FIELD_ID);
|
|
131
|
+
expect(value).toEqual(BETA);
|
|
132
|
+
});
|
|
133
|
+
it('should handle a reference to an array that does not exist', function () {
|
|
134
|
+
var FIELD_ID = 'items[1]';
|
|
135
|
+
var DATA = {};
|
|
136
|
+
var value = (0, _getSourceData.default)(DATA, FIELD_ID);
|
|
137
|
+
expect(value).toBeUndefined();
|
|
138
|
+
});
|
|
123
139
|
});
|
|
124
140
|
});
|
|
125
141
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"lint": "eslint --ext .js,.jsx src",
|
|
10
10
|
"storybook:start": "start-storybook --docs -s src/assets --no-manager-cache -p 6007",
|
|
11
11
|
"storybook:build": "build-storybook --docs -s src/assets",
|
|
12
|
-
"storybook:deploy": "yarn deploy-storybook",
|
|
12
|
+
"storybook:deploy": "yarn storybook:build && yarn deploy-storybook",
|
|
13
13
|
"storybook": "yarn storybook:start",
|
|
14
14
|
"deploy-storybook": "storybook-to-ghpages -e storybook-static",
|
|
15
15
|
"compile": "yarn clean && cross-env NODE_ENV=production babel src --out-dir dist --copy-files && yarn post-compile",
|