@ukhomeoffice/cop-react-form-renderer 5.13.2 → 5.14.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.
@@ -10,6 +10,19 @@ var _setupConditions = _interopRequireDefault(require("./setupConditions"));
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
  // Local imports
12
12
 
13
+ var testConditions = function testConditions(conditions, data) {
14
+ var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'and';
15
+ var arr = Array.isArray(conditions) ? conditions : [conditions];
16
+ var testCallback = function testCallback(condition) {
17
+ if (Array.isArray(condition.conditions)) {
18
+ return testConditions(condition.conditions, data, condition.type);
19
+ }
20
+ var sourceDataValue = _Data.default.getSource(data, condition.field);
21
+ return (0, _meetsCondition.default)(condition, sourceDataValue, data);
22
+ };
23
+ return type === 'and' ? arr.every(testCallback) : arr.some(testCallback);
24
+ };
25
+
13
26
  /**
14
27
  * Evaluates all condition(s) on a page, container, or component.
15
28
  * @param {object} options The container to consider.
@@ -19,11 +32,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
32
  var meetsAllConditions = function meetsAllConditions(options, data) {
20
33
  var conditions = (0, _setupConditions.default)(options);
21
34
  if (conditions) {
22
- var arr = Array.isArray(conditions) ? conditions : [conditions];
23
- return arr.every(function (condition) {
24
- var sourceDataValue = _Data.default.getSource(data, condition.field);
25
- return (0, _meetsCondition.default)(condition, sourceDataValue, data);
26
- });
35
+ return testConditions(conditions, data);
27
36
  }
28
37
  return true;
29
38
  };
@@ -58,4 +58,38 @@ describe('utils.Condition.meetsAllConditions', function () {
58
58
  }];
59
59
  expect((0, _meetsAllConditions.default)(CONDITIONS, DATA)).toBeFalsy();
60
60
  });
61
+ it('should handle nested conditions', function () {
62
+ var VALID_DATA = {
63
+ shouldBeAlpha: 'alpha',
64
+ shouldBeBravo: 'bravo',
65
+ canBeCharlieOrDelta: 'charlie'
66
+ };
67
+ var INVALID_DATA = {
68
+ shouldBeAlpha: 'alpha',
69
+ shouldBeBravo: 'bravo',
70
+ canBeCharlieOrDelta: 'echo'
71
+ };
72
+ var CONDITIONS = [{
73
+ type: 'or',
74
+ conditions: [{
75
+ field: 'canBeCharlieOrDelta',
76
+ op: 'eq',
77
+ value: 'charlie'
78
+ }, {
79
+ field: 'canBeCharlieOrDelta',
80
+ op: 'eq',
81
+ value: 'delta'
82
+ }]
83
+ }, {
84
+ field: 'shouldBeAlpha',
85
+ op: 'eq',
86
+ value: 'alpha'
87
+ }, {
88
+ field: 'shouldBeBravo',
89
+ op: 'eq',
90
+ value: 'bravo'
91
+ }];
92
+ expect((0, _meetsAllConditions.default)(CONDITIONS, VALID_DATA)).toBeTruthy();
93
+ expect((0, _meetsAllConditions.default)(CONDITIONS, INVALID_DATA)).toBeFalsy();
94
+ });
61
95
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.13.2",
3
+ "version": "5.14.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",