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

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 (277) hide show
  1. package/dist/components/CheckYourAnswers/Answer.js +18 -7
  2. package/dist/components/CheckYourAnswers/Answer.test.js +137 -0
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +70 -46
  4. package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +410 -0
  5. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +551 -0
  6. package/dist/components/CollectionPage/CollectionPage.js +15 -10
  7. package/dist/components/CollectionPage/CollectionPage.test.js +382 -0
  8. package/dist/components/FormComponent/Collection.js +91 -48
  9. package/dist/components/FormComponent/Collection.scss +2 -1
  10. package/dist/components/FormComponent/Collection.test.js +807 -0
  11. package/dist/components/FormComponent/Container.js +35 -16
  12. package/dist/components/FormComponent/Container.test.js +370 -0
  13. package/dist/components/FormComponent/FormComponent.js +45 -14
  14. package/dist/components/FormComponent/FormComponent.stories.mdx +184 -0
  15. package/dist/components/FormComponent/FormComponent.test.js +226 -0
  16. package/dist/components/FormComponent/helpers/addLabel.js +5 -4
  17. package/dist/components/FormComponent/helpers/getComponentFieldSet.js +16 -0
  18. package/dist/components/FormComponent/helpers/getComponentFieldSet.test.js +29 -0
  19. package/dist/components/FormComponent/helpers/index.js +8 -1
  20. package/dist/components/FormPage/FormPage.js +69 -30
  21. package/dist/components/FormPage/FormPage.stories.mdx +155 -0
  22. package/dist/components/FormPage/FormPage.test.js +330 -0
  23. package/dist/components/FormRenderer/FormRenderer.js +142 -228
  24. package/dist/components/FormRenderer/FormRenderer.stories.mdx +183 -0
  25. package/dist/components/FormRenderer/FormRenderer.test.js +968 -0
  26. package/dist/components/FormRenderer/handlers/index.js +1 -2
  27. package/dist/components/FormRenderer/handlers/navigate.js +3 -1
  28. package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.js +35 -14
  29. package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.test.js +88 -5
  30. package/dist/components/FormRenderer/helpers/getCYA.js +11 -2
  31. package/dist/components/FormRenderer/helpers/getNextPageId.js +13 -7
  32. package/dist/components/FormRenderer/helpers/getRelevantPages.js +10 -2
  33. package/dist/components/FormRenderer/helpers/getRelevantPages.test.js +1 -1
  34. package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +2 -2
  35. package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +6 -2
  36. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +171 -51
  37. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +307 -24
  38. package/dist/components/FormRenderer/helpers/index.js +1 -2
  39. package/dist/components/FormRenderer/onCYAAction.js +108 -0
  40. package/dist/components/FormRenderer/onCYAAction.test.js +583 -0
  41. package/dist/components/FormRenderer/onPageAction.js +145 -0
  42. package/dist/components/FormRenderer/onPageAction.test.js +678 -0
  43. package/dist/components/FormRenderer/onTaskAction.js +48 -0
  44. package/dist/components/FormRenderer/onTaskAction.test.js +251 -0
  45. package/dist/components/PageActions/ActionButton.js +0 -3
  46. package/dist/components/PageActions/ActionButton.test.js +93 -0
  47. package/dist/components/PageActions/PageActions.js +2 -2
  48. package/dist/components/PageActions/PageActions.stories.mdx +74 -0
  49. package/dist/components/PageActions/PageActions.test.js +132 -0
  50. package/dist/components/SummaryList/GroupAction.js +2 -2
  51. package/dist/components/SummaryList/GroupAction.test.js +80 -0
  52. package/dist/components/SummaryList/RowAction.js +2 -2
  53. package/dist/components/SummaryList/RowAction.test.js +80 -0
  54. package/dist/components/SummaryList/SummaryList.js +29 -12
  55. package/dist/components/SummaryList/SummaryList.scss +19 -0
  56. package/dist/components/SummaryList/SummaryList.stories.mdx +90 -0
  57. package/dist/components/SummaryList/SummaryList.test.js +400 -0
  58. package/dist/components/SummaryList/SummaryListHeadingRow.js +30 -0
  59. package/dist/components/SummaryList/SummaryListRow.js +5 -2
  60. package/dist/components/SummaryList/helpers/getGroupActionAttributes.js +2 -1
  61. package/dist/components/SummaryList/helpers/getRowActionAttributes.js +2 -1
  62. package/dist/components/TaskList/Task.js +3 -3
  63. package/dist/components/TaskList/Task.test.js +167 -0
  64. package/dist/components/TaskList/TaskList.js +76 -23
  65. package/dist/components/TaskList/TaskList.stories.mdx +164 -0
  66. package/dist/components/TaskList/TaskList.test.js +389 -0
  67. package/dist/components/TaskList/TaskState.js +2 -2
  68. package/dist/components/TaskList/TaskState.test.js +86 -0
  69. package/dist/components/index.js +14 -0
  70. package/dist/context/HooksContext/HooksContext.js +72 -8
  71. package/dist/context/HooksContext/HooksContext.test.js +44 -0
  72. package/dist/context/ValidationContext/ValidationContext.js +38 -28
  73. package/dist/context/ValidationContext/ValidationContext.test.js +84 -0
  74. package/dist/hooks/useAxios.js +10 -12
  75. package/dist/hooks/useGetRequest.js +48 -49
  76. package/dist/hooks/useRefData.js +7 -4
  77. package/dist/index.js +34 -1
  78. package/dist/json/addressDetails.json +149 -0
  79. package/dist/json/areYouACivilServant.json +7 -0
  80. package/dist/json/firstForm.json +94 -0
  81. package/dist/json/grade.json +108 -0
  82. package/dist/json/group.data.json +21 -0
  83. package/dist/json/group.json +402 -0
  84. package/dist/json/groupOfRow.json +137 -0
  85. package/dist/json/groupOfRowData.json +15 -0
  86. package/dist/json/port.json +346 -0
  87. package/dist/json/saveAndContinue.json +98 -0
  88. package/dist/json/sublocation.json +859 -0
  89. package/dist/json/taskList.json +265 -0
  90. package/dist/json/team.json +17351 -0
  91. package/dist/json/terminal.json +81 -0
  92. package/dist/json/userProfile.data.json +21 -0
  93. package/dist/json/userProfile.json +276 -0
  94. package/dist/models/ComponentTypes.js +8 -0
  95. package/dist/models/PageAction.js +4 -1
  96. package/dist/models/TaskStates.js +10 -2
  97. package/dist/models/index.js +16 -1
  98. package/dist/setupTests.js +46 -0
  99. package/dist/utils/CheckYourAnswers/getCYAAction.js +5 -5
  100. package/dist/utils/CheckYourAnswers/getCYAAction.test.js +121 -0
  101. package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.js +6 -2
  102. package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.test.js +71 -0
  103. package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.js +6 -2
  104. package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.test.js +55 -0
  105. package/dist/utils/CheckYourAnswers/getCYARow.js +14 -11
  106. package/dist/utils/CheckYourAnswers/getCYARow.test.js +291 -0
  107. package/dist/utils/CheckYourAnswers/getCYARowForGroup.js +4 -1
  108. package/dist/utils/CheckYourAnswers/getCYARowForGroup.test.js +70 -0
  109. package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +15 -10
  110. package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +288 -0
  111. package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.js +94 -36
  112. package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.test.js +414 -0
  113. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +21 -7
  114. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +486 -0
  115. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -3
  116. package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +346 -0
  117. package/dist/utils/CheckYourAnswers/showComponentCYA.js +19 -3
  118. package/dist/utils/CheckYourAnswers/showComponentCYA.test.js +143 -0
  119. package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.js +5 -1
  120. package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.test.js +40 -0
  121. package/dist/utils/CollectionPage/getCollectionPageActiveIndex.test.js +53 -0
  122. package/dist/utils/CollectionPage/mergeCollectionPages.js +25 -17
  123. package/dist/utils/CollectionPage/mergeCollectionPages.test.js +130 -0
  124. package/dist/utils/Component/addShowWhen.js +44 -0
  125. package/dist/utils/Component/addShowWhen.test.js +216 -0
  126. package/dist/utils/Component/applyToComponentTree.js +71 -0
  127. package/dist/utils/Component/applyToComponentTree.test.js +127 -0
  128. package/dist/utils/Component/cleanAttributes.js +8 -2
  129. package/dist/utils/Component/cleanAttributes.test.js +68 -0
  130. package/dist/utils/Component/elevateNestedComponents.js +4 -1
  131. package/dist/utils/Component/elevateNestedComponents.test.js +122 -0
  132. package/dist/utils/Component/getComponent.js +80 -11
  133. package/dist/utils/Component/getComponentTests/getComponent.autocomplete.test.js +4 -4
  134. package/dist/utils/Component/getComponentTests/getComponent.calculation.test.js +25 -24
  135. package/dist/utils/Component/getComponentTests/getComponent.checkboxes.test.js +66 -5
  136. package/dist/utils/Component/getComponentTests/getComponent.date.test.js +2 -2
  137. package/dist/utils/Component/getComponentTests/getComponent.details.test.js +9 -7
  138. package/dist/utils/Component/getComponentTests/getComponent.email.test.js +3 -3
  139. package/dist/utils/Component/getComponentTests/getComponent.file.test.js +3 -3
  140. package/dist/utils/Component/getComponentTests/getComponent.list.test.js +44 -0
  141. package/dist/utils/Component/getComponentTests/{getComponent.textarea.test.js → getComponent.multifile.test.js} +29 -21
  142. package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +159 -83
  143. package/dist/utils/Component/getComponentTests/getComponent.paragraph.test.js +43 -0
  144. package/dist/utils/Component/getComponentTests/getComponent.phoneNumber.test.js +3 -3
  145. package/dist/utils/Component/getComponentTests/getComponent.radios.test.js +55 -5
  146. package/dist/utils/Component/getComponentTests/getComponent.select.test.js +3 -9
  147. package/dist/utils/Component/getComponentTests/getComponent.text.test.js +3 -3
  148. package/dist/utils/Component/getComponentTests/getComponent.textArea.test.js +109 -0
  149. package/dist/utils/Component/getComponentTests/getComponent.time.test.js +7 -7
  150. package/dist/utils/Component/getDefaultValue.js +4 -1
  151. package/dist/utils/Component/getDefaultValue.test.js +50 -0
  152. package/dist/utils/Component/index.js +2 -0
  153. package/dist/utils/Component/isEditable.js +2 -2
  154. package/dist/utils/Component/isEditable.test.js +43 -0
  155. package/dist/utils/Component/optionIsSelected.js +22 -0
  156. package/dist/utils/Component/optionIsSelected.test.js +42 -0
  157. package/dist/utils/Component/setupContainerComponentsPath.js +40 -0
  158. package/dist/utils/Component/setupContainerComponentsPath.test.js +78 -0
  159. package/dist/utils/Component/showComponent.test.js +156 -0
  160. package/dist/utils/Component/wrapInFormGroup.js +1 -1
  161. package/dist/utils/Condition/meetsAllConditions.js +1 -1
  162. package/dist/utils/Condition/meetsAllConditions.test.js +61 -0
  163. package/dist/utils/Condition/meetsCondition.js +74 -4
  164. package/dist/utils/Condition/meetsCondition.test.js +763 -0
  165. package/dist/utils/Condition/meetsOneCondition.js +1 -1
  166. package/dist/utils/Condition/meetsOneCondition.test.js +100 -0
  167. package/dist/utils/Condition/setupConditions.js +6 -3
  168. package/dist/utils/Condition/setupConditions.test.js +33 -0
  169. package/dist/utils/Container/getEditableComponents.test.js +135 -0
  170. package/dist/utils/Container/setupNesting.js +21 -2
  171. package/dist/utils/Container/setupNesting.test.js +145 -0
  172. package/dist/utils/Container/showContainer.test.js +178 -0
  173. package/dist/utils/Data/applyFormula.js +20 -11
  174. package/dist/utils/Data/applyFormula.test.js +263 -0
  175. package/dist/utils/Data/getAutocompleteSource.js +6 -2
  176. package/dist/utils/Data/getAutocompleteSource.test.js +352 -0
  177. package/dist/utils/Data/getDataPath.test.js +48 -0
  178. package/dist/utils/Data/getOptions.js +17 -3
  179. package/dist/utils/Data/getOptions.test.js +140 -0
  180. package/dist/utils/Data/getSourceData.js +2 -2
  181. package/dist/utils/Data/getSourceData.test.js +153 -0
  182. package/dist/utils/Data/refDataToOptions.js +4 -1
  183. package/dist/utils/Data/refDataToOptions.test.js +196 -0
  184. package/dist/utils/Data/setDataItem.test.js +110 -0
  185. package/dist/utils/Data/setupFormData.js +39 -16
  186. package/dist/utils/Data/setupFormData.test.js +410 -0
  187. package/dist/utils/Data/setupRefDataUrlForComponent.js +4 -1
  188. package/dist/utils/Data/setupRefDataUrlForComponent.test.js +131 -0
  189. package/dist/utils/FormPage/applyConditionalProperties.js +35 -0
  190. package/dist/utils/FormPage/applyConditionalProperties.test.js +61 -0
  191. package/dist/utils/FormPage/getFormPage.js +5 -2
  192. package/dist/utils/FormPage/getFormPage.test.js +206 -0
  193. package/dist/utils/FormPage/getFormPages.js +4 -1
  194. package/dist/utils/FormPage/getFormPages.test.js +97 -0
  195. package/dist/utils/FormPage/getPageActions.js +8 -4
  196. package/dist/utils/FormPage/getPageActions.test.js +114 -0
  197. package/dist/utils/FormPage/getParagraphFromText.test.js +27 -0
  198. package/dist/utils/FormPage/index.js +2 -0
  199. package/dist/utils/FormPage/showFormPage.js +2 -2
  200. package/dist/utils/FormPage/showFormPage.test.js +180 -0
  201. package/dist/utils/FormPage/showFormPageCYA.js +1 -0
  202. package/dist/utils/FormPage/showFormPageCYA.test.js +28 -0
  203. package/dist/utils/FormPage/useComponent.js +29 -14
  204. package/dist/utils/FormPage/useComponent.test.js +167 -0
  205. package/dist/utils/Format/formatData.test.js +45 -0
  206. package/dist/utils/Format/formatDataForComponent.js +2 -1
  207. package/dist/utils/Format/formatDataForComponent.test.js +142 -0
  208. package/dist/utils/Format/formatDataForForm.js +4 -1
  209. package/dist/utils/Format/formatDataForForm.test.js +81 -0
  210. package/dist/utils/Format/formatDataForPage.test.js +99 -0
  211. package/dist/utils/Hub/getFormHub.test.js +105 -0
  212. package/dist/utils/Meta/documents/getDocuments.test.js +4 -1
  213. package/dist/utils/Meta/documents/index.js +2 -2
  214. package/dist/utils/Meta/documents/setDocumentsForField.js +31 -0
  215. package/dist/utils/Meta/documents/setDocumentsForField.test.js +59 -0
  216. package/dist/utils/Operate/checkValueIsTruthy.test.js +42 -0
  217. package/dist/utils/Operate/getFirstOf.js +31 -0
  218. package/dist/utils/Operate/getFirstOf.test.js +86 -0
  219. package/dist/utils/Operate/getIndexOfMatchingValueIn.test.js +163 -0
  220. package/dist/utils/Operate/persistValueInFormData.js +2 -1
  221. package/dist/utils/Operate/persistValueInFormData.test.js +115 -0
  222. package/dist/utils/Operate/runPageOperations.js +3 -1
  223. package/dist/utils/Operate/runPageOperations.test.js +105 -0
  224. package/dist/utils/Operate/setValueInFormData.test.js +44 -0
  225. package/dist/utils/Operate/shouldRun.js +16 -13
  226. package/dist/utils/Operate/shouldRun.test.js +66 -0
  227. package/dist/utils/Validate/additional/conditionallyRequired.js +24 -0
  228. package/dist/utils/Validate/additional/conditionallyRequired.test.js +73 -0
  229. package/dist/utils/Validate/additional/index.js +13 -4
  230. package/dist/utils/Validate/additional/mustBeAfter.js +8 -6
  231. package/dist/utils/Validate/additional/mustBeAfter.test.js +15 -1
  232. package/dist/utils/Validate/additional/mustBeBefore.js +9 -5
  233. package/dist/utils/Validate/additional/mustBeBefore.test.js +14 -0
  234. package/dist/utils/Validate/additional/mustBeEarlierDateTime.js +1 -1
  235. package/dist/utils/Validate/additional/mustBeEarlierDateTime.test.js +6 -3
  236. package/dist/utils/Validate/additional/mustBeGreaterThan.js +22 -0
  237. package/dist/utils/Validate/additional/mustBeGreaterThan.test.js +56 -0
  238. package/dist/utils/Validate/additional/mustBeInTheFuture.js +1 -1
  239. package/dist/utils/Validate/additional/mustBeInThePast.js +5 -5
  240. package/dist/utils/Validate/additional/mustBeLessThan.js +7 -6
  241. package/dist/utils/Validate/additional/mustBeLessThan.test.js +11 -1
  242. package/dist/utils/Validate/additional/mustBeNumbersOnly.js +9 -5
  243. package/dist/utils/Validate/additional/mustBeNumbersOnly.test.js +10 -2
  244. package/dist/utils/Validate/additional/mustEnterAtLeastOne.js +1 -0
  245. package/dist/utils/Validate/additional/mustEnterAtLeastOne.test.js +4 -1
  246. package/dist/utils/Validate/additional/mustHaveLessThanDecimalPlaces.js +19 -0
  247. package/dist/utils/Validate/additional/mustHaveLessThanDecimalPlaces.test.js +36 -0
  248. package/dist/utils/Validate/additional/mustSelectOnlyOne.js +29 -0
  249. package/dist/utils/Validate/additional/mustSelectOnlyOne.test.js +52 -0
  250. package/dist/utils/Validate/additional/utils.js +18 -18
  251. package/dist/utils/Validate/index.js +6 -0
  252. package/dist/utils/Validate/validateCollection.js +20 -5
  253. package/dist/utils/Validate/validateCollection.test.js +125 -0
  254. package/dist/utils/Validate/validateComponent.js +36 -20
  255. package/dist/utils/Validate/validateComponent.test.js +365 -0
  256. package/dist/utils/Validate/validateContainer.js +9 -3
  257. package/dist/utils/Validate/validateContainer.test.js +75 -0
  258. package/dist/utils/Validate/validateDate.js +4 -3
  259. package/dist/utils/Validate/validateDate.test.js +114 -0
  260. package/dist/utils/Validate/validateEmail.js +14 -1
  261. package/dist/utils/Validate/validateEmail.test.js +74 -0
  262. package/dist/utils/Validate/validateMultifile.js +32 -0
  263. package/dist/utils/Validate/validateMultifile.test.js +47 -0
  264. package/dist/utils/Validate/validatePage.js +12 -6
  265. package/dist/utils/Validate/validatePage.test.js +472 -0
  266. package/dist/utils/Validate/validateRegex.js +2 -5
  267. package/dist/utils/Validate/validateRegex.test.js +42 -0
  268. package/dist/utils/Validate/validateRequired.js +2 -4
  269. package/dist/utils/Validate/validateRequired.test.js +63 -0
  270. package/dist/utils/Validate/validateTextArea.js +37 -0
  271. package/dist/utils/Validate/validateTextArea.test.js +42 -0
  272. package/dist/utils/Validate/validateTime.js +2 -2
  273. package/dist/utils/Validate/validateTime.test.js +59 -0
  274. package/dist/utils/index.js +4 -1
  275. package/package.json +35 -50
  276. package/dist/utils/Meta/documents/setDocumentForField.js +0 -29
  277. package/dist/utils/Meta/documents/setDocumentForField.test.js +0 -49
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ var _validateEmail = _interopRequireDefault(require("./validateEmail"));
4
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
+ // Local imports
6
+
7
+ describe('utils', function () {
8
+ describe('Validate', function () {
9
+ describe('email', function () {
10
+ var LABEL = 'Component';
11
+ var ERROR = "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk");
12
+
13
+ // Valid values
14
+ it('should return no error when the value is a valid .gov.uk address', function () {
15
+ expect((0, _validateEmail.default)('alpha@homeoffice.gov.uk', LABEL)).toBeUndefined();
16
+ });
17
+ it('should return no error when the value is a capitalised digital.homeoffice.gov.uk address', function () {
18
+ expect((0, _validateEmail.default)('ALPHA.BRAVO@DIGITAL.HOMEOFFICE.GOV.UK', LABEL)).toBeUndefined();
19
+ });
20
+ it('should return no error when the value is an empty string', function () {
21
+ expect((0, _validateEmail.default)('', LABEL)).toBeUndefined();
22
+ });
23
+
24
+ // Invalid values
25
+ it('should return an error when the value is an empty object', function () {
26
+ expect((0, _validateEmail.default)({}, LABEL)).toEqual(ERROR);
27
+ });
28
+ it('should return an error when the value is an array', function () {
29
+ expect((0, _validateEmail.default)(['bob'], LABEL)).toEqual(ERROR);
30
+ });
31
+ it('should return an error when the value is numeric', function () {
32
+ expect((0, _validateEmail.default)(24, LABEL)).toEqual(ERROR);
33
+ });
34
+ it('should return an error when the value is in the wrong domain', function () {
35
+ expect((0, _validateEmail.default)('alpha@domain.com', LABEL)).toEqual(ERROR);
36
+ });
37
+ it('should return an error when the domain contains spaces', function () {
38
+ expect((0, _validateEmail.default)('alpha.bravo@digital homeoffice.gov.uk', LABEL)).toEqual(ERROR);
39
+ });
40
+ it('should return an error when the value has no TLD', function () {
41
+ expect((0, _validateEmail.default)('alpha.bravo@homeoffice', LABEL)).toEqual(ERROR);
42
+ });
43
+ it('should return an error when there is no name', function () {
44
+ expect((0, _validateEmail.default)('@homeoffice.gov.uk', LABEL)).toEqual(ERROR);
45
+ });
46
+ it('should return an error when there is no @ symbol', function () {
47
+ expect((0, _validateEmail.default)('alpha.bravo.homeoffice.gov.uk', LABEL)).toEqual(ERROR);
48
+ });
49
+ it('should return an error when the name contains spaces', function () {
50
+ expect((0, _validateEmail.default)('alpha bravo@digital.homeoffice.gov.uk', LABEL)).toEqual(ERROR);
51
+ });
52
+ it('should use a default label when none is specified', function () {
53
+ var DEFAULT_ERROR = 'Enter email address in the correct format, like jane.doe@homeoffice.gov.uk';
54
+ expect((0, _validateEmail.default)(['bob'], undefined)).toEqual(DEFAULT_ERROR);
55
+ });
56
+ it('should use a custom format error message when specified', function () {
57
+ var customErrors = [{
58
+ 'type': 'format',
59
+ 'message': 'Enter email address in the correct format'
60
+ }];
61
+ var CUSTOM_ERROR = 'Enter email address in the correct format';
62
+ expect((0, _validateEmail.default)('bob', '', customErrors)).toEqual(CUSTOM_ERROR);
63
+ });
64
+ it('should use the default format error message when custom errors is not an array', function () {
65
+ var DEFAULT_ERROR = 'Enter email address in the correct format, like jane.doe@homeoffice.gov.uk';
66
+ var customErrors = {
67
+ 'type': 'format',
68
+ 'message': 'Enter email address in the correct format'
69
+ };
70
+ expect((0, _validateEmail.default)('bob', '', customErrors)).toEqual(DEFAULT_ERROR);
71
+ });
72
+ });
73
+ });
74
+ });
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.DEFAULT_ERROR = void 0;
7
+ var DEFAULT_ERROR = 'Invalid files must be deleted';
8
+ exports.DEFAULT_ERROR = DEFAULT_ERROR;
9
+ var validateMultifile = function validateMultifile(value, customErrors) {
10
+ if (!value) {
11
+ return undefined;
12
+ }
13
+ var inError = false;
14
+ value.every(function (entry) {
15
+ inError = entry.error;
16
+ return !inError;
17
+ });
18
+ if (inError) {
19
+ if (Array.isArray(customErrors)) {
20
+ var result = customErrors.filter(function (error) {
21
+ return error.type === 'invalidFile';
22
+ });
23
+ if (result && result.length > 0 && result[0].message) {
24
+ return result[0].message;
25
+ }
26
+ }
27
+ return DEFAULT_ERROR;
28
+ }
29
+ return undefined;
30
+ };
31
+ var _default = validateMultifile;
32
+ exports.default = _default;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+ var _validateMultifile = _interopRequireWildcard(require("./validateMultifile"));
5
+ 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); }
6
+ 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; }
7
+ // Local imports
8
+
9
+ describe('utils', function () {
10
+ describe('Validate', function () {
11
+ describe('multifile', function () {
12
+ var ERROR_MSG = 'This is a custom error message';
13
+ var CUSTOM_ERRORS = [{
14
+ type: 'invalidFile',
15
+ message: ERROR_MSG
16
+ }];
17
+
18
+ // Valid values.
19
+ it('should return no error when value is nullish', function () {
20
+ var VALUE = null;
21
+ expect((0, _validateMultifile.default)(VALUE, [])).toBeUndefined();
22
+ });
23
+ it('should return no error when value is an empty array', function () {
24
+ var VALUE = [];
25
+ expect((0, _validateMultifile.default)(VALUE, [])).toBeUndefined();
26
+ });
27
+ it('should return no error when value only contains valid files', function () {
28
+ var VALUE = [{}, {}];
29
+ expect((0, _validateMultifile.default)(VALUE, [])).toBeUndefined();
30
+ });
31
+
32
+ // Invalid values.
33
+ it('should return an error when valid contains invalid files', function () {
34
+ var VALUE = [{
35
+ error: 'This is an invalid file'
36
+ }];
37
+ expect((0, _validateMultifile.default)(VALUE, [])).toEqual(_validateMultifile.DEFAULT_ERROR);
38
+ });
39
+ it('should return a custom error when one is provided', function () {
40
+ var VALUE = [{
41
+ error: 'This is an invalid file'
42
+ }];
43
+ expect((0, _validateMultifile.default)(VALUE, CUSTOM_ERRORS)).toEqual(ERROR_MSG);
44
+ });
45
+ });
46
+ });
47
+ });
@@ -9,9 +9,12 @@ var _validateComponent = _interopRequireDefault(require("./validateComponent"));
9
9
  var _CollectionPage = _interopRequireDefault(require("../CollectionPage"));
10
10
  var _showFormPage = _interopRequireDefault(require("../FormPage/showFormPage"));
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
12
13
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
14
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
- 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; }
15
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
16
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
17
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
15
18
  /**
16
19
  * Validate all of the components on a page.
17
20
  * @param {object} page The page to validate
@@ -23,16 +26,19 @@ var validatePage = function validatePage(page) {
23
26
  var activeIndex = _CollectionPage.default.getActiveIndex(page.collection.name, page.formData);
24
27
  if (activeIndex !== null) {
25
28
  var _page$formData$page$c;
26
- data = (_page$formData$page$c = page.formData[page.collection.name]) === null || _page$formData$page$c === void 0 ? void 0 : _page$formData$page$c[activeIndex];
29
+ 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]);
27
30
  }
28
31
  }
29
- if ((0, _showFormPage.default)(page, page.formData) && Array.isArray(page.components)) {
32
+ if ((0, _showFormPage.default)(page, data) && Array.isArray(page.components)) {
30
33
  var errs = page.components.reduce(function (errors, component) {
31
34
  var componentErrors = (0, _validateComponent.default)(component, data, data);
32
35
  return errors.concat(componentErrors).flat().map(function (err) {
33
- return !!err ? _objectSpread(_objectSpread({}, err), {}, {
34
- error: _copReactComponents.Utils.interpolateString(err.error, data)
35
- }) : err;
36
+ if (err) {
37
+ return _objectSpread(_objectSpread({}, err), {}, {
38
+ error: _copReactComponents.Utils.interpolateString(err.error, data)
39
+ });
40
+ }
41
+ return err;
36
42
  });
37
43
  }, []).filter(function (e) {
38
44
  return !!e;
@@ -0,0 +1,472 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+ var _validatePage = _interopRequireDefault(require("./validatePage"));
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
7
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
8
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
12
+ describe('utils.Validate.Page', function () {
13
+ var setup = function setup(id, type, label, required) {
14
+ return {
15
+ id: id,
16
+ fieldId: id,
17
+ type: type,
18
+ label: label,
19
+ required: required
20
+ };
21
+ };
22
+ describe('with FormPage', function () {
23
+ it('should return no error when the components array is null', function () {
24
+ var PAGE = {
25
+ components: null,
26
+ formData: null
27
+ };
28
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
29
+ });
30
+ it('should return no error when the components array is empty', function () {
31
+ var PAGE = {
32
+ components: [],
33
+ formData: null
34
+ };
35
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
36
+ });
37
+ describe('when there is no form data', function () {
38
+ it('should return no errors when no components are required', function () {
39
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', false), setup('b', _models.ComponentTypes.TEXT, 'Bravo', false)];
40
+ var PAGE = {
41
+ components: COMPONENTS,
42
+ formData: null
43
+ };
44
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
45
+ });
46
+ it('should return an error for each required component', function () {
47
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
48
+ // The only unrequired one
49
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
50
+ var PAGE = {
51
+ components: COMPONENTS,
52
+ formData: null
53
+ };
54
+ var RESULT = (0, _validatePage.default)(PAGE);
55
+ expect(RESULT.length).toEqual(4);
56
+ expect(RESULT[0]).toEqual({
57
+ id: 'a',
58
+ error: 'Alpha is required'
59
+ });
60
+ expect(RESULT[1]).toEqual({
61
+ id: 'b',
62
+ error: 'Bravo is required'
63
+ });
64
+ expect(RESULT[2]).toEqual({
65
+ id: 'd',
66
+ error: 'Delta is required'
67
+ });
68
+ expect(RESULT[3]).toEqual({
69
+ id: 'e',
70
+ error: 'Echo is required'
71
+ });
72
+ });
73
+ it('should return an error for each required component on a collection page', function () {
74
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
75
+ // The only unrequired one
76
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
77
+ var PAGE = {
78
+ components: COMPONENTS,
79
+ collection: {
80
+ name: 'testCollection'
81
+ },
82
+ formData: null
83
+ };
84
+ var RESULT = (0, _validatePage.default)(PAGE);
85
+ expect(RESULT.length).toEqual(4);
86
+ expect(RESULT[0]).toEqual({
87
+ id: 'a',
88
+ error: 'Alpha is required'
89
+ });
90
+ expect(RESULT[1]).toEqual({
91
+ id: 'b',
92
+ error: 'Bravo is required'
93
+ });
94
+ expect(RESULT[2]).toEqual({
95
+ id: 'd',
96
+ error: 'Delta is required'
97
+ });
98
+ expect(RESULT[3]).toEqual({
99
+ id: 'e',
100
+ error: 'Echo is required'
101
+ });
102
+ });
103
+ it('should return an error for each required component with interpolated label', function () {
104
+ var COMPONENTS = [
105
+ // eslint-disable-next-line no-template-curly-in-string
106
+ setup('a', _models.ComponentTypes.TEXT, 'Alpha ${tiger}', true),
107
+ // eslint-disable-next-line no-template-curly-in-string
108
+ setup('b', _models.ComponentTypes.EMAIL, 'Bravo ${panther}', true),
109
+ // eslint-disable-next-line no-template-curly-in-string
110
+ setup('c', _models.ComponentTypes.AUTOCOMPLETE, 'Charlie ${eagle}', true),
111
+ // eslint-disable-next-line no-template-curly-in-string
112
+ setup('d', _models.ComponentTypes.CHECKBOXES, 'Delta ${lion}', true),
113
+ // eslint-disable-next-line no-template-curly-in-string
114
+ setup('e', _models.ComponentTypes.FILE, 'Echo ${zoo}', true)];
115
+ var PAGE = {
116
+ components: COMPONENTS,
117
+ formData: {
118
+ tiger: 'Tiger',
119
+ panther: 'Panther',
120
+ eagle: 'Eagle',
121
+ lion: 'Lion',
122
+ zoo: 'Zoo'
123
+ }
124
+ };
125
+ var RESULT = (0, _validatePage.default)(PAGE);
126
+ expect(RESULT.length).toEqual(5);
127
+ expect(RESULT[0]).toEqual({
128
+ id: 'a',
129
+ error: "Alpha ".concat(PAGE.formData.tiger, " is required")
130
+ });
131
+ expect(RESULT[1]).toEqual({
132
+ id: 'b',
133
+ error: "Bravo ".concat(PAGE.formData.panther, " is required")
134
+ });
135
+ expect(RESULT[2]).toEqual({
136
+ id: 'c',
137
+ error: "Charlie ".concat(PAGE.formData.eagle, " is required")
138
+ });
139
+ expect(RESULT[3]).toEqual({
140
+ id: 'd',
141
+ error: "Delta ".concat(PAGE.formData.lion, " is required")
142
+ });
143
+ expect(RESULT[4]).toEqual({
144
+ id: 'e',
145
+ error: "Echo ".concat(PAGE.formData.zoo, " is required")
146
+ });
147
+ });
148
+ });
149
+ describe('when the form data is fully valid', function () {
150
+ var DATA = {
151
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
152
+ bravo: 'bravo.jones@digital.homeoffice.gov.uk'
153
+ };
154
+ it('should return no errors when none of the components are required or email types', function () {
155
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false)];
156
+ var PAGE = {
157
+ components: COMPONENTS,
158
+ formData: DATA
159
+ };
160
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
161
+ });
162
+ it('should return no errors when all of the components are required but not email types', function () {
163
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true)];
164
+ var PAGE = {
165
+ components: COMPONENTS,
166
+ formData: DATA
167
+ };
168
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
169
+ });
170
+ it('should return no errors when none of the components are required but are all email types', function () {
171
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false)];
172
+ var PAGE = {
173
+ components: COMPONENTS,
174
+ formData: DATA
175
+ };
176
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
177
+ });
178
+ it('should return no errors when all of the components are required and email types', function () {
179
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true)];
180
+ var PAGE = {
181
+ components: COMPONENTS,
182
+ formData: DATA
183
+ };
184
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
185
+ });
186
+ });
187
+ describe('when the form data has one field missing and includes an invalid email', function () {
188
+ var DATA = {
189
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
190
+ bravo: 'bravo.jones@hotmail.com'
191
+ };
192
+ it('should return no errors when none of the components are required or email types', function () {
193
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', false)];
194
+ var PAGE = {
195
+ components: COMPONENTS,
196
+ formData: null
197
+ };
198
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
199
+ });
200
+ it('should return an error for the missing field when all are required but not email types', function () {
201
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', true)];
202
+ var PAGE = {
203
+ components: COMPONENTS,
204
+ formData: DATA
205
+ };
206
+ var RESULT = (0, _validatePage.default)(PAGE);
207
+ expect(RESULT.length).toEqual(1);
208
+ expect(RESULT[0]).toEqual({
209
+ id: 'charlie',
210
+ error: 'Charlie is required'
211
+ });
212
+ });
213
+ it('should return an error for the invalid email field when none are required but all email types', function () {
214
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', false)];
215
+ var PAGE = {
216
+ components: COMPONENTS,
217
+ formData: DATA
218
+ };
219
+ var RESULT = (0, _validatePage.default)(PAGE);
220
+ expect(RESULT.length).toEqual(1);
221
+ expect(RESULT[0]).toEqual({
222
+ id: 'bravo',
223
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
224
+ });
225
+ });
226
+ it('should return an error for both invalid fields when all are required and email types', function () {
227
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', true)];
228
+ var PAGE = {
229
+ components: COMPONENTS,
230
+ formData: DATA
231
+ };
232
+ var RESULT = (0, _validatePage.default)(PAGE);
233
+ expect(RESULT.length).toEqual(2);
234
+ expect(RESULT[0]).toEqual({
235
+ id: 'bravo',
236
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
237
+ });
238
+ expect(RESULT[1]).toEqual({
239
+ id: 'charlie',
240
+ error: 'Charlie is required'
241
+ });
242
+ });
243
+ it('should return an interpolated error for both invalid fields when all are required and email types', function () {
244
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true),
245
+ // eslint-disable-next-line no-template-curly-in-string
246
+ setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo ${lion}', true),
247
+ // eslint-disable-next-line no-template-curly-in-string
248
+ setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie ${panther}', true)];
249
+ var PAGE = {
250
+ components: COMPONENTS,
251
+ formData: _objectSpread(_objectSpread({}, DATA), {}, {
252
+ lion: 'Lion',
253
+ panther: 'Panther'
254
+ })
255
+ };
256
+ var RESULT = (0, _validatePage.default)(PAGE);
257
+ expect(RESULT.length).toEqual(2);
258
+ expect(RESULT[0]).toEqual({
259
+ id: 'bravo',
260
+ error: "Enter bravo ".concat(PAGE.formData.lion, " in the correct format, like jane.doe@homeoffice.gov.uk")
261
+ });
262
+ expect(RESULT[1]).toEqual({
263
+ id: 'charlie',
264
+ error: "Charlie ".concat(PAGE.formData.panther, " is required")
265
+ });
266
+ });
267
+ });
268
+ });
269
+ describe('with CollectionPage', function () {
270
+ var COLLECTION = {
271
+ name: 'pageCollection'
272
+ };
273
+ describe('when the form data is fully valid', function () {
274
+ var _DATA;
275
+ var DATA = (_DATA = {}, _defineProperty(_DATA, "".concat(COLLECTION.name, "ActiveId"), '01'), _defineProperty(_DATA, COLLECTION.name, [{
276
+ id: '01',
277
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
278
+ bravo: 'bravo.jones@digital.homeoffice.gov.uk'
279
+ }]), _DATA);
280
+ it('should return no errors when none of the components are required or email types', function () {
281
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false)];
282
+ var PAGE = {
283
+ collection: COLLECTION,
284
+ components: COMPONENTS,
285
+ formData: DATA
286
+ };
287
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
288
+ });
289
+ it('should return no errors when all of the components are required but not email types', function () {
290
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true)];
291
+ var PAGE = {
292
+ collection: COLLECTION,
293
+ components: COMPONENTS,
294
+ formData: DATA
295
+ };
296
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
297
+ });
298
+ it('should return no errors when none of the components are required but are all email types', function () {
299
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false)];
300
+ var PAGE = {
301
+ collection: COLLECTION,
302
+ components: COMPONENTS,
303
+ formData: DATA
304
+ };
305
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
306
+ });
307
+ it('should return no errors when all of the components are required and email types', function () {
308
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true)];
309
+ var PAGE = {
310
+ collection: COLLECTION,
311
+ components: COMPONENTS,
312
+ formData: DATA
313
+ };
314
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
315
+ });
316
+ it('should return an error for each required component on a collection page with a show_when that is true', function () {
317
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
318
+ // The only unrequired one
319
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
320
+ var PAGE = {
321
+ components: COMPONENTS,
322
+ collection: {
323
+ name: 'testCollection'
324
+ },
325
+ show_when: {
326
+ field: 'isShown',
327
+ op: '=',
328
+ value: true
329
+ },
330
+ formData: {
331
+ testCollectionActiveId: 123,
332
+ testCollection: [{
333
+ id: 123,
334
+ isShown: true
335
+ }]
336
+ }
337
+ };
338
+ var RESULT = (0, _validatePage.default)(PAGE);
339
+ expect(RESULT.length).toEqual(4);
340
+ expect(RESULT[0]).toEqual({
341
+ id: 'a',
342
+ error: 'Alpha is required'
343
+ });
344
+ expect(RESULT[1]).toEqual({
345
+ id: 'b',
346
+ error: 'Bravo is required'
347
+ });
348
+ expect(RESULT[2]).toEqual({
349
+ id: 'd',
350
+ error: 'Delta is required'
351
+ });
352
+ expect(RESULT[3]).toEqual({
353
+ id: 'e',
354
+ error: 'Echo is required'
355
+ });
356
+ });
357
+ it('should return no errors on a collection page with a show_when that is false', function () {
358
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false),
359
+ // The only unrequired one
360
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
361
+ var PAGE = {
362
+ components: COMPONENTS,
363
+ collection: {
364
+ name: 'testCollection'
365
+ },
366
+ show_when: {
367
+ field: 'isShown',
368
+ op: '=',
369
+ value: true
370
+ },
371
+ formData: {
372
+ testCollectionActiveId: 123,
373
+ testCollection: [{
374
+ id: 123,
375
+ isShown: false
376
+ }]
377
+ }
378
+ };
379
+ var RESULT = (0, _validatePage.default)(PAGE);
380
+ expect(RESULT.length).toEqual(0);
381
+ });
382
+ });
383
+ describe('when the form data has one field missing and includes an invalid email', function () {
384
+ var _DATA2;
385
+ var DATA = (_DATA2 = {}, _defineProperty(_DATA2, "".concat(COLLECTION.name, "ActiveId"), '01'), _defineProperty(_DATA2, COLLECTION.name, [{
386
+ id: '01',
387
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
388
+ bravo: 'bravo.jones@hotmail.com'
389
+ }]), _DATA2);
390
+ it('should return no errors when none of the components are required or email types', function () {
391
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', false)];
392
+ var PAGE = {
393
+ collection: COLLECTION,
394
+ components: COMPONENTS,
395
+ formData: null
396
+ };
397
+ expect((0, _validatePage.default)(PAGE).length).toEqual(0);
398
+ });
399
+ it('should return an error for the missing field when all are required but not email types', function () {
400
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', true)];
401
+ var PAGE = {
402
+ collection: COLLECTION,
403
+ components: COMPONENTS,
404
+ formData: DATA
405
+ };
406
+ var RESULT = (0, _validatePage.default)(PAGE);
407
+ expect(RESULT.length).toEqual(1);
408
+ expect(RESULT[0]).toEqual({
409
+ id: 'charlie',
410
+ error: 'Charlie is required'
411
+ });
412
+ });
413
+ it('should return an error for the invalid email field when none are required but all email types', function () {
414
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', false)];
415
+ var PAGE = {
416
+ collection: COLLECTION,
417
+ components: COMPONENTS,
418
+ formData: DATA
419
+ };
420
+ var RESULT = (0, _validatePage.default)(PAGE);
421
+ expect(RESULT.length).toEqual(1);
422
+ expect(RESULT[0]).toEqual({
423
+ id: 'bravo',
424
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
425
+ });
426
+ });
427
+ it('should return an error for both invalid fields when all are required and email types', function () {
428
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', true)];
429
+ var PAGE = {
430
+ collection: COLLECTION,
431
+ components: COMPONENTS,
432
+ formData: DATA
433
+ };
434
+ var RESULT = (0, _validatePage.default)(PAGE);
435
+ expect(RESULT.length).toEqual(2);
436
+ expect(RESULT[0]).toEqual({
437
+ id: 'bravo',
438
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
439
+ });
440
+ expect(RESULT[1]).toEqual({
441
+ id: 'charlie',
442
+ error: 'Charlie is required'
443
+ });
444
+ });
445
+ it('should return an interpolated error for both invalid fields when all are required and email types', function () {
446
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true),
447
+ // eslint-disable-next-line no-template-curly-in-string
448
+ setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo ${lion}', true),
449
+ // eslint-disable-next-line no-template-curly-in-string
450
+ setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie ${panther}', true)];
451
+ var PAGE = {
452
+ collection: COLLECTION,
453
+ components: COMPONENTS,
454
+ formData: _objectSpread(_objectSpread({}, DATA), {}, _defineProperty({}, COLLECTION.name, [_objectSpread(_objectSpread({}, DATA[COLLECTION.name][0]), {}, {
455
+ lion: 'Lion',
456
+ panther: 'Panther'
457
+ })]))
458
+ };
459
+ var RESULT = (0, _validatePage.default)(PAGE);
460
+ expect(RESULT.length).toEqual(2);
461
+ expect(RESULT[0]).toEqual({
462
+ id: 'bravo',
463
+ error: "Enter bravo ".concat(PAGE.formData[COLLECTION.name][0].lion, " in the correct format, like jane.doe@homeoffice.gov.uk")
464
+ });
465
+ expect(RESULT[1]).toEqual({
466
+ id: 'charlie',
467
+ error: "Charlie ".concat(PAGE.formData[COLLECTION.name][0].panther, " is required")
468
+ });
469
+ });
470
+ });
471
+ });
472
+ });