@ukhomeoffice/cop-react-form-renderer 0.1.0-beta → 1.0.0-delta
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/Answer.js +48 -0
- package/dist/components/CheckYourAnswers/Answer.test.js +94 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +64 -19
- package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +0 -7
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +100 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +317 -0
- package/dist/components/FormComponent/FormComponent.js +7 -7
- package/dist/components/FormComponent/FormComponent.stories.mdx +167 -0
- package/dist/components/FormComponent/FormComponent.test.js +137 -0
- package/dist/components/FormPage/FormPage.js +12 -11
- package/dist/components/FormPage/FormPage.stories.mdx +118 -0
- package/dist/components/FormPage/FormPage.test.js +255 -0
- package/dist/components/FormRenderer/FormRenderer.js +70 -58
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +90 -0
- package/dist/components/FormRenderer/FormRenderer.test.js +469 -0
- package/dist/components/FormRenderer/handlers/cyaAction.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.test.js +61 -0
- package/dist/components/FormRenderer/handlers/handlers.test.js +97 -0
- package/dist/components/FormRenderer/handlers/index.js +23 -0
- package/dist/components/FormRenderer/handlers/navigate.js +28 -0
- package/dist/components/FormRenderer/handlers/submissionError.js +20 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.js +35 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.test.js +73 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.js +42 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +155 -0
- package/dist/components/FormRenderer/helpers/getCYA.js +31 -0
- package/dist/components/FormRenderer/helpers/getCYA.test.js +38 -0
- package/dist/components/FormRenderer/helpers/getFormState.js +32 -0
- package/dist/components/FormRenderer/helpers/getFormState.test.js +61 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.js +65 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.test.js +90 -0
- package/dist/components/FormRenderer/helpers/getPage.js +34 -0
- package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
- package/dist/components/FormRenderer/helpers/index.js +27 -0
- package/dist/components/PageActions/ActionButton.js +58 -0
- package/dist/components/PageActions/ActionButton.test.js +116 -0
- package/dist/components/PageActions/PageActions.js +18 -25
- package/dist/components/PageActions/PageActions.stories.mdx +74 -0
- package/dist/components/PageActions/PageActions.test.js +157 -0
- package/dist/components/SummaryList/RowAction.js +13 -22
- package/dist/components/SummaryList/RowAction.test.js +94 -0
- package/dist/components/SummaryList/SummaryList.js +6 -20
- package/dist/components/SummaryList/SummaryList.scss +7 -0
- package/dist/components/SummaryList/SummaryList.stories.mdx +36 -0
- package/dist/components/SummaryList/SummaryList.test.js +197 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.js +27 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +77 -0
- package/dist/components/SummaryList/helpers/index.js +15 -0
- package/dist/components/index.js +8 -0
- package/dist/hooks/useGetRequest.js +16 -1
- package/dist/hooks/useHooks.js +16 -5
- package/dist/index.js +3 -1
- package/dist/models/EventTypes.js +14 -0
- package/dist/models/FormPages.js +14 -0
- package/dist/models/PageAction.js +40 -0
- package/dist/models/index.js +24 -0
- package/dist/utils/CheckYourAnswers/getCYAAction.js +9 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +10 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -0
- package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -5
- package/dist/utils/Component/getComponent.js +2 -2
- package/dist/utils/Component/getComponent.test.js +1 -1
- package/dist/utils/Data/setupFormData.js +3 -3
- package/dist/utils/Data/setupFormData.test.js +6 -6
- package/dist/utils/Data/setupRefDataUrlForComponent.test.js +4 -4
- package/dist/utils/FormPage/getFormPage.js +12 -1
- package/dist/utils/FormPage/getFormPage.test.js +178 -0
- package/dist/utils/FormPage/getFormPages.js +11 -6
- package/dist/utils/FormPage/getFormPages.test.js +95 -0
- package/dist/utils/FormPage/getParagraphFromText.js +6 -0
- package/dist/utils/FormPage/getParagraphFromText.test.js +29 -0
- package/dist/utils/FormPage/useComponent.js +9 -2
- package/dist/utils/FormPage/useComponent.test.js +82 -0
- package/dist/utils/Format/formatData.js +32 -0
- package/dist/utils/Format/formatData.test.js +46 -0
- package/dist/utils/Format/formatDataForComponent.js +41 -0
- package/dist/utils/Format/formatDataForComponent.test.js +161 -0
- package/dist/utils/Format/formatDataForForm.js +33 -0
- package/dist/utils/Format/formatDataForForm.test.js +78 -0
- package/dist/utils/Format/formatDataForPage.js +37 -0
- package/dist/utils/Format/formatDataForPage.test.js +96 -0
- package/dist/utils/Format/index.js +26 -0
- package/dist/utils/Hub/getFormHub.js +18 -4
- package/dist/utils/Hub/getFormHub.test.js +96 -0
- package/dist/utils/Hub/index.js +0 -3
- package/dist/utils/Validate/validatePage.test.js +1 -1
- package/dist/utils/index.js +3 -0
- package/package.json +13 -9
- package/dist/components/FormRenderer/helpers.js +0 -66
- package/dist/components/FormRenderer/helpers.test.js +0 -158
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
4
|
+
|
|
5
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
6
|
+
|
|
7
|
+
var _axiosMockAdapter = _interopRequireDefault(require("axios-mock-adapter"));
|
|
8
|
+
|
|
9
|
+
var _react = _interopRequireDefault(require("react"));
|
|
10
|
+
|
|
11
|
+
var _reactDom = require("react-dom");
|
|
12
|
+
|
|
13
|
+
var _testUtils = require("react-dom/test-utils");
|
|
14
|
+
|
|
15
|
+
var _areYouACivilServant = _interopRequireDefault(require("../../json/areYouACivilServant.json"));
|
|
16
|
+
|
|
17
|
+
var _grade = _interopRequireDefault(require("../../json/grade.json"));
|
|
18
|
+
|
|
19
|
+
var _team = _interopRequireDefault(require("../../json/team.json"));
|
|
20
|
+
|
|
21
|
+
var _userProfileData = _interopRequireDefault(require("../../json/userProfile.data.json"));
|
|
22
|
+
|
|
23
|
+
var _userProfile = _interopRequireDefault(require("../../json/userProfile.json"));
|
|
24
|
+
|
|
25
|
+
var _utils = _interopRequireDefault(require("../../utils"));
|
|
26
|
+
|
|
27
|
+
var _CheckYourAnswers = _interopRequireWildcard(require("./CheckYourAnswers"));
|
|
28
|
+
|
|
29
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
30
|
+
|
|
31
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
32
|
+
|
|
33
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
34
|
+
|
|
35
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
36
|
+
|
|
37
|
+
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."); }
|
|
38
|
+
|
|
39
|
+
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); }
|
|
40
|
+
|
|
41
|
+
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; }
|
|
42
|
+
|
|
43
|
+
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; }
|
|
44
|
+
|
|
45
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
46
|
+
|
|
47
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
48
|
+
|
|
49
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
50
|
+
|
|
51
|
+
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; }
|
|
52
|
+
|
|
53
|
+
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; }
|
|
54
|
+
|
|
55
|
+
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; }
|
|
56
|
+
|
|
57
|
+
describe('components', function () {
|
|
58
|
+
describe('CheckYourAnswers', function () {
|
|
59
|
+
var mockAxios = new _axiosMockAdapter.default(_axios.default);
|
|
60
|
+
|
|
61
|
+
var DATA = _utils.default.Data.setupForm(_userProfile.default.pages, _userProfile.default.components, _userProfileData.default);
|
|
62
|
+
|
|
63
|
+
var PAGES = _utils.default.FormPage.getAll(_userProfile.default.pages, _userProfile.default.components, _objectSpread({}, DATA));
|
|
64
|
+
|
|
65
|
+
var ON_ACTION_CALLS = [];
|
|
66
|
+
|
|
67
|
+
var ON_ACTION = function ON_ACTION(row) {
|
|
68
|
+
ON_ACTION_CALLS.push(row);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
var container = null;
|
|
72
|
+
|
|
73
|
+
var refDataUrl = function refDataUrl(suffix) {
|
|
74
|
+
return "".concat(_userProfileData.default.urls.refData, "/").concat(suffix);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
beforeEach(function () {
|
|
78
|
+
ON_ACTION_CALLS.length = 0;
|
|
79
|
+
container = document.createElement('div');
|
|
80
|
+
document.body.appendChild(container);
|
|
81
|
+
mockAxios.reset();
|
|
82
|
+
mockAxios.onGet(refDataUrl('areYouACivilServant')).reply(200, _areYouACivilServant.default);
|
|
83
|
+
mockAxios.onGet(refDataUrl('grade')).reply(200, _grade.default);
|
|
84
|
+
mockAxios.onGet(refDataUrl('team')).reply(200, _team.default);
|
|
85
|
+
});
|
|
86
|
+
afterEach(function () {
|
|
87
|
+
(0, _reactDom.unmountComponentAtNode)(container);
|
|
88
|
+
container.remove();
|
|
89
|
+
container = null;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
var checkCYA = function checkCYA(container) {
|
|
93
|
+
var cya = container.childNodes[0];
|
|
94
|
+
expect(cya.tagName).toEqual('DIV');
|
|
95
|
+
expect(cya.classList).toContain(_CheckYourAnswers.DEFAULT_CLASS);
|
|
96
|
+
return cya;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
var checkRow = function checkRow(row, key, value, action) {
|
|
100
|
+
expect(row.textContent).toContain(key);
|
|
101
|
+
expect(row.textContent).toContain(value);
|
|
102
|
+
|
|
103
|
+
if (action) {
|
|
104
|
+
expect(row.textContent).toContain('Change');
|
|
105
|
+
} else {
|
|
106
|
+
expect(row.textContent).not.toContain('Change');
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
it('should show default title if none is provided', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
111
|
+
var cya, title;
|
|
112
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
113
|
+
while (1) {
|
|
114
|
+
switch (_context2.prev = _context2.next) {
|
|
115
|
+
case 0:
|
|
116
|
+
_context2.next = 2;
|
|
117
|
+
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
118
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
119
|
+
while (1) {
|
|
120
|
+
switch (_context.prev = _context.next) {
|
|
121
|
+
case 0:
|
|
122
|
+
(0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
|
|
123
|
+
pages: PAGES,
|
|
124
|
+
onAction: ON_ACTION
|
|
125
|
+
}), container);
|
|
126
|
+
|
|
127
|
+
case 1:
|
|
128
|
+
case "end":
|
|
129
|
+
return _context.stop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}, _callee);
|
|
133
|
+
})));
|
|
134
|
+
|
|
135
|
+
case 2:
|
|
136
|
+
cya = checkCYA(container);
|
|
137
|
+
title = cya.childNodes[0];
|
|
138
|
+
expect(title.classList).toContain('govuk-heading-l');
|
|
139
|
+
expect(title.textContent).toEqual(_CheckYourAnswers.DEFAULT_TITLE);
|
|
140
|
+
|
|
141
|
+
case 6:
|
|
142
|
+
case "end":
|
|
143
|
+
return _context2.stop();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}, _callee2);
|
|
147
|
+
})));
|
|
148
|
+
it('should allow title to be overridden', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
|
|
149
|
+
var TITLE, cya, title;
|
|
150
|
+
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
151
|
+
while (1) {
|
|
152
|
+
switch (_context4.prev = _context4.next) {
|
|
153
|
+
case 0:
|
|
154
|
+
TITLE = 'Alpha Bravo Charlie';
|
|
155
|
+
_context4.next = 3;
|
|
156
|
+
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
|
|
157
|
+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
158
|
+
while (1) {
|
|
159
|
+
switch (_context3.prev = _context3.next) {
|
|
160
|
+
case 0:
|
|
161
|
+
(0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
|
|
162
|
+
title: TITLE,
|
|
163
|
+
pages: PAGES,
|
|
164
|
+
onAction: ON_ACTION
|
|
165
|
+
}), container);
|
|
166
|
+
|
|
167
|
+
case 1:
|
|
168
|
+
case "end":
|
|
169
|
+
return _context3.stop();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}, _callee3);
|
|
173
|
+
})));
|
|
174
|
+
|
|
175
|
+
case 3:
|
|
176
|
+
cya = checkCYA(container);
|
|
177
|
+
title = cya.childNodes[0];
|
|
178
|
+
expect(title.classList).toContain('govuk-heading-l');
|
|
179
|
+
expect(title.textContent).toEqual(TITLE);
|
|
180
|
+
|
|
181
|
+
case 7:
|
|
182
|
+
case "end":
|
|
183
|
+
return _context4.stop();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}, _callee4);
|
|
187
|
+
})));
|
|
188
|
+
it('should show readonly page components correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
|
|
189
|
+
var cya, _cya$childNodes, names, _names$childNodes, firstName, surname;
|
|
190
|
+
|
|
191
|
+
return regeneratorRuntime.wrap(function _callee6$(_context6) {
|
|
192
|
+
while (1) {
|
|
193
|
+
switch (_context6.prev = _context6.next) {
|
|
194
|
+
case 0:
|
|
195
|
+
_context6.next = 2;
|
|
196
|
+
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
|
|
197
|
+
return regeneratorRuntime.wrap(function _callee5$(_context5) {
|
|
198
|
+
while (1) {
|
|
199
|
+
switch (_context5.prev = _context5.next) {
|
|
200
|
+
case 0:
|
|
201
|
+
(0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
|
|
202
|
+
pages: PAGES,
|
|
203
|
+
onAction: ON_ACTION
|
|
204
|
+
}), container);
|
|
205
|
+
|
|
206
|
+
case 1:
|
|
207
|
+
case "end":
|
|
208
|
+
return _context5.stop();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}, _callee5);
|
|
212
|
+
})));
|
|
213
|
+
|
|
214
|
+
case 2:
|
|
215
|
+
cya = checkCYA(container);
|
|
216
|
+
_cya$childNodes = _slicedToArray(cya.childNodes, 2), names = _cya$childNodes[1];
|
|
217
|
+
expect(names.tagName).toEqual('DL');
|
|
218
|
+
expect(names.classList).toContain("govuk-!-margin-bottom-".concat(_CheckYourAnswers.DEFAULT_MARGIN_BOTTOM));
|
|
219
|
+
_names$childNodes = _slicedToArray(names.childNodes, 2), firstName = _names$childNodes[0], surname = _names$childNodes[1];
|
|
220
|
+
checkRow(firstName, 'First name', 'John', false);
|
|
221
|
+
checkRow(surname, 'Last name', 'Smith', false);
|
|
222
|
+
|
|
223
|
+
case 9:
|
|
224
|
+
case "end":
|
|
225
|
+
return _context6.stop();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}, _callee6);
|
|
229
|
+
})));
|
|
230
|
+
it('should show editable page components correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() {
|
|
231
|
+
var cya, _cya$childNodes2, title, civilServant, _civilServant$childNo, status;
|
|
232
|
+
|
|
233
|
+
return regeneratorRuntime.wrap(function _callee8$(_context8) {
|
|
234
|
+
while (1) {
|
|
235
|
+
switch (_context8.prev = _context8.next) {
|
|
236
|
+
case 0:
|
|
237
|
+
_context8.next = 2;
|
|
238
|
+
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
|
|
239
|
+
return regeneratorRuntime.wrap(function _callee7$(_context7) {
|
|
240
|
+
while (1) {
|
|
241
|
+
switch (_context7.prev = _context7.next) {
|
|
242
|
+
case 0:
|
|
243
|
+
(0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
|
|
244
|
+
pages: PAGES,
|
|
245
|
+
onAction: ON_ACTION
|
|
246
|
+
}), container);
|
|
247
|
+
|
|
248
|
+
case 1:
|
|
249
|
+
case "end":
|
|
250
|
+
return _context7.stop();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}, _callee7);
|
|
254
|
+
})));
|
|
255
|
+
|
|
256
|
+
case 2:
|
|
257
|
+
cya = checkCYA(container);
|
|
258
|
+
_cya$childNodes2 = _slicedToArray(cya.childNodes, 4), title = _cya$childNodes2[2], civilServant = _cya$childNodes2[3];
|
|
259
|
+
expect(title.textContent).toEqual('Are you a civil servant?');
|
|
260
|
+
expect(civilServant.tagName).toEqual('DL');
|
|
261
|
+
expect(civilServant.classList).toContain("govuk-!-margin-bottom-".concat(_CheckYourAnswers.DEFAULT_MARGIN_BOTTOM));
|
|
262
|
+
_civilServant$childNo = _slicedToArray(civilServant.childNodes, 1), status = _civilServant$childNo[0];
|
|
263
|
+
checkRow(status, 'Are you a civil servant?', 'Yes', true);
|
|
264
|
+
|
|
265
|
+
case 9:
|
|
266
|
+
case "end":
|
|
267
|
+
return _context8.stop();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}, _callee8);
|
|
271
|
+
})));
|
|
272
|
+
it('should show editable page components correctly with page titles hidden', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() {
|
|
273
|
+
var cya, _cya$childNodes3, civilServant, _civilServant$childNo2, status;
|
|
274
|
+
|
|
275
|
+
return regeneratorRuntime.wrap(function _callee10$(_context10) {
|
|
276
|
+
while (1) {
|
|
277
|
+
switch (_context10.prev = _context10.next) {
|
|
278
|
+
case 0:
|
|
279
|
+
_context10.next = 2;
|
|
280
|
+
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
|
|
281
|
+
return regeneratorRuntime.wrap(function _callee9$(_context9) {
|
|
282
|
+
while (1) {
|
|
283
|
+
switch (_context9.prev = _context9.next) {
|
|
284
|
+
case 0:
|
|
285
|
+
(0, _reactDom.render)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
|
|
286
|
+
pages: PAGES,
|
|
287
|
+
onAction: ON_ACTION,
|
|
288
|
+
hide_page_titles: true
|
|
289
|
+
}), container);
|
|
290
|
+
|
|
291
|
+
case 1:
|
|
292
|
+
case "end":
|
|
293
|
+
return _context9.stop();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}, _callee9);
|
|
297
|
+
})));
|
|
298
|
+
|
|
299
|
+
case 2:
|
|
300
|
+
cya = checkCYA(container);
|
|
301
|
+
_cya$childNodes3 = _slicedToArray(cya.childNodes, 3), civilServant = _cya$childNodes3[2]; // The title simply isn't returned
|
|
302
|
+
|
|
303
|
+
expect(civilServant.tagName).toEqual('DL');
|
|
304
|
+
expect(civilServant.classList).toContain("govuk-!-margin-bottom-0"); // Changed margin if no titles
|
|
305
|
+
|
|
306
|
+
_civilServant$childNo2 = _slicedToArray(civilServant.childNodes, 1), status = _civilServant$childNo2[0];
|
|
307
|
+
checkRow(status, 'Are you a civil servant?', 'Yes', true);
|
|
308
|
+
|
|
309
|
+
case 8:
|
|
310
|
+
case "end":
|
|
311
|
+
return _context10.stop();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}, _callee10);
|
|
315
|
+
})));
|
|
316
|
+
});
|
|
317
|
+
});
|
|
@@ -15,7 +15,7 @@ var _useRefData2 = _interopRequireWildcard(require("../../hooks/useRefData"));
|
|
|
15
15
|
|
|
16
16
|
var _utils = _interopRequireDefault(require("../../utils"));
|
|
17
17
|
|
|
18
|
-
var _excluded = ["component", "value", "
|
|
18
|
+
var _excluded = ["component", "value", "wrap", "onChange"];
|
|
19
19
|
|
|
20
20
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
21
21
|
|
|
@@ -48,7 +48,7 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
|
|
|
48
48
|
var FormComponent = function FormComponent(_ref) {
|
|
49
49
|
var component = _ref.component,
|
|
50
50
|
value = _ref.value,
|
|
51
|
-
|
|
51
|
+
wrap = _ref.wrap,
|
|
52
52
|
onChange = _ref.onChange,
|
|
53
53
|
attrs = _objectWithoutProperties(_ref, _excluded);
|
|
54
54
|
|
|
@@ -70,9 +70,9 @@ var FormComponent = function FormComponent(_ref) {
|
|
|
70
70
|
label: component.label || '',
|
|
71
71
|
hint: component.hint || '',
|
|
72
72
|
options: options,
|
|
73
|
-
value: value,
|
|
73
|
+
value: value || '',
|
|
74
74
|
onChange: onChange
|
|
75
|
-
}),
|
|
75
|
+
}), wrap);
|
|
76
76
|
};
|
|
77
77
|
|
|
78
78
|
FormComponent.propTypes = {
|
|
@@ -84,12 +84,12 @@ FormComponent.propTypes = {
|
|
|
84
84
|
url: _propTypes.default.string
|
|
85
85
|
})
|
|
86
86
|
}).isRequired,
|
|
87
|
-
value: _propTypes.default.any
|
|
88
|
-
|
|
87
|
+
value: _propTypes.default.any,
|
|
88
|
+
wrap: _propTypes.default.bool,
|
|
89
89
|
onChange: _propTypes.default.func
|
|
90
90
|
};
|
|
91
91
|
FormComponent.defaultProps = {
|
|
92
|
-
|
|
92
|
+
wrap: true
|
|
93
93
|
};
|
|
94
94
|
var _default = FormComponent;
|
|
95
95
|
exports.default = _default;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
<!-- Global imports -->
|
|
2
|
+
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';
|
|
3
|
+
import { Details, Heading, Link } from '@ukhomeoffice/cop-react-components';
|
|
4
|
+
import withMock from 'storybook-addon-mock';
|
|
5
|
+
|
|
6
|
+
<!-- Local imports -->
|
|
7
|
+
import FormComponent from './FormComponent';
|
|
8
|
+
|
|
9
|
+
<!-- JSON documents -->
|
|
10
|
+
import CIVIL_SERVANT from '../../json/areYouACivilServant.json';
|
|
11
|
+
import GRADE from '../../json/grade.json';
|
|
12
|
+
import TEAMS from '../../json/team.json';
|
|
13
|
+
import USER_PROFILE_DATA from '../../json/userProfile.data.json';
|
|
14
|
+
import USER_PROFILE from '../../json/userProfile.json';
|
|
15
|
+
|
|
16
|
+
<Meta title="Components/Form component" id="D-FormComponent" component={ FormComponent } decorators={[withMock]} />
|
|
17
|
+
|
|
18
|
+
<Heading size="xl" caption="Components">Form component</Heading>
|
|
19
|
+
|
|
20
|
+
Renders a single component on a form with <Link href="https://ukhomeoffice.github.io/cop-react-components/?path=/docs/d-alert--default-story">COP React components</Link>,
|
|
21
|
+
on the basis of a <Link href="/?path=/docs/f-json-component">JSON</Link> configuration.
|
|
22
|
+
|
|
23
|
+
<Canvas withToolbar>
|
|
24
|
+
<Story name="Default" parameters={{
|
|
25
|
+
mockData: [
|
|
26
|
+
{
|
|
27
|
+
url: `${USER_PROFILE_DATA.urls.refData}/areYouACivilServant`,
|
|
28
|
+
method: 'GET',
|
|
29
|
+
status: 200,
|
|
30
|
+
response: CIVIL_SERVANT
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
url: `${USER_PROFILE_DATA.urls.refData}/grade`,
|
|
34
|
+
method: 'GET',
|
|
35
|
+
status: 200,
|
|
36
|
+
response: GRADE
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
url: `${USER_PROFILE_DATA.urls.refData}/team`,
|
|
40
|
+
method: 'GET',
|
|
41
|
+
status: 200,
|
|
42
|
+
response: TEAMS
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
}}>
|
|
46
|
+
{() => {
|
|
47
|
+
const COMPONENT = {
|
|
48
|
+
...USER_PROFILE.components.find(c => c.id === 'areYouACivilServant'),
|
|
49
|
+
data: {
|
|
50
|
+
url: `${USER_PROFILE_DATA.urls.refData}/areYouACivilServant`
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const ON_CHANGE = ({ target }) => {
|
|
54
|
+
// Do something with the target here.
|
|
55
|
+
};
|
|
56
|
+
return (
|
|
57
|
+
<FormComponent component={COMPONENT} value={''} onChange={ON_CHANGE} />
|
|
58
|
+
);
|
|
59
|
+
}}
|
|
60
|
+
</Story>
|
|
61
|
+
</Canvas>
|
|
62
|
+
|
|
63
|
+
<Details summary="Properties" className="no-indent">
|
|
64
|
+
<ArgsTable of={ FormComponent } />
|
|
65
|
+
</Details>
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
## Variants
|
|
69
|
+
### `autocomplete`
|
|
70
|
+
<Canvas>
|
|
71
|
+
<Story name="Autocomplete" parameters={{
|
|
72
|
+
mockData: [
|
|
73
|
+
{
|
|
74
|
+
url: `${USER_PROFILE_DATA.urls.refData}/areYouACivilServant`,
|
|
75
|
+
method: 'GET',
|
|
76
|
+
status: 200,
|
|
77
|
+
response: CIVIL_SERVANT
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
url: `${USER_PROFILE_DATA.urls.refData}/grade`,
|
|
81
|
+
method: 'GET',
|
|
82
|
+
status: 200,
|
|
83
|
+
response: GRADE
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
url: `${USER_PROFILE_DATA.urls.refData}/team`,
|
|
87
|
+
method: 'GET',
|
|
88
|
+
status: 200,
|
|
89
|
+
response: TEAMS
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
}}>
|
|
93
|
+
{() => {
|
|
94
|
+
const COMPONENT = {
|
|
95
|
+
...USER_PROFILE.components.find(c => c.id === 'team'),
|
|
96
|
+
data: {
|
|
97
|
+
url: `${USER_PROFILE_DATA.urls.refData}/team`
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const ON_CHANGE = ({ target }) => {
|
|
101
|
+
// Do something with the target here.
|
|
102
|
+
};
|
|
103
|
+
return (
|
|
104
|
+
<FormComponent component={COMPONENT} value={''} onChange={ON_CHANGE} />
|
|
105
|
+
);
|
|
106
|
+
}}
|
|
107
|
+
</Story>
|
|
108
|
+
</Canvas>
|
|
109
|
+
|
|
110
|
+
### `html`
|
|
111
|
+
|
|
112
|
+
<Canvas>
|
|
113
|
+
<Story name="HTML">
|
|
114
|
+
{() => {
|
|
115
|
+
const COMPONENT = { type: 'html', tagName: 'p', content: 'A paragraph of text to render' };
|
|
116
|
+
const ON_CHANGE = ({ target }) => {
|
|
117
|
+
// Do something with the target here.
|
|
118
|
+
};
|
|
119
|
+
return (
|
|
120
|
+
<FormComponent component={COMPONENT} value={''} onChange={ON_CHANGE} />
|
|
121
|
+
);
|
|
122
|
+
}}
|
|
123
|
+
</Story>
|
|
124
|
+
</Canvas>
|
|
125
|
+
|
|
126
|
+
### Unwrapped `autocomplete`
|
|
127
|
+
<Canvas>
|
|
128
|
+
<Story name="Unwrapped autocomplete" parameters={{
|
|
129
|
+
mockData: [
|
|
130
|
+
{
|
|
131
|
+
url: `${USER_PROFILE_DATA.urls.refData}/areYouACivilServant`,
|
|
132
|
+
method: 'GET',
|
|
133
|
+
status: 200,
|
|
134
|
+
response: CIVIL_SERVANT
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
url: `${USER_PROFILE_DATA.urls.refData}/grade`,
|
|
138
|
+
method: 'GET',
|
|
139
|
+
status: 200,
|
|
140
|
+
response: GRADE
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
url: `${USER_PROFILE_DATA.urls.refData}/team`,
|
|
144
|
+
method: 'GET',
|
|
145
|
+
status: 200,
|
|
146
|
+
response: TEAMS
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
}}>
|
|
150
|
+
{() => {
|
|
151
|
+
const COMPONENT = {
|
|
152
|
+
...USER_PROFILE.components.find(c => c.id === 'team'),
|
|
153
|
+
id: 'team2',
|
|
154
|
+
fieldId: 'team2',
|
|
155
|
+
data: {
|
|
156
|
+
url: `${USER_PROFILE_DATA.urls.refData}/team`
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const ON_CHANGE = ({ target }) => {
|
|
160
|
+
// Do something with the target here.
|
|
161
|
+
};
|
|
162
|
+
return (
|
|
163
|
+
<FormComponent component={COMPONENT} value={''} wrap={false} onChange={ON_CHANGE} />
|
|
164
|
+
);
|
|
165
|
+
}}
|
|
166
|
+
</Story>
|
|
167
|
+
</Canvas>
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = require("@testing-library/react");
|
|
4
|
+
|
|
5
|
+
var _react2 = _interopRequireDefault(require("react"));
|
|
6
|
+
|
|
7
|
+
var _FormComponent = _interopRequireDefault(require("./FormComponent"));
|
|
8
|
+
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
|
|
11
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
12
|
+
|
|
13
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
14
|
+
|
|
15
|
+
describe('components', function () {
|
|
16
|
+
describe('FormComponent', function () {
|
|
17
|
+
it('should render a text component appropriately', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
18
|
+
var ID, VALUE, COMPONENT, ON_CHANGE, _render, container, formGroup, label, hint, input;
|
|
19
|
+
|
|
20
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
21
|
+
while (1) {
|
|
22
|
+
switch (_context.prev = _context.next) {
|
|
23
|
+
case 0:
|
|
24
|
+
ID = 'component';
|
|
25
|
+
VALUE = 'Text value';
|
|
26
|
+
COMPONENT = {
|
|
27
|
+
id: ID,
|
|
28
|
+
fieldId: ID,
|
|
29
|
+
type: 'text',
|
|
30
|
+
label: 'Text component',
|
|
31
|
+
hint: 'Text hint'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
ON_CHANGE = function ON_CHANGE() {};
|
|
35
|
+
|
|
36
|
+
_render = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_FormComponent.default, {
|
|
37
|
+
"data-testid": ID,
|
|
38
|
+
component: COMPONENT,
|
|
39
|
+
value: VALUE,
|
|
40
|
+
onChange: ON_CHANGE
|
|
41
|
+
})), container = _render.container; // text components are wrapper in a FormGroup by default.
|
|
42
|
+
|
|
43
|
+
formGroup = container.childNodes[0];
|
|
44
|
+
expect(formGroup.tagName).toEqual('DIV');
|
|
45
|
+
expect(formGroup.classList).toContain('govuk-form-group');
|
|
46
|
+
label = formGroup.childNodes[0];
|
|
47
|
+
expect(label.tagName).toEqual('LABEL');
|
|
48
|
+
expect(label.classList).toContain('govuk-label');
|
|
49
|
+
expect(label.textContent).toEqual("".concat(COMPONENT.label, " (optional)"));
|
|
50
|
+
expect(label.getAttribute('for')).toEqual(ID);
|
|
51
|
+
hint = formGroup.childNodes[1];
|
|
52
|
+
expect(hint.tagName).toEqual('SPAN');
|
|
53
|
+
expect(hint.classList).toContain('govuk-hint');
|
|
54
|
+
expect(hint.textContent).toEqual(COMPONENT.hint);
|
|
55
|
+
input = formGroup.childNodes[2];
|
|
56
|
+
expect(input.tagName).toEqual('INPUT');
|
|
57
|
+
expect(input.classList).toContain('govuk-input');
|
|
58
|
+
expect(input.id).toEqual(ID);
|
|
59
|
+
expect(input.value).toEqual(VALUE);
|
|
60
|
+
|
|
61
|
+
case 22:
|
|
62
|
+
case "end":
|
|
63
|
+
return _context.stop();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}, _callee);
|
|
67
|
+
})));
|
|
68
|
+
it('should render a text component appropriately with wrap turned off', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
69
|
+
var ID, VALUE, COMPONENT, ON_CHANGE, _render2, container, input;
|
|
70
|
+
|
|
71
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
72
|
+
while (1) {
|
|
73
|
+
switch (_context2.prev = _context2.next) {
|
|
74
|
+
case 0:
|
|
75
|
+
ID = 'component';
|
|
76
|
+
VALUE = 'Text value';
|
|
77
|
+
COMPONENT = {
|
|
78
|
+
id: ID,
|
|
79
|
+
fieldId: ID,
|
|
80
|
+
type: 'text',
|
|
81
|
+
label: 'Text component',
|
|
82
|
+
hint: 'Text hint'
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
ON_CHANGE = function ON_CHANGE() {};
|
|
86
|
+
|
|
87
|
+
_render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_FormComponent.default, {
|
|
88
|
+
"data-testid": ID,
|
|
89
|
+
component: COMPONENT,
|
|
90
|
+
wrap: false,
|
|
91
|
+
value: VALUE,
|
|
92
|
+
onChange: ON_CHANGE
|
|
93
|
+
})), container = _render2.container; // With wrap = false, there should be no form group.
|
|
94
|
+
|
|
95
|
+
input = container.childNodes[0];
|
|
96
|
+
expect(input.tagName).toEqual('INPUT');
|
|
97
|
+
expect(input.classList).toContain('govuk-input');
|
|
98
|
+
expect(input.id).toEqual(ID);
|
|
99
|
+
expect(input.value).toEqual(VALUE);
|
|
100
|
+
|
|
101
|
+
case 10:
|
|
102
|
+
case "end":
|
|
103
|
+
return _context2.stop();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}, _callee2);
|
|
107
|
+
})));
|
|
108
|
+
it('should render an html component appropriately', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
|
|
109
|
+
var ID, COMPONENT, _render3, container, p;
|
|
110
|
+
|
|
111
|
+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
112
|
+
while (1) {
|
|
113
|
+
switch (_context3.prev = _context3.next) {
|
|
114
|
+
case 0:
|
|
115
|
+
ID = 'component';
|
|
116
|
+
COMPONENT = {
|
|
117
|
+
type: 'html',
|
|
118
|
+
tagName: 'p',
|
|
119
|
+
content: 'HTML content'
|
|
120
|
+
};
|
|
121
|
+
_render3 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_FormComponent.default, {
|
|
122
|
+
"data-testid": ID,
|
|
123
|
+
component: COMPONENT
|
|
124
|
+
})), container = _render3.container;
|
|
125
|
+
p = container.childNodes[0];
|
|
126
|
+
expect(p.tagName).toEqual('P');
|
|
127
|
+
expect(p.textContent).toEqual(COMPONENT.content);
|
|
128
|
+
|
|
129
|
+
case 6:
|
|
130
|
+
case "end":
|
|
131
|
+
return _context3.stop();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}, _callee3);
|
|
135
|
+
})));
|
|
136
|
+
});
|
|
137
|
+
});
|