@ukhomeoffice/cop-react-form-renderer 4.26.0 → 4.28.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.
@@ -67,6 +67,30 @@ var meetsCondition = function meetsCondition(condition, value) {
67
67
  return true;
68
68
  }
69
69
 
70
+ case '<':
71
+ {
72
+ if (!value || !compare) {
73
+ return false;
74
+ }
75
+
76
+ var valFloat = parseFloat(value.replace(',', ''));
77
+ var compareFloat = parseFloat(compare.replace(',', ''));
78
+ return valFloat < compareFloat;
79
+ }
80
+
81
+ case '>':
82
+ {
83
+ if (!value || !compare) {
84
+ return false;
85
+ }
86
+
87
+ var _valFloat = parseFloat(value.replace(',', ''));
88
+
89
+ var _compareFloat = parseFloat(compare.replace(',', ''));
90
+
91
+ return _valFloat > _compareFloat;
92
+ }
93
+
70
94
  case 'contains':
71
95
  {
72
96
  return value === null || value === void 0 ? void 0 : value.toString().toLowerCase().includes(compare); // If no value is provided, the field cannot contain it, so it must fail the condition.
@@ -439,6 +439,78 @@ describe('utils.Condition.meetsCondition', function () {
439
439
  expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
440
440
  });
441
441
  });
442
+ describe('numerical operators', function () {
443
+ it('less than should reject when value is null', function () {
444
+ var CONDITION = {
445
+ op: '<',
446
+ value: '100'
447
+ };
448
+ expect((0, _meetsCondition.default)(CONDITION, null)).toBeFalsy();
449
+ });
450
+ it('less than should reject when condition.value is null', function () {
451
+ var CONDITION = {
452
+ op: '<',
453
+ value: null
454
+ };
455
+ expect((0, _meetsCondition.default)(CONDITION, '100')).toBeFalsy();
456
+ });
457
+ it('less than should reject when condition.value and value are null', function () {
458
+ var CONDITION = {
459
+ op: '<',
460
+ value: null
461
+ };
462
+ expect((0, _meetsCondition.default)(CONDITION, null)).toBeFalsy();
463
+ });
464
+ it('less than should reject when value is greater than condition.value', function () {
465
+ var CONDITION = {
466
+ op: '<',
467
+ value: '100'
468
+ };
469
+ expect((0, _meetsCondition.default)(CONDITION, '101')).toBeFalsy();
470
+ });
471
+ it('less than should accept when value is less than condition.value', function () {
472
+ var CONDITION = {
473
+ op: '<',
474
+ value: '100'
475
+ };
476
+ expect((0, _meetsCondition.default)(CONDITION, '99')).toBeTruthy();
477
+ });
478
+ it('greater than should reject when value is null', function () {
479
+ var CONDITION = {
480
+ op: '>',
481
+ value: '100'
482
+ };
483
+ expect((0, _meetsCondition.default)(CONDITION, null)).toBeFalsy();
484
+ });
485
+ it('greater than should reject when condition.value is null', function () {
486
+ var CONDITION = {
487
+ op: '>',
488
+ value: null
489
+ };
490
+ expect((0, _meetsCondition.default)(CONDITION, '100')).toBeFalsy();
491
+ });
492
+ it('greater than should reject when condition.value and value are null', function () {
493
+ var CONDITION = {
494
+ op: '>',
495
+ value: null
496
+ };
497
+ expect((0, _meetsCondition.default)(CONDITION, null)).toBeFalsy();
498
+ });
499
+ it('greater than should reject when value is less than condition.value', function () {
500
+ var CONDITION = {
501
+ op: '>',
502
+ value: '100'
503
+ };
504
+ expect((0, _meetsCondition.default)(CONDITION, '99')).toBeFalsy();
505
+ });
506
+ it('greater than should accept when value is greater than condition.value', function () {
507
+ var CONDITION = {
508
+ op: '>',
509
+ value: '100'
510
+ };
511
+ expect((0, _meetsCondition.default)(CONDITION, '101')).toBeTruthy();
512
+ });
513
+ });
442
514
  describe('unknown operator', function () {
443
515
  var op = 'definitely_not_a_real_operator';
444
516
  it('should reject anything regardless of the value', function () {
@@ -29,6 +29,8 @@ var _mustBeLessThan = _interopRequireDefault(require("./mustBeLessThan"));
29
29
 
30
30
  var _mustHaveLessThanDecimalPlaces = _interopRequireDefault(require("./mustHaveLessThanDecimalPlaces"));
31
31
 
32
+ var _mustSelectOnlyOne = _interopRequireDefault(require("./mustSelectOnlyOne"));
33
+
32
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
35
 
34
36
  // Local imports
@@ -44,7 +46,8 @@ var functions = {
44
46
  mustEnterAtLeastOne: _mustEnterAtLeastOne.default,
45
47
  mustBeNumbersOnly: _mustBeNumbersOnly.default,
46
48
  mustBeLessThan: _mustBeLessThan.default,
47
- mustHaveLessThanDecimalPlaces: _mustHaveLessThanDecimalPlaces.default
49
+ mustHaveLessThanDecimalPlaces: _mustHaveLessThanDecimalPlaces.default,
50
+ mustSelectOnlyOne: _mustSelectOnlyOne.default
48
51
  };
49
52
 
50
53
  var additionalValidation = function additionalValidation(value, config, component) {
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * Additional validator for the Container component type.
10
+ * @param {object} data the form data
11
+ * @param {object} component the container component
12
+ * @returns true if the user has only provided a value for one of the components in the container, otherwise false.
13
+ */
14
+ var mustSelectOnlyOne = function mustSelectOnlyOne(data, _, component) {
15
+ var _component$components;
16
+
17
+ var valueCount = (_component$components = component.components) === null || _component$components === void 0 ? void 0 : _component$components.reduce(function (acc, inner) {
18
+ var _data$component$id;
19
+
20
+ if ((_data$component$id = data[component.id]) !== null && _data$component$id !== void 0 && _data$component$id[inner.id]) {
21
+ // Account for checkboxes that the user may have unselected
22
+ if (Array.isArray(data[component.id][inner.id]) && data[component.id][inner.id].length === 0) {
23
+ return acc;
24
+ }
25
+
26
+ return acc + 1;
27
+ }
28
+
29
+ return acc;
30
+ }, 0);
31
+ return valueCount < 2;
32
+ };
33
+
34
+ var _default = mustSelectOnlyOne;
35
+ exports.default = _default;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ var _mustSelectOnlyOne = _interopRequireDefault(require("./mustSelectOnlyOne"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ 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; }
8
+
9
+ describe('utils', function () {
10
+ describe('Validate', function () {
11
+ describe('additional', function () {
12
+ describe('mustSelectOnlyOne', function () {
13
+ var CONTAINER_ID = 'containerId';
14
+ var COMPONENT = {
15
+ id: CONTAINER_ID,
16
+ type: 'container',
17
+ components: [{
18
+ id: 'alpha'
19
+ }, {
20
+ id: 'bravo'
21
+ }, {
22
+ id: 'charlie'
23
+ }]
24
+ };
25
+ test('should return true if all components are empty', function () {
26
+ var DATA = {};
27
+ expect((0, _mustSelectOnlyOne.default)(DATA, {}, COMPONENT)).toEqual(true);
28
+ });
29
+ test('should return true if one component has data', function () {
30
+ var DATA = _defineProperty({}, CONTAINER_ID, {
31
+ alpha: 'text'
32
+ });
33
+
34
+ expect((0, _mustSelectOnlyOne.default)(DATA, {}, COMPONENT)).toEqual(true);
35
+ });
36
+ test('should return false if more than one component has data', function () {
37
+ var DATA = _defineProperty({}, CONTAINER_ID, {
38
+ alpha: 'text',
39
+ bravo: 'test'
40
+ });
41
+
42
+ expect((0, _mustSelectOnlyOne.default)(DATA, {}, COMPONENT)).toEqual(false);
43
+ });
44
+ test('should return true if more than one component has data but one is an empty array', function () {
45
+ var DATA = _defineProperty({}, CONTAINER_ID, {
46
+ alpha: 'text',
47
+ bravo: []
48
+ });
49
+
50
+ expect((0, _mustSelectOnlyOne.default)(DATA, {}, COMPONENT)).toEqual(true);
51
+ });
52
+ });
53
+ });
54
+ });
55
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.26.0",
3
+ "version": "4.28.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",