@ukhomeoffice/cop-react-form-renderer 2.2.0 → 2.5.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.
- package/dist/components/FormRenderer/FormRenderer.js +105 -6
- package/dist/components/FormRenderer/FormRenderer.test.js +235 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.js +3 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.test.js +6 -0
- package/dist/components/FormRenderer/helpers/getPage.js +1 -1
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +18 -1
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +44 -6
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +50 -0
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +122 -0
- package/dist/components/FormRenderer/helpers/index.js +7 -1
- package/dist/components/TaskList/Task.js +83 -0
- package/dist/components/TaskList/Task.test.js +112 -0
- package/dist/components/TaskList/TaskList.js +112 -0
- package/dist/components/TaskList/TaskList.scss +70 -0
- package/dist/components/TaskList/TaskList.stories.mdx +57 -0
- package/dist/components/TaskList/TaskList.test.js +223 -0
- package/dist/components/TaskList/TaskState.js +42 -0
- package/dist/components/TaskList/TaskState.test.js +99 -0
- package/dist/components/TaskList/index.js +13 -0
- package/dist/json/taskList.json +228 -0
- package/dist/json/userProfile.data.json +2 -1
- package/dist/models/ComponentTypes.js +3 -1
- package/dist/models/FormTypes.js +2 -0
- package/dist/models/HubFormats.js +3 -1
- package/dist/models/PageAction.js +5 -0
- package/dist/models/TaskStates.js +41 -0
- package/dist/models/index.js +8 -0
- package/dist/utils/Component/cleanAttributes.js +1 -1
- package/dist/utils/Component/getComponent.js +8 -0
- package/dist/utils/Component/isEditable.js +1 -1
- package/dist/utils/FormPage/getPageActions.js +6 -0
- package/dist/utils/Hub/getFormHub.js +4 -0
- package/dist/utils/Hub/getFormHub.test.js +10 -2
- package/dist/utils/Validate/additional/index.js +55 -0
- package/dist/utils/Validate/additional/index.test.js +69 -0
- package/dist/utils/Validate/additional/mustBeAfter.js +37 -0
- package/dist/utils/Validate/additional/mustBeAfter.test.js +98 -0
- package/dist/utils/Validate/additional/mustBeBefore.js +35 -0
- package/dist/utils/Validate/additional/mustBeBefore.test.js +66 -0
- package/dist/utils/Validate/additional/mustBeInTheFuture.js +42 -0
- package/dist/utils/Validate/additional/mustBeInTheFuture.test.js +34 -0
- package/dist/utils/Validate/additional/mustBeInThePast.js +44 -0
- package/dist/utils/Validate/additional/mustBeInThePast.test.js +34 -0
- package/dist/utils/Validate/additional/utils.js +66 -0
- package/dist/utils/Validate/validateComponent.js +37 -0
- package/dist/utils/Validate/validateComponent.test.js +48 -2
- package/dist/utils/Validate/validateDate.js +144 -0
- package/dist/utils/Validate/validateDate.test.js +112 -0
- package/dist/utils/Validate/validateTime.js +76 -0
- package/dist/utils/Validate/validateTime.test.js +55 -0
- package/package.json +3 -2
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.StateTypes = void 0;
|
|
7
|
+
|
|
8
|
+
var _StateDetails;
|
|
9
|
+
|
|
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
|
+
|
|
12
|
+
var TYPE_COMPLETE = 'complete';
|
|
13
|
+
var TYPE_IN_PROGRESS = 'inProgress';
|
|
14
|
+
var TYPE_NOT_STARTED = 'notStarted';
|
|
15
|
+
var TYPE_CANNOT_START_YET = 'cannotStartYet';
|
|
16
|
+
var StateTypes = {
|
|
17
|
+
COMPLETE: TYPE_COMPLETE,
|
|
18
|
+
IN_PROGRESS: TYPE_IN_PROGRESS,
|
|
19
|
+
NOT_STARTED: TYPE_NOT_STARTED,
|
|
20
|
+
CANNOT_START_YET: TYPE_CANNOT_START_YET
|
|
21
|
+
};
|
|
22
|
+
exports.StateTypes = StateTypes;
|
|
23
|
+
var StateDetails = (_StateDetails = {}, _defineProperty(_StateDetails, TYPE_COMPLETE, {
|
|
24
|
+
label: 'Completed',
|
|
25
|
+
colour: ''
|
|
26
|
+
}), _defineProperty(_StateDetails, TYPE_IN_PROGRESS, {
|
|
27
|
+
label: 'In Progress',
|
|
28
|
+
colour: 'blue'
|
|
29
|
+
}), _defineProperty(_StateDetails, TYPE_NOT_STARTED, {
|
|
30
|
+
label: 'Not Started',
|
|
31
|
+
colour: 'grey'
|
|
32
|
+
}), _defineProperty(_StateDetails, TYPE_CANNOT_START_YET, {
|
|
33
|
+
label: 'Cannot Start Yet',
|
|
34
|
+
colour: 'grey'
|
|
35
|
+
}), _StateDetails);
|
|
36
|
+
var TaskStates = {
|
|
37
|
+
TYPES: StateTypes,
|
|
38
|
+
DETAILS: StateDetails
|
|
39
|
+
};
|
|
40
|
+
var _default = TaskStates;
|
|
41
|
+
exports.default = _default;
|
package/dist/models/index.js
CHANGED
|
@@ -39,6 +39,12 @@ Object.defineProperty(exports, "PageAction", {
|
|
|
39
39
|
return _PageAction.default;
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
|
+
Object.defineProperty(exports, "TaskStates", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function get() {
|
|
45
|
+
return _TaskStates.default;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
42
48
|
|
|
43
49
|
var _ComponentTypes = _interopRequireDefault(require("./ComponentTypes"));
|
|
44
50
|
|
|
@@ -52,4 +58,6 @@ var _HubFormats = _interopRequireDefault(require("./HubFormats"));
|
|
|
52
58
|
|
|
53
59
|
var _PageAction = _interopRequireDefault(require("./PageAction"));
|
|
54
60
|
|
|
61
|
+
var _TaskStates = _interopRequireDefault(require("./TaskStates"));
|
|
62
|
+
|
|
55
63
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -19,7 +19,7 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
19
19
|
|
|
20
20
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
21
|
|
|
22
|
-
var JSON_ONLY_PROPERTIES = ['source', 'use', 'show_when', 'options'];
|
|
22
|
+
var JSON_ONLY_PROPERTIES = ['source', 'use', 'show_when', 'options', 'additionalValidation'];
|
|
23
23
|
/**
|
|
24
24
|
* This method removes and properties that are entirely specific to the JSON
|
|
25
25
|
* configuration and that should not be used as HTML attributes.
|
|
@@ -95,6 +95,11 @@ var getTextInput = function getTextInput(config) {
|
|
|
95
95
|
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, attrs);
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
+
var getTime = function getTime(config) {
|
|
99
|
+
var attrs = (0, _cleanAttributes.default)(config);
|
|
100
|
+
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TimeInput, attrs);
|
|
101
|
+
};
|
|
102
|
+
|
|
98
103
|
var getComponentByType = function getComponentByType(config) {
|
|
99
104
|
switch (config.type) {
|
|
100
105
|
case _models.ComponentTypes.HTML:
|
|
@@ -123,6 +128,9 @@ var getComponentByType = function getComponentByType(config) {
|
|
|
123
128
|
case _models.ComponentTypes.DATE:
|
|
124
129
|
return getDate(config);
|
|
125
130
|
|
|
131
|
+
case _models.ComponentTypes.TIME:
|
|
132
|
+
return getTime(config);
|
|
133
|
+
|
|
126
134
|
case _models.ComponentTypes.CHECKBOXES:
|
|
127
135
|
return getCheckboxes(config);
|
|
128
136
|
|
|
@@ -8,7 +8,7 @@ exports.default = exports.EDITABLE_TYPES = void 0;
|
|
|
8
8
|
var _models = require("../../models");
|
|
9
9
|
|
|
10
10
|
// Local imports
|
|
11
|
-
var EDITABLE_TYPES = [_models.ComponentTypes.AUTOCOMPLETE, _models.ComponentTypes.CHECKBOXES, _models.ComponentTypes.DATE, _models.ComponentTypes.EMAIL, _models.ComponentTypes.PHONE_NUMBER, _models.ComponentTypes.RADIOS, _models.ComponentTypes.TEXT, _models.ComponentTypes.TEXT_AREA];
|
|
11
|
+
var EDITABLE_TYPES = [_models.ComponentTypes.AUTOCOMPLETE, _models.ComponentTypes.CHECKBOXES, _models.ComponentTypes.DATE, _models.ComponentTypes.EMAIL, _models.ComponentTypes.PHONE_NUMBER, _models.ComponentTypes.RADIOS, _models.ComponentTypes.TEXT, _models.ComponentTypes.TEXT_AREA, _models.ComponentTypes.TIME];
|
|
12
12
|
exports.EDITABLE_TYPES = EDITABLE_TYPES;
|
|
13
13
|
|
|
14
14
|
var isEditable = function isEditable(options) {
|
|
@@ -50,6 +50,12 @@ var getPageActions = function getPageActions(page) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
if (a && _typeof(a) === 'object') {
|
|
53
|
+
var _PageAction$DEFAULTS$;
|
|
54
|
+
|
|
55
|
+
if ((_PageAction$DEFAULTS$ = _models.PageAction.DEFAULTS[a.type]) !== null && _PageAction$DEFAULTS$ !== void 0 && _PageAction$DEFAULTS$.validate) {
|
|
56
|
+
a.validate = true;
|
|
57
|
+
}
|
|
58
|
+
|
|
53
59
|
return standardiseAction(a);
|
|
54
60
|
}
|
|
55
61
|
|
|
@@ -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: [
|
|
66
|
+
components: ['Opening paragraph', {
|
|
67
67
|
type: 'heading',
|
|
68
68
|
size: 'l',
|
|
69
69
|
content: 'Hub heading'
|
|
70
|
-
},
|
|
70
|
+
}, 'Closing paragraph', {
|
|
71
71
|
use: 'a'
|
|
72
72
|
}]
|
|
73
73
|
};
|
|
@@ -91,6 +91,14 @@ describe('utils', function () {
|
|
|
91
91
|
});
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
|
+
describe("when the FormType is \"".concat(_models.FormTypes.TASK, "\""), function () {
|
|
95
|
+
it("should return the string \"".concat(_models.HubFormats.TASK, "\" when the format is set to \"").concat(_models.HubFormats.TASK, "\""), function () {
|
|
96
|
+
var HUB = {
|
|
97
|
+
format: _models.HubFormats.TASK
|
|
98
|
+
};
|
|
99
|
+
expect((0, _getFormHub.default)(_models.FormTypes.TASK, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual(_models.HubFormats.TASK);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
94
102
|
});
|
|
95
103
|
});
|
|
96
104
|
});
|
|
@@ -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;
|