@ukhomeoffice/cop-react-form-renderer 2.1.0 → 2.6.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 (90) hide show
  1. package/dist/components/FormComponent/Container.test.js +250 -0
  2. package/dist/components/FormComponent/FormComponent.js +105 -21
  3. package/dist/components/FormComponent/FormComponent.test.js +67 -0
  4. package/dist/components/FormPage/FormPage.js +2 -1
  5. package/dist/components/FormRenderer/FormRenderer.js +105 -6
  6. package/dist/components/FormRenderer/FormRenderer.test.js +235 -0
  7. package/dist/components/FormRenderer/helpers/getNextPageId.js +3 -0
  8. package/dist/components/FormRenderer/helpers/getNextPageId.test.js +6 -0
  9. package/dist/components/FormRenderer/helpers/getPage.js +1 -1
  10. package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +18 -1
  11. package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +44 -6
  12. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +50 -0
  13. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +122 -0
  14. package/dist/components/FormRenderer/helpers/index.js +7 -1
  15. package/dist/components/TaskList/Task.js +83 -0
  16. package/dist/components/TaskList/Task.test.js +112 -0
  17. package/dist/components/TaskList/TaskList.js +112 -0
  18. package/dist/components/TaskList/TaskList.scss +70 -0
  19. package/dist/components/TaskList/TaskList.stories.mdx +57 -0
  20. package/dist/components/TaskList/TaskList.test.js +223 -0
  21. package/dist/components/TaskList/TaskState.js +42 -0
  22. package/dist/components/TaskList/TaskState.test.js +99 -0
  23. package/dist/components/TaskList/index.js +13 -0
  24. package/dist/hooks/index.js +19 -1
  25. package/dist/hooks/useHooks.js +5 -1
  26. package/dist/json/taskList.json +228 -0
  27. package/dist/json/userProfile.data.json +2 -1
  28. package/dist/models/ComponentTypes.js +5 -1
  29. package/dist/models/FormTypes.js +2 -0
  30. package/dist/models/HubFormats.js +3 -1
  31. package/dist/models/PageAction.js +5 -0
  32. package/dist/models/TaskStates.js +41 -0
  33. package/dist/models/index.js +8 -0
  34. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +40 -0
  35. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +257 -0
  36. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -1
  37. package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +44 -0
  38. package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -0
  39. package/dist/utils/Component/cleanAttributes.js +1 -1
  40. package/dist/utils/Component/getComponent.js +26 -0
  41. package/dist/utils/Component/isEditable.js +1 -1
  42. package/dist/utils/Component/showComponent.js +2 -20
  43. package/dist/utils/Condition/index.js +19 -0
  44. package/dist/utils/Condition/meetsAllConditions.js +40 -0
  45. package/dist/utils/Condition/meetsAllConditions.test.js +62 -0
  46. package/dist/utils/{meetsCondition.js → Condition/meetsCondition.js} +0 -0
  47. package/dist/utils/Condition/meetsCondition.test.js +302 -0
  48. package/dist/utils/Condition/setupConditions.js +47 -0
  49. package/dist/utils/Condition/setupConditions.test.js +35 -0
  50. package/dist/utils/Container/getEditableComponents.js +38 -0
  51. package/dist/utils/Container/getEditableComponents.test.js +157 -0
  52. package/dist/utils/Container/index.js +22 -0
  53. package/dist/utils/Container/setupNesting.js +45 -0
  54. package/dist/utils/Container/setupNesting.test.js +92 -0
  55. package/dist/utils/Container/showContainer.js +61 -0
  56. package/dist/utils/Container/showContainer.test.js +128 -0
  57. package/dist/utils/Data/getDataPath.js +90 -0
  58. package/dist/utils/Data/getDataPath.test.js +52 -0
  59. package/dist/utils/Data/index.js +3 -0
  60. package/dist/utils/FormPage/getFormPage.js +4 -2
  61. package/dist/utils/FormPage/getFormPage.test.js +18 -7
  62. package/dist/utils/FormPage/getFormPages.test.js +5 -2
  63. package/dist/utils/FormPage/getPageActions.js +6 -0
  64. package/dist/utils/FormPage/index.js +0 -3
  65. package/dist/utils/FormPage/showFormPage.js +7 -27
  66. package/dist/utils/Hub/getFormHub.js +4 -0
  67. package/dist/utils/Hub/getFormHub.test.js +15 -4
  68. package/dist/utils/Validate/additional/index.js +55 -0
  69. package/dist/utils/Validate/additional/index.test.js +69 -0
  70. package/dist/utils/Validate/additional/mustBeAfter.js +37 -0
  71. package/dist/utils/Validate/additional/mustBeAfter.test.js +98 -0
  72. package/dist/utils/Validate/additional/mustBeBefore.js +35 -0
  73. package/dist/utils/Validate/additional/mustBeBefore.test.js +66 -0
  74. package/dist/utils/Validate/additional/mustBeInTheFuture.js +42 -0
  75. package/dist/utils/Validate/additional/mustBeInTheFuture.test.js +34 -0
  76. package/dist/utils/Validate/additional/mustBeInThePast.js +44 -0
  77. package/dist/utils/Validate/additional/mustBeInThePast.test.js +34 -0
  78. package/dist/utils/Validate/additional/utils.js +66 -0
  79. package/dist/utils/Validate/index.js +7 -1
  80. package/dist/utils/Validate/validateComponent.js +57 -0
  81. package/dist/utils/Validate/validateComponent.test.js +92 -2
  82. package/dist/utils/Validate/validateDate.js +155 -0
  83. package/dist/utils/Validate/validateDate.test.js +118 -0
  84. package/dist/utils/Validate/validatePage.js +6 -8
  85. package/dist/utils/Validate/validateTime.js +95 -0
  86. package/dist/utils/Validate/validateTime.test.js +61 -0
  87. package/dist/utils/index.js +6 -3
  88. package/package.json +3 -2
  89. package/dist/utils/FormPage/getEditableComponents.js +0 -28
  90. package/dist/utils/FormPage/getEditableComponents.test.js +0 -75
@@ -7,44 +7,24 @@ exports.default = void 0;
7
7
 
8
8
  var _Component = _interopRequireDefault(require("../Component"));
9
9
 
10
- var _Data = _interopRequireDefault(require("../Data"));
10
+ var _Condition = _interopRequireDefault(require("../Condition"));
11
11
 
12
- var _meetsCondition = _interopRequireDefault(require("../meetsCondition"));
13
-
14
- var _getEditableComponents = _interopRequireDefault(require("./getEditableComponents"));
12
+ var _Container = _interopRequireDefault(require("../Container"));
15
13
 
16
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
15
 
18
16
  // Local imports
19
17
 
20
- /**
21
- * Evaluates the show_when condition(s) on a page.
22
- * @param {object} page The page to consider.
23
- * @param {object} data The top-level form data.
24
- * @returns Boolean true if all show_when conditions are met; false otherwise.
25
- */
26
- var evaluatePageShowWhen = function evaluatePageShowWhen(page, data) {
27
- var show_when = Array.isArray(page.show_when) ? page.show_when : [page.show_when];
28
- var allConditionsMet = true;
29
- show_when.forEach(function (condition) {
30
- var sourceDataValue = _Data.default.getSource(data, condition.field);
31
-
32
- allConditionsMet = allConditionsMet && (0, _meetsCondition.default)(condition, sourceDataValue);
33
- });
34
- return allConditionsMet;
35
- };
36
18
  /**
37
19
  * Checks whether any of the editable components on a page should be shown.
38
20
  * @param {Array} editableComponents The editable components on the page.
39
21
  * @param {object} data The top-level form data.
40
22
  * @returns Boolean true if ANY of the editable components should be shown; false otherwise.
41
23
  */
42
-
43
-
44
24
  var showEditableComponent = function showEditableComponent(editableComponents, data) {
45
- return editableComponents.reduce(function (shown, component) {
46
- return shown || _Component.default.show(component, data);
47
- }, false);
25
+ return editableComponents.some(function (component) {
26
+ return _Component.default.show(component, data);
27
+ });
48
28
  };
49
29
  /**
50
30
  * Indicates whether or not a page should be shown.
@@ -61,12 +41,12 @@ var showFormPage = function showFormPage(page, data) {
61
41
 
62
42
 
63
43
  if (page.show_when) {
64
- return evaluatePageShowWhen(page, data);
44
+ return _Condition.default.meetsAll(page, data);
65
45
  } // If the page itself doesn't have a show_when, we need to make sure that if it
66
46
  // contains ANY editable components, at least one of them is shown.
67
47
 
68
48
 
69
- var editableComponents = (0, _getEditableComponents.default)(page);
49
+ var editableComponents = _Container.default.editableComponents(page);
70
50
 
71
51
  if (editableComponents.length > 0) {
72
52
  return showEditableComponent(editableComponents, data);
@@ -37,6 +37,10 @@ var getFormHub = function getFormHub(type, hub, components, formData) {
37
37
  }
38
38
  }
39
39
 
40
+ if (type === _models.FormTypes.TASK) {
41
+ return _models.HubFormats.TASK;
42
+ }
43
+
40
44
  return undefined;
41
45
  };
42
46
 
@@ -63,11 +63,11 @@ describe('utils', function () {
63
63
  it("should return an appropriate page when the format is not \"".concat(_models.HubFormats.CYA, "\" and hub contains components"), function () {
64
64
  var HUB = {
65
65
  title: 'Hub title',
66
- components: ["Opening paragraph", {
66
+ components: ['Opening paragraph', {
67
67
  type: 'heading',
68
68
  size: 'l',
69
69
  content: 'Hub heading'
70
- }, "Closing paragraph", {
70
+ }, 'Closing paragraph', {
71
71
  use: 'a'
72
72
  }]
73
73
  };
@@ -78,19 +78,30 @@ describe('utils', function () {
78
78
  type: 'html',
79
79
  tagName: 'p',
80
80
  content: HUB.components[0]
81
- }, HUB.components[1], {
81
+ }, _objectSpread(_objectSpread({}, HUB.components[1]), {}, {
82
+ full_path: HUB.components[1].fieldId
83
+ }), {
82
84
  type: 'html',
83
85
  tagName: 'p',
84
86
  content: HUB.components[2]
85
87
  }, _objectSpread(_objectSpread({
86
88
  use: 'a'
87
89
  }, A), {}, {
88
- cya_label: A.label
90
+ cya_label: A.label,
91
+ full_path: A.fieldId
89
92
  })],
90
93
  formData: FORM_DATA
91
94
  });
92
95
  });
93
96
  });
97
+ describe("when the FormType is \"".concat(_models.FormTypes.TASK, "\""), function () {
98
+ it("should return the string \"".concat(_models.HubFormats.TASK, "\" when the format is set to \"").concat(_models.HubFormats.TASK, "\""), function () {
99
+ var HUB = {
100
+ format: _models.HubFormats.TASK
101
+ };
102
+ expect((0, _getFormHub.default)(_models.FormTypes.TASK, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual(_models.HubFormats.TASK);
103
+ });
104
+ });
94
105
  });
95
106
  });
96
107
  });
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _mustBeAfter = _interopRequireDefault(require("./mustBeAfter"));
9
+
10
+ var _mustBeBefore = _interopRequireDefault(require("./mustBeBefore"));
11
+
12
+ var _mustBeInThePast = _interopRequireDefault(require("./mustBeInThePast"));
13
+
14
+ var _mustBeInTheFuture = _interopRequireDefault(require("./mustBeInTheFuture"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ // Local imports
19
+ var functions = {
20
+ mustBeAfter: _mustBeAfter.default,
21
+ mustBeBefore: _mustBeBefore.default,
22
+ mustBeInThePast: _mustBeInThePast.default,
23
+ mustBeInTheFuture: _mustBeInTheFuture.default
24
+ };
25
+
26
+ var additionalValidation = function additionalValidation(value, config) {
27
+ var fn = functions[config.function];
28
+
29
+ if (typeof fn === 'function') {
30
+ return fn(value, config) ? undefined : config.message;
31
+ }
32
+
33
+ return undefined;
34
+ };
35
+
36
+ var runAdditionalComponentValidation = function runAdditionalComponentValidation(component, value) {
37
+ // We only care when we have a value - if we don't have one but want one, set `required: true`.
38
+ if (!!value) {
39
+ if (Array.isArray(component.additionalValidation)) {
40
+ var error = undefined;
41
+ component.additionalValidation.forEach(function (config) {
42
+ // If we've already encountered an error, don't run any more validators.
43
+ if (!error) {
44
+ error = additionalValidation(value, config);
45
+ }
46
+ });
47
+ return error;
48
+ }
49
+ }
50
+
51
+ return undefined;
52
+ };
53
+
54
+ var _default = runAdditionalComponentValidation;
55
+ exports.default = _default;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
6
+
7
+ var _utils = require("./utils");
8
+
9
+ var _index = _interopRequireDefault(require("./index"));
10
+
11
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
+
13
+ _dayjs.default.extend(_customParseFormat.default);
14
+
15
+ describe('utils', function () {
16
+ describe('Validate', function () {
17
+ describe('additional', function () {
18
+ it('should apply optional validators when specified', function () {
19
+ var COMPONENT = {
20
+ additionalValidation: [{
21
+ function: 'mustBeAfter',
22
+ value: 3,
23
+ unit: 'day',
24
+ message: 'Date must be more than 3 days in the future'
25
+ }]
26
+ };
27
+ var VALUE = (0, _dayjs.default)().add(1, 'day').format(_utils.DATE_FORMAT);
28
+ expect((0, _index.default)(COMPONENT, VALUE)).toEqual('Date must be more than 3 days in the future');
29
+ });
30
+ it('should apply multiple optional validators when specified', function () {
31
+ var COMPONENT = {
32
+ additionalValidation: [{
33
+ function: 'mustBeAfter',
34
+ value: 100,
35
+ unit: 'day',
36
+ message: 'Date must be more than 100 days in the future'
37
+ }, {
38
+ function: 'mustBeBefore',
39
+ value: 200,
40
+ unit: 'day',
41
+ message: 'Date must be less than 200 days in the future'
42
+ }]
43
+ };
44
+ var VALUE = (0, _dayjs.default)().add(250, 'day').format(_utils.DATE_FORMAT);
45
+ expect((0, _index.default)(COMPONENT, VALUE)).toEqual('Date must be less than 200 days in the future');
46
+ });
47
+ it('should apply multiple optional validators when specified and return undefined if all are passed', function () {
48
+ var COMPONENT = {
49
+ additionalValidation: [{
50
+ function: 'mustBeAfter',
51
+ value: 100,
52
+ unit: 'day',
53
+ message: 'Date must be more than 100 days in the future'
54
+ }, {
55
+ function: 'mustBeBefore',
56
+ value: 200,
57
+ unit: 'day',
58
+ message: 'Date must be less than 200 days in the future'
59
+ }, {
60
+ function: 'mustBeInTheFuture',
61
+ message: 'Date must be in the future'
62
+ }]
63
+ };
64
+ var VALUE = (0, _dayjs.default)().add(150, 'day').format(_utils.DATE_FORMAT);
65
+ expect((0, _index.default)(COMPONENT, VALUE)).toEqual(undefined);
66
+ });
67
+ });
68
+ });
69
+ });
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dayjs = _interopRequireDefault(require("dayjs"));
9
+
10
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
11
+
12
+ var _utils = require("./utils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ // Global imports
17
+ // Local imports
18
+ _dayjs.default.extend(_customParseFormat.default);
19
+ /**
20
+ * Function returns true if the date string parameter is for a date more than ('is before') N months
21
+ * in the past, where N is the number of months parameter
22
+ * NOTE: the one minute reduction on the threshold date is a workaround for as apparent bug in the
23
+ * day.js isBefore() function when checking on 'same dates'
24
+ * @param {string} date - date string in a valid date format e.g. DD-MM-YYYY
25
+ * @param {string} config.unit - day, month or year
26
+ * @param {number} config.value - number of the unit to be after
27
+ * @returns false if date is NOT after the no of months in the past.
28
+ */
29
+
30
+
31
+ var mustBeAfter = function mustBeAfter(date, config) {
32
+ var dateToCompare = (0, _dayjs.default)().add(config.value, config.unit);
33
+ return (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isAfter(dateToCompare);
34
+ };
35
+
36
+ var _default = mustBeAfter;
37
+ exports.default = _default;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _mustBeAfter = _interopRequireDefault(require("./mustBeAfter"));
6
+
7
+ var _utils = require("./utils");
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ describe('utils', function () {
12
+ describe('Validate', function () {
13
+ describe('additional', function () {
14
+ describe('mustBeAfter', function () {
15
+ test('should return true given a date after the given number of months in the future', function () {
16
+ var date = (0, _dayjs.default)().add(3, 'month').format(_utils.DATE_FORMAT);
17
+ var result = (0, _mustBeAfter.default)(date, {
18
+ value: 1,
19
+ unit: 'month'
20
+ });
21
+ expect(result).toEqual(true);
22
+ });
23
+ test('should return false given a date before the given number of months in the future', function () {
24
+ var date = (0, _dayjs.default)().add(3, 'month').format(_utils.DATE_FORMAT);
25
+ var result = (0, _mustBeAfter.default)(date, {
26
+ value: 5,
27
+ unit: 'month'
28
+ });
29
+ expect(result).toEqual(false);
30
+ });
31
+ test('should return true given a date after the given number of months in the past', function () {
32
+ var date = (0, _dayjs.default)().add(-3, 'month').format(_utils.DATE_FORMAT);
33
+ var result = (0, _mustBeAfter.default)(date, {
34
+ value: -5,
35
+ unit: 'month'
36
+ });
37
+ expect(result).toEqual(true);
38
+ });
39
+ test('should return false given a date before the given number of months in the past', function () {
40
+ var date = (0, _dayjs.default)().add(-10, 'month').format(_utils.DATE_FORMAT);
41
+ var result = (0, _mustBeAfter.default)(date, {
42
+ value: -5,
43
+ unit: 'month'
44
+ });
45
+ expect(result).toEqual(false);
46
+ });
47
+ test('should return true given a date after the given number of years in the future', function () {
48
+ var date = (0, _dayjs.default)().add(3, 'year').format(_utils.DATE_FORMAT);
49
+ var result = (0, _mustBeAfter.default)(date, {
50
+ value: 1,
51
+ unit: 'year'
52
+ });
53
+ expect(result).toEqual(true);
54
+ });
55
+ test('should return false given a date before the given number of years in the future', function () {
56
+ var date = (0, _dayjs.default)().add(15, 'year').format(_utils.DATE_FORMAT);
57
+ var result = (0, _mustBeAfter.default)(date, {
58
+ value: 20,
59
+ unit: 'year'
60
+ });
61
+ expect(result).toEqual(false);
62
+ });
63
+ test('should return true given a date after the given number of years in the past', function () {
64
+ var date = (0, _dayjs.default)().add(-15, 'year').format(_utils.DATE_FORMAT);
65
+ var result = (0, _mustBeAfter.default)(date, {
66
+ value: -20,
67
+ unit: 'year'
68
+ });
69
+ expect(result).toEqual(true);
70
+ });
71
+ test('should return false given a date before the given number of years in the past', function () {
72
+ var date = (0, _dayjs.default)().add(-25, 'year').format(_utils.DATE_FORMAT);
73
+ var result = (0, _mustBeAfter.default)(date, {
74
+ value: -20,
75
+ unit: 'year'
76
+ });
77
+ expect(result).toEqual(false);
78
+ });
79
+ test('should return true given a date after the given number of days in the future', function () {
80
+ var date = (0, _dayjs.default)().add(3, 'day').format(_utils.DATE_FORMAT);
81
+ var result = (0, _mustBeAfter.default)(date, {
82
+ value: 1,
83
+ unit: 'day'
84
+ });
85
+ expect(result).toEqual(true);
86
+ });
87
+ test('should return true given a date before the given number of days in the future', function () {
88
+ var date = (0, _dayjs.default)().add(250, 'day').format(_utils.DATE_FORMAT);
89
+ var result = (0, _mustBeAfter.default)(date, {
90
+ value: 275,
91
+ unit: 'day'
92
+ });
93
+ expect(result).toEqual(false);
94
+ });
95
+ });
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dayjs = _interopRequireDefault(require("dayjs"));
9
+
10
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
11
+
12
+ var _utils = require("./utils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ // Global imports
17
+ // Local imports
18
+ _dayjs.default.extend(_customParseFormat.default);
19
+ /**
20
+ * Function returns false if the date string parameter is for a date more than N in the future
21
+ * where N is the number of months parameter
22
+ * @param {string} dateToCheck - date string in a valid date format e.g. DD-MM-YYYY
23
+ * @param {string} config.unit - day, month or year
24
+ * @param {number} config.value - number of the unit to be before
25
+ * @returns true if date is not after the no of months in future.
26
+ */
27
+
28
+
29
+ var mustBeBefore = function mustBeBefore(date, config) {
30
+ var dateToCompare = (0, _dayjs.default)().add(config.value, config.unit);
31
+ return (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isBefore(dateToCompare);
32
+ };
33
+
34
+ var _default = mustBeBefore;
35
+ exports.default = _default;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _mustBeBefore = _interopRequireDefault(require("./mustBeBefore"));
6
+
7
+ var _utils = require("./utils");
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ describe('utils', function () {
12
+ describe('Validate', function () {
13
+ describe('additional', function () {
14
+ describe('mustBeBefore', function () {
15
+ test('should return true given a date before the given number of months in the future', function () {
16
+ var date = (0, _dayjs.default)().add(1, 'month').format(_utils.DATE_FORMAT);
17
+ var result = (0, _mustBeBefore.default)(date, {
18
+ value: 3,
19
+ unit: 'month'
20
+ });
21
+ expect(result).toEqual(true);
22
+ });
23
+ test('should return false given a date after the given number of months in the future', function () {
24
+ var date = (0, _dayjs.default)().add(5, 'month').format(_utils.DATE_FORMAT);
25
+ var result = (0, _mustBeBefore.default)(date, {
26
+ value: 3,
27
+ unit: 'month'
28
+ });
29
+ expect(result).toEqual(false);
30
+ });
31
+ test('should return true given a date before the given number of years in the future', function () {
32
+ var date = (0, _dayjs.default)().add(1, 'year').format(_utils.DATE_FORMAT);
33
+ var result = (0, _mustBeBefore.default)(date, {
34
+ value: 3,
35
+ unit: 'year'
36
+ });
37
+ expect(result).toEqual(true);
38
+ });
39
+ test('should return false given a date after the given number of years in the future', function () {
40
+ var date = (0, _dayjs.default)().add(20, 'year').format(_utils.DATE_FORMAT);
41
+ var result = (0, _mustBeBefore.default)(date, {
42
+ value: 15,
43
+ unit: 'year'
44
+ });
45
+ expect(result).toEqual(false);
46
+ });
47
+ test('should return true given a date before the given number of days in the future', function () {
48
+ var date = (0, _dayjs.default)().add(1, 'day').format(_utils.DATE_FORMAT);
49
+ var result = (0, _mustBeBefore.default)(date, {
50
+ value: 3,
51
+ unit: 'day'
52
+ });
53
+ expect(result).toEqual(true);
54
+ });
55
+ test('should return true given a date after the given number of days in the future', function () {
56
+ var date = (0, _dayjs.default)().add(275, 'day').format(_utils.DATE_FORMAT);
57
+ var result = (0, _mustBeBefore.default)(date, {
58
+ value: 250,
59
+ unit: 'day'
60
+ });
61
+ expect(result).toEqual(false);
62
+ });
63
+ });
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dayjs = _interopRequireDefault(require("dayjs"));
9
+
10
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
11
+
12
+ var _isToday = _interopRequireDefault(require("dayjs/plugin/isToday"));
13
+
14
+ var _utils = require("./utils");
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ // Global Imports
19
+ //Local imports
20
+ _dayjs.default.extend(_customParseFormat.default);
21
+
22
+ _dayjs.default.extend(_isToday.default);
23
+ /**
24
+ * Function returns true if the date parameter is a date after today, else returns false
25
+ * @param {string} date - date string e.g. '03-12-2021'
26
+ * @param {boolean} config.todayAllowed - true if today should be a valid date
27
+ * @returns true if date is after current date
28
+ */
29
+
30
+
31
+ var mustBeInTheFuture = function mustBeInTheFuture(date, config) {
32
+ var afterToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isAfter((0, _dayjs.default)());
33
+
34
+ if (afterToday) {
35
+ return true;
36
+ }
37
+
38
+ return !!(config !== null && config !== void 0 && config.todayAllowed) && (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isToday();
39
+ };
40
+
41
+ var _default = mustBeInTheFuture;
42
+ exports.default = _default;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _mustBeInTheFuture = _interopRequireDefault(require("./mustBeInTheFuture"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ describe('utils', function () {
10
+ describe('Validate', function () {
11
+ describe('additional', function () {
12
+ describe('mustBeInTheFuture', function () {
13
+ test('should return false given a date in the past', function () {
14
+ var result = (0, _mustBeInTheFuture.default)((0, _dayjs.default)().subtract(1, 'day').format('DD-MM-YYYY'));
15
+ expect(result).toEqual(false);
16
+ });
17
+ test('should return true given a date in the future', function () {
18
+ var result = (0, _mustBeInTheFuture.default)((0, _dayjs.default)().add(1, 'day').format('DD-MM-YYYY'));
19
+ expect(result).toEqual(true);
20
+ });
21
+ test('should return false given todays date', function () {
22
+ var result = (0, _mustBeInTheFuture.default)((0, _dayjs.default)().format('DD-MM-YYYY'));
23
+ expect(result).toEqual(false);
24
+ });
25
+ test('should return true given todays date if todayAllowed is true', function () {
26
+ var result = (0, _mustBeInTheFuture.default)((0, _dayjs.default)().format('DD-MM-YYYY'), {
27
+ todayAllowed: true
28
+ });
29
+ expect(result).toEqual(true);
30
+ });
31
+ });
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dayjs = _interopRequireDefault(require("dayjs"));
9
+
10
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
11
+
12
+ var _isToday = _interopRequireDefault(require("dayjs/plugin/isToday"));
13
+
14
+ var _utils = require("./utils");
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ // Global imports
19
+ //Local imports
20
+ _dayjs.default.extend(_customParseFormat.default);
21
+
22
+ _dayjs.default.extend(_isToday.default);
23
+ /**
24
+ * Function returns true if the date parameter is a date before today, else returns false
25
+ * dayjs counts today as before today so also cover same day inputs
26
+ * @param {string} date - date string e.g. '03-12-2021'
27
+ * @param {boolean} config.todayAllowed - true if today should be a valid date
28
+ * @returns true if date is before current date
29
+ */
30
+
31
+
32
+ var mustBeInThePast = function mustBeInThePast(date, config) {
33
+ var beforeToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isBefore((0, _dayjs.default)());
34
+ var isToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isToday(); //dayjs classifies the current day as in the past so exclude it here
35
+
36
+ if (beforeToday && !isToday) {
37
+ return true;
38
+ }
39
+
40
+ return isToday && !!(config !== null && config !== void 0 && config.todayAllowed);
41
+ };
42
+
43
+ var _default = mustBeInThePast;
44
+ exports.default = _default;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _mustBeInThePast = _interopRequireDefault(require("./mustBeInThePast"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ describe('utils', function () {
10
+ describe('Validate', function () {
11
+ describe('additional', function () {
12
+ describe('mustBeInThePast', function () {
13
+ test('should return true given a date in the past', function () {
14
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().subtract(1, 'day').format('DD-MM-YYYY'));
15
+ expect(result).toEqual(true);
16
+ });
17
+ test('should return false given a date in the future', function () {
18
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().add(1, 'day').format('DD-MM-YYYY'));
19
+ expect(result).toEqual(false);
20
+ });
21
+ test('should return false given todays date', function () {
22
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().format('DD-MM-YYYY'));
23
+ expect(result).toEqual(false);
24
+ });
25
+ test('should return true given todays date if todayAllowed is true', function () {
26
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().format('DD-MM-YYYY'), {
27
+ todayAllowed: true
28
+ });
29
+ expect(result).toEqual(true);
30
+ });
31
+ });
32
+ });
33
+ });
34
+ });