Skip to content

Commit 60ffa54

Browse files
committed
refactor: test or conditions
1 parent 856febe commit 60ffa54

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

source/js/conditions/condition/orCondition.test.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,63 @@ import { describe, expect, it } from '@jest/globals';
22
import OrCondition from './orCondition';
33

44
describe('OR Condition', () => {
5+
const conditionWithoutClass: Condition = {
6+
field: 'testField',
7+
class: null,
8+
value: null,
9+
operator: '',
10+
};
11+
12+
const orConditionWithoutClass = new OrCondition(conditionWithoutClass);
13+
514
it('getConditionFieldNames() returns an array of field names from provided Condition', () => {
6-
const condition: Condition = {
7-
field: 'testField',
8-
class: null,
9-
value: null,
10-
operator: '',
11-
};
15+
expect(orConditionWithoutClass.getConditionFieldNames()).toEqual(['testField']);
16+
});
1217

13-
const orCondition = new OrCondition(condition);
14-
const fieldNames = orCondition.getConditionFieldNames();
18+
it('getConditions() returns an array of Conditions', () => {
19+
expect(orConditionWithoutClass.getConditions()).toEqual([conditionWithoutClass]);
20+
});
21+
22+
it('validate() returns true if no class', () => {
23+
expect(orConditionWithoutClass.validate()).toEqual(true);
24+
});
1525

16-
expect(fieldNames).toEqual(['testField']);
26+
it('validate() returns false if field is already disabled', () => {
27+
const orCondition = new OrCondition(createConditionWithClass(true, true));
28+
expect(orCondition.validate()).toEqual(false);
1729
});
18-
});
30+
31+
it('validate() returns true if is not disabled and condition is valid', () => {
32+
const orCondition = new OrCondition(createConditionWithClass(false, true));
33+
expect(orCondition.validate()).toEqual(true);
34+
});
35+
36+
it('validate() returns false if validation failed', () => {
37+
const orCondition = new OrCondition(createConditionWithClass(false, false));
38+
expect(orCondition.validate()).toEqual(false);
39+
});
40+
});
41+
42+
function createConditionWithClass(isDisabled: boolean, isValid: boolean): Condition {
43+
return {
44+
field: 'testField',
45+
class: {
46+
getConditionsHandler() {
47+
return {
48+
getIsDisabled() {
49+
return isDisabled;
50+
}
51+
} as ConditionsHandlerInterface;
52+
},
53+
getConditionValidator() {
54+
return {
55+
validate(condition: Condition) {
56+
return isValid;
57+
}
58+
} as ConditionValidatorInterface;
59+
},
60+
} as FieldInterface,
61+
value: null,
62+
operator: '',
63+
} as Condition;
64+
}

source/js/conditions/condition/orCondition.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ class OrCondition implements ConditionInterface {
77
return true;
88
}
99

10-
return this.condition.class.getConditionsHandler().getIsDisabled() ?
11-
false :
12-
(this.condition.class.getConditionValidator().validate(this.condition) ?? true);
10+
if (this.condition.class.getConditionsHandler().getIsDisabled()) {
11+
return false;
12+
}
13+
14+
return this.condition.class.getConditionValidator().validate(this.condition);
1315
}
1416

1517
public getConditions(): Condition[] {

0 commit comments

Comments
 (0)