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

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 +39 -13
  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 +217 -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 +73 -22
  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
@@ -5,14 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
8
+ 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); }
8
9
  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; }
9
10
  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; }
10
- 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; }
11
+ 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; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
13
+ 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); }
11
14
  var applyFormula = function applyFormula(config) {
12
15
  try {
13
16
  var result = simplify(config);
14
17
  return !result && result !== 0 ? '' : result;
15
18
  } catch (err) {
19
+ // eslint-disable-next-line no-console
16
20
  console.error(err.message);
17
21
  }
18
22
  return '';
@@ -57,14 +61,13 @@ var reduceNumber = function reduceNumber(config, reduction) {
57
61
  }, 0), config);
58
62
  };
59
63
  var round = function round(number, config) {
60
- var round = config.formula.round;
61
- if (!round && round !== 0 || !number) return number;
62
- if (round === 0) {
63
- return parseInt(number);
64
- } else {
65
- var precisionScale = Math.pow(10, round);
66
- return Math.round((number + Number.EPSILON) * precisionScale) / precisionScale;
64
+ var roundNumber = config.formula.round;
65
+ if (!roundNumber && roundNumber !== 0 || !number) return number;
66
+ if (roundNumber === 0) {
67
+ return parseInt(number, 10);
67
68
  }
69
+ var precisionScale = Math.pow(10, roundNumber);
70
+ return Math.round((number + Number.EPSILON) * precisionScale) / precisionScale;
68
71
  };
69
72
  var getValue = function getValue(arg, formData) {
70
73
  var keys = Object.keys(arg);
@@ -73,8 +76,11 @@ var getValue = function getValue(arg, formData) {
73
76
  var val = arg[key];
74
77
  switch (key) {
75
78
  case 'field':
76
- var fieldVal = fieldValue(val, formData);
77
- return !fieldVal && fieldVal !== 0 ? NaN : parseFloat(fieldVal);
79
+ {
80
+ var fieldVal = fieldValue(val, formData);
81
+ return !fieldVal && fieldVal !== 0 ? NaN : parseFloat(fieldVal);
82
+ }
83
+ ;
78
84
  case 'value':
79
85
  return parseFloat(val);
80
86
  case 'formula':
@@ -89,7 +95,10 @@ var getValue = function getValue(arg, formData) {
89
95
  }
90
96
  };
91
97
  var fieldValue = function fieldValue(field, data) {
92
- return _copReactComponents.Utils.interpolateString('${' + field + '}', data);
98
+ return (
99
+ // eslint-disable-next-line prefer-template
100
+ _copReactComponents.Utils.interpolateString('${' + field + '}', data)
101
+ );
93
102
  };
94
103
  var _default = applyFormula;
95
104
  exports.default = _default;
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+
3
+ var _applyFormula = _interopRequireDefault(require("./applyFormula"));
4
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
+ // Local imports
6
+
7
+ describe('utils.Data.applyFormula', function () {
8
+ var error = jest.spyOn(console, 'error').mockImplementation(function () {});
9
+ afterAll(function () {
10
+ error.mockReset();
11
+ });
12
+ afterEach(function () {
13
+ error.mockClear();
14
+ });
15
+ it('should throw and handle exception for a null config', function () {
16
+ (0, _applyFormula.default)(null);
17
+ expect(error).toBeCalledWith("Missing 'formula' definition");
18
+ });
19
+ it("should throw and handle exception for config with missing 'formula'", function () {
20
+ (0, _applyFormula.default)({});
21
+ expect(error).toBeCalledWith("Missing 'formula' definition");
22
+ });
23
+ it("should throw and handle exception for config with 'formula' but missing 'name'", function () {
24
+ (0, _applyFormula.default)({
25
+ formula: {}
26
+ });
27
+ expect(error).toBeCalledWith("Calculation formula 'name' cannot be empty");
28
+ });
29
+ it("should throw and handle exception for config with 'formula' with unsupported operation 'name'", function () {
30
+ (0, _applyFormula.default)({
31
+ formula: {
32
+ name: "something"
33
+ }
34
+ });
35
+ expect(error).toBeCalledWith("Unsupported operation 'something'");
36
+ });
37
+ it("should throw and handle exception for 'formula' with wrong argument name", function () {
38
+ (0, _applyFormula.default)({
39
+ formula: {
40
+ name: 'plus',
41
+ args: [{
42
+ somthing: 1
43
+ }, {
44
+ somthing: 1
45
+ }]
46
+ }
47
+ });
48
+ expect(error).toBeCalledWith('Only accept following as argument field: {field, value, or formula}');
49
+ });
50
+ it("should throw and handle exception for 'formula' argument with more than one field", function () {
51
+ (0, _applyFormula.default)({
52
+ formula: {
53
+ name: 'plus',
54
+ args: [{
55
+ value: 1,
56
+ field: 'fieldA'
57
+ }, {
58
+ value: 1
59
+ }]
60
+ }
61
+ });
62
+ expect(error).toBeCalledWith('Argument cannot have more than one reference');
63
+ });
64
+ it("should throw and handle exception for 'formula' with single argument", function () {
65
+ (0, _applyFormula.default)({
66
+ formula: {
67
+ name: 'plus',
68
+ args: [{
69
+ value: 10
70
+ }]
71
+ }
72
+ });
73
+ expect(error).toBeCalledWith('Requires more than one argument for calculation');
74
+ });
75
+ var DATA = {
76
+ fieldA: '10',
77
+ fieldB: '20',
78
+ fieldC: 'abc',
79
+ fieldD: '0',
80
+ fieldE: '10'
81
+ };
82
+ [{
83
+ name: 'plus',
84
+ args: [{
85
+ field: 'fieldA'
86
+ }, {
87
+ field: 'fieldB'
88
+ }],
89
+ result: 30
90
+ }, {
91
+ name: 'minus',
92
+ args: [{
93
+ field: 'fieldB'
94
+ }, {
95
+ field: 'fieldA'
96
+ }],
97
+ result: 10
98
+ }, {
99
+ name: 'multiply',
100
+ args: [{
101
+ field: 'fieldA'
102
+ }, {
103
+ field: 'fieldB'
104
+ }],
105
+ result: 200
106
+ }, {
107
+ name: 'divide',
108
+ args: [{
109
+ field: 'fieldA'
110
+ }, {
111
+ field: 'fieldB'
112
+ }],
113
+ result: 0.5
114
+ }, {
115
+ name: 'divide',
116
+ args: [{
117
+ field: 'fieldA'
118
+ }, {
119
+ field: 'fieldD'
120
+ }],
121
+ result: Infinity
122
+ }, {
123
+ name: 'plus',
124
+ args: [{
125
+ field: 'fieldA'
126
+ }, {
127
+ field: 'fieldC'
128
+ }],
129
+ result: ''
130
+ }, {
131
+ name: 'multiply',
132
+ args: [{
133
+ field: 'fieldA'
134
+ }, {
135
+ field: 'fieldB'
136
+ }, {
137
+ field: 'fieldE'
138
+ }],
139
+ result: 2000
140
+ }].forEach(function (test) {
141
+ it("should calculate formula '".concat(test.name, "' correctly for field args to '").concat(test.result, "'"), function () {
142
+ var config = {
143
+ formData: DATA,
144
+ formula: {
145
+ name: test.name,
146
+ args: test.args
147
+ }
148
+ };
149
+ expect((0, _applyFormula.default)(config)).toEqual(test.result);
150
+ });
151
+ });
152
+ [{
153
+ fieldA: '1',
154
+ fieldB: '3',
155
+ round: 5,
156
+ result: 0.33333
157
+ }, {
158
+ fieldA: '2',
159
+ fieldB: '3',
160
+ round: 5,
161
+ result: 0.66667
162
+ }, {
163
+ fieldA: '1',
164
+ fieldB: '3',
165
+ round: 4,
166
+ result: 0.3333
167
+ }, {
168
+ fieldA: '2',
169
+ fieldB: '3',
170
+ round: 4,
171
+ result: 0.6667
172
+ }, {
173
+ fieldA: '1',
174
+ fieldB: '3',
175
+ round: 3,
176
+ result: 0.333
177
+ }, {
178
+ fieldA: '2',
179
+ fieldB: '3',
180
+ round: 3,
181
+ result: 0.667
182
+ }, {
183
+ fieldA: '1',
184
+ fieldB: '3',
185
+ round: 2,
186
+ result: 0.33
187
+ }, {
188
+ fieldA: '2',
189
+ fieldB: '3',
190
+ round: 2,
191
+ result: 0.67
192
+ }, {
193
+ fieldA: '1',
194
+ fieldB: '3',
195
+ round: 1,
196
+ result: 0.3
197
+ }, {
198
+ fieldA: '2',
199
+ fieldB: '3',
200
+ round: 1,
201
+ result: 0.7
202
+ }, {
203
+ fieldA: '1',
204
+ fieldB: '3',
205
+ round: 0,
206
+ result: 0
207
+ }, {
208
+ fieldA: '2',
209
+ fieldB: '3',
210
+ round: 0,
211
+ result: 0
212
+ }, {
213
+ fieldA: 'L',
214
+ fieldB: 'G3',
215
+ round: 1,
216
+ result: ''
217
+ }].forEach(function (test) {
218
+ it("should calculdate and round result to precision ".concat(test.round), function () {
219
+ var config = {
220
+ formData: {
221
+ fieldA: test.fieldA,
222
+ fieldB: test.fieldB
223
+ },
224
+ formula: {
225
+ name: 'divide',
226
+ round: test.round,
227
+ args: [{
228
+ field: 'fieldA'
229
+ }, {
230
+ field: 'fieldB'
231
+ }]
232
+ }
233
+ };
234
+ expect((0, _applyFormula.default)(config)).toEqual(test.result);
235
+ });
236
+ });
237
+ it("should calculate nested 'formula'", function () {
238
+ var config = {
239
+ formData: {
240
+ fieldA: '19',
241
+ fieldB: '66'
242
+ },
243
+ formula: {
244
+ name: 'multiply',
245
+ round: 2,
246
+ args: [{
247
+ formula: {
248
+ name: 'divide',
249
+ round: 4,
250
+ args: [{
251
+ field: 'fieldA'
252
+ }, {
253
+ field: 'fieldB'
254
+ }]
255
+ }
256
+ }, {
257
+ value: 100
258
+ }]
259
+ }
260
+ };
261
+ expect((0, _applyFormula.default)(config)).toEqual(28.79);
262
+ });
263
+ });
@@ -4,8 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
7
8
  var _getOptions = _interopRequireDefault(require("./getOptions"));
8
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ // Global Imports
11
+
9
12
  // Local imports
10
13
 
11
14
  var getAutocompleteSource = function getAutocompleteSource(config) {
@@ -13,11 +16,12 @@ var getAutocompleteSource = function getAutocompleteSource(config) {
13
16
  (0, _getOptions.default)(config, function (val) {
14
17
  options = val;
15
18
  });
19
+ var labelMaker = config !== null && config !== void 0 && config.item ? _copReactComponents.Utils.itemLabel(config.item) : null;
16
20
  return function (query, populateResults) {
17
21
  var lcQuery = query ? query.toLowerCase() : '';
18
22
  populateResults(options.filter(function (opt) {
19
- var lcLabel = opt.label ? opt.label.toLowerCase() : '';
20
- return lcLabel.includes(lcQuery);
23
+ var label = labelMaker ? labelMaker(opt) : opt.label || '';
24
+ return label.toLowerCase().includes(lcQuery);
21
25
  }));
22
26
  };
23
27
  };
@@ -0,0 +1,352 @@
1
+ "use strict";
2
+
3
+ var _getAutocompleteSource = _interopRequireDefault(require("./getAutocompleteSource"));
4
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
+ // Local imports
6
+
7
+ describe('utils', function () {
8
+ describe('Data', function () {
9
+ describe('getAutocompleteSource', function () {
10
+ it('should handle a null config', function () {
11
+ var SOURCE = (0, _getAutocompleteSource.default)(null);
12
+ expect(typeof SOURCE === 'function').toBeTruthy();
13
+ SOURCE('query', function (results) {
14
+ expect(Array.isArray(results)).toBeTruthy();
15
+ expect(results.length).toEqual(0);
16
+ });
17
+ });
18
+ describe('without custom item labels', function () {
19
+ it('should get any specified options from the config', function () {
20
+ var CONFIG = {
21
+ data: {
22
+ options: [{
23
+ value: 'a',
24
+ label: 'Alpha'
25
+ }, {
26
+ value: 'b',
27
+ label: 'Bravo'
28
+ }]
29
+ }
30
+ };
31
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
32
+ expect(typeof SOURCE === 'function').toBeTruthy();
33
+ SOURCE('a', function (results) {
34
+ expect(results.length).toEqual(2);
35
+ expect(results).toEqual([{
36
+ value: 'a',
37
+ label: 'Alpha'
38
+ }, {
39
+ value: 'b',
40
+ label: 'Bravo'
41
+ }]);
42
+ });
43
+ SOURCE('al', function (results) {
44
+ expect(results.length).toEqual(1);
45
+ expect(results).toEqual([{
46
+ value: 'a',
47
+ label: 'Alpha'
48
+ }]);
49
+ });
50
+ });
51
+ it('should handle an empty query', function () {
52
+ var CONFIG = {
53
+ data: {
54
+ options: [{
55
+ value: 'a',
56
+ label: 'Alpha'
57
+ }, {
58
+ value: 'b',
59
+ label: 'Bravo'
60
+ }]
61
+ }
62
+ };
63
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
64
+ SOURCE('', function (results) {
65
+ expect(results.length).toEqual(2);
66
+ expect(results).toEqual([{
67
+ value: 'a',
68
+ label: 'Alpha'
69
+ }, {
70
+ value: 'b',
71
+ label: 'Bravo'
72
+ }]);
73
+ });
74
+ });
75
+ it('should handle an undefined query', function () {
76
+ var CONFIG = {
77
+ data: {
78
+ options: [{
79
+ value: 'a',
80
+ label: 'Alpha'
81
+ }, {
82
+ value: 'b',
83
+ label: 'Bravo'
84
+ }]
85
+ }
86
+ };
87
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
88
+ SOURCE(undefined, function (results) {
89
+ expect(results.length).toEqual(2);
90
+ expect(results).toEqual([{
91
+ value: 'a',
92
+ label: 'Alpha'
93
+ }, {
94
+ value: 'b',
95
+ label: 'Bravo'
96
+ }]);
97
+ });
98
+ });
99
+ it('should handle a null query', function () {
100
+ var CONFIG = {
101
+ data: {
102
+ options: [{
103
+ value: 'a',
104
+ label: 'Alpha'
105
+ }, {
106
+ value: 'b',
107
+ label: 'Bravo'
108
+ }]
109
+ }
110
+ };
111
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
112
+ SOURCE(null, function (results) {
113
+ expect(results.length).toEqual(2);
114
+ expect(results).toEqual([{
115
+ value: 'a',
116
+ label: 'Alpha'
117
+ }, {
118
+ value: 'b',
119
+ label: 'Bravo'
120
+ }]);
121
+ });
122
+ });
123
+ it('should handle a missing label on an option', function () {
124
+ var CONFIG = {
125
+ data: {
126
+ options: [{
127
+ value: 'a',
128
+ label: 'Alpha'
129
+ }, {
130
+ value: 'b'
131
+ }]
132
+ }
133
+ };
134
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
135
+ expect(typeof SOURCE === 'function').toBeTruthy();
136
+ SOURCE('a', function (results) {
137
+ expect(results.length).toEqual(1);
138
+ expect(results).toEqual([{
139
+ value: 'a',
140
+ label: 'Alpha'
141
+ }]);
142
+ });
143
+ });
144
+ });
145
+ describe('with custom item labels', function () {
146
+ it('should get any specified options from the config', function () {
147
+ var CONFIG = {
148
+ item: {
149
+ value: 'currencyCode',
150
+ label: 'currencyCode',
151
+ // eslint-disable-next-line no-template-curly-in-string
152
+ format: '${currencyName} (${currencyCode})'
153
+ },
154
+ data: {
155
+ options: [{
156
+ currencyName: 'Great British Pounds',
157
+ currencyCode: 'GBP',
158
+ value: 'GBP',
159
+ label: 'GBP'
160
+ }, {
161
+ currencyName: 'United States Dollars',
162
+ currencyCode: 'USD',
163
+ value: 'USD',
164
+ label: 'USD'
165
+ }]
166
+ }
167
+ };
168
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
169
+ expect(typeof SOURCE === 'function').toBeTruthy();
170
+ SOURCE('GB', function (results) {
171
+ expect(results.length).toEqual(1);
172
+ expect(results).toEqual([{
173
+ currencyName: 'Great British Pounds',
174
+ currencyCode: 'GBP',
175
+ value: 'GBP',
176
+ label: 'GBP'
177
+ }]);
178
+ });
179
+ SOURCE(' (', function (results) {
180
+ expect(results.length).toEqual(2);
181
+ expect(results).toEqual([{
182
+ currencyName: 'Great British Pounds',
183
+ currencyCode: 'GBP',
184
+ value: 'GBP',
185
+ label: 'GBP'
186
+ }, {
187
+ currencyName: 'United States Dollars',
188
+ currencyCode: 'USD',
189
+ value: 'USD',
190
+ label: 'USD'
191
+ }]);
192
+ });
193
+ });
194
+ it('should handle an empty query', function () {
195
+ var CONFIG = {
196
+ item: {
197
+ value: 'currencyCode',
198
+ label: 'currencyCode',
199
+ // eslint-disable-next-line no-template-curly-in-string
200
+ format: '${currencyName} (${currencyCode})'
201
+ },
202
+ data: {
203
+ options: [{
204
+ currencyName: 'Great British Pounds',
205
+ currencyCode: 'GBP',
206
+ value: 'GBP',
207
+ label: 'GBP'
208
+ }, {
209
+ currencyName: 'United States Dollars',
210
+ currencyCode: 'USD',
211
+ value: 'USD',
212
+ label: 'USD'
213
+ }]
214
+ }
215
+ };
216
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
217
+ SOURCE('', function (results) {
218
+ expect(results.length).toEqual(2);
219
+ expect(results).toEqual([{
220
+ currencyName: 'Great British Pounds',
221
+ currencyCode: 'GBP',
222
+ value: 'GBP',
223
+ label: 'GBP'
224
+ }, {
225
+ currencyName: 'United States Dollars',
226
+ currencyCode: 'USD',
227
+ value: 'USD',
228
+ label: 'USD'
229
+ }]);
230
+ });
231
+ });
232
+ it('should handle an undefined query', function () {
233
+ var CONFIG = {
234
+ item: {
235
+ value: 'currencyCode',
236
+ label: 'currencyCode',
237
+ // eslint-disable-next-line no-template-curly-in-string
238
+ format: '${currencyName} (${currencyCode})'
239
+ },
240
+ data: {
241
+ options: [{
242
+ currencyName: 'Great British Pounds',
243
+ currencyCode: 'GBP',
244
+ value: 'GBP',
245
+ label: 'GBP'
246
+ }, {
247
+ currencyName: 'United States Dollars',
248
+ currencyCode: 'USD',
249
+ value: 'USD',
250
+ label: 'USD'
251
+ }]
252
+ }
253
+ };
254
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
255
+ SOURCE(undefined, function (results) {
256
+ expect(results.length).toEqual(2);
257
+ expect(results).toEqual([{
258
+ currencyName: 'Great British Pounds',
259
+ currencyCode: 'GBP',
260
+ value: 'GBP',
261
+ label: 'GBP'
262
+ }, {
263
+ currencyName: 'United States Dollars',
264
+ currencyCode: 'USD',
265
+ value: 'USD',
266
+ label: 'USD'
267
+ }]);
268
+ });
269
+ });
270
+ it('should handle a null query', function () {
271
+ var CONFIG = {
272
+ item: {
273
+ value: 'currencyCode',
274
+ label: 'currencyCode',
275
+ // eslint-disable-next-line no-template-curly-in-string
276
+ format: '${currencyName} (${currencyCode})'
277
+ },
278
+ data: {
279
+ options: [{
280
+ currencyName: 'Great British Pounds',
281
+ currencyCode: 'GBP',
282
+ value: 'GBP',
283
+ label: 'GBP'
284
+ }, {
285
+ currencyName: 'United States Dollars',
286
+ currencyCode: 'USD',
287
+ value: 'USD',
288
+ label: 'USD'
289
+ }]
290
+ }
291
+ };
292
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
293
+ SOURCE(null, function (results) {
294
+ expect(results.length).toEqual(2);
295
+ expect(results).toEqual([{
296
+ currencyName: 'Great British Pounds',
297
+ currencyCode: 'GBP',
298
+ value: 'GBP',
299
+ label: 'GBP'
300
+ }, {
301
+ currencyName: 'United States Dollars',
302
+ currencyCode: 'USD',
303
+ value: 'USD',
304
+ label: 'USD'
305
+ }]);
306
+ });
307
+ });
308
+ it('should handle a missing label on an option', function () {
309
+ var CONFIG = {
310
+ item: {
311
+ value: 'currencyCode',
312
+ label: 'currencyCode',
313
+ // eslint-disable-next-line no-template-curly-in-string
314
+ format: '${currencyName} (${currencyCode})'
315
+ },
316
+ data: {
317
+ options: [{
318
+ currencyName: 'Great British Pounds',
319
+ currencyCode: 'GBP',
320
+ value: 'GBP',
321
+ label: 'GBP'
322
+ }, {
323
+ currencyName: 'United States Dollars',
324
+ currencyCode: 'USD',
325
+ value: 'USD'
326
+ }]
327
+ }
328
+ };
329
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
330
+ expect(typeof SOURCE === 'function').toBeTruthy();
331
+ SOURCE('a', function (results) {
332
+ // We expect 2 results here instead of 1 because defining a custom
333
+ // format for item labels means the label is constructed upon processing
334
+ // a query - we're not relying on the 'label' field of the option.
335
+ expect(results.length).toEqual(2);
336
+ expect(results).toEqual([{
337
+ currencyName: 'Great British Pounds',
338
+ currencyCode: 'GBP',
339
+ value: 'GBP',
340
+ label: 'GBP'
341
+ }, {
342
+ currencyName: 'United States Dollars',
343
+ currencyCode: 'USD',
344
+ value: 'USD',
345
+ label: ''
346
+ }]);
347
+ });
348
+ });
349
+ });
350
+ });
351
+ });
352
+ });