1+ import { describe , expect , it } from '@jest/globals' ;
2+ import ConditionBuilder from './conditionBuilder' ;
3+ import NullCondition from './condition/nullCondition' ;
4+ import OrCondition from './condition/orCondition' ;
5+ import AndCondition from './condition/andCondition' ;
6+
7+ describe ( 'Condition Builder' , ( ) => {
8+ const conditionWithoutClass : Condition = {
9+ field : 'testField' ,
10+ class : null ,
11+ value : null ,
12+ operator : '' ,
13+ } ;
14+
15+ const conditionBuilder = new ConditionBuilder ( {
16+ 'testField' : {
17+ } as FieldInterface ,
18+ 'testField2' : {
19+ } as FieldInterface ,
20+ } ) ;
21+
22+ it ( 'build() returns an array with a NullCondition if no conditions or faulty value are provided' , ( ) => {
23+ const nullConditionEmptyArray = conditionBuilder . build ( [ ] ) ;
24+ const nullConditionFaultyValue = conditionBuilder . build ( null ) ;
25+ expect ( nullConditionEmptyArray ) . toHaveLength ( 1 ) ;
26+ expect ( nullConditionEmptyArray [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
27+ expect ( nullConditionFaultyValue ) . toHaveLength ( 1 ) ;
28+ expect ( nullConditionFaultyValue [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
29+ } ) ;
30+
31+ it ( 'build() returns an array with NullConditions faulty array items are provided' , ( ) => {
32+ const nullConditionFaultyValue = conditionBuilder . build ( [ 'item' , [ ] ] ) ;
33+ expect ( nullConditionFaultyValue ) . toHaveLength ( 2 ) ;
34+ expect ( nullConditionFaultyValue [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
35+ expect ( nullConditionFaultyValue [ 1 ] ) . toBeInstanceOf ( NullCondition ) ;
36+ } ) ;
37+
38+ it ( 'build() returns an array with a NullCondition if conditionSet doesnt contain valid conditionalData' , ( ) => {
39+ const faultyItemType = conditionBuilder . build ( [ [ 'item' , 'item' ] ] ) ;
40+ const faultyObjectMissingFieldName = conditionBuilder . build ( [ [ { operator : '==' , value : 1 } ] ] ) ;
41+ const faultyObjectMissingValue = conditionBuilder . build ( [ [ { field : 'field' , operator : '==' } ] ] ) ;
42+ const faultyObjectMissingOperator = conditionBuilder . build ( [ [ { field : 'field' , value : 1 } ] ] ) ;
43+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
44+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
45+ expect ( faultyObjectMissingFieldName ) . toHaveLength ( 1 ) ;
46+ expect ( faultyObjectMissingFieldName [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
47+ expect ( faultyObjectMissingValue ) . toHaveLength ( 1 ) ;
48+ expect ( faultyObjectMissingValue [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
49+ expect ( faultyObjectMissingOperator ) . toHaveLength ( 1 ) ;
50+ expect ( faultyObjectMissingOperator [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
51+ } ) ;
52+
53+ it ( 'build() returns an array with a NullCondition fieldName doesnt exist in class param fields (fieldsObject)' , ( ) => {
54+ const faultyItemType = conditionBuilder . build ( [ [ createConditionSet ( 'unknownField' , 1 , '==' ) ] ] ) ;
55+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
56+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
57+ } ) ;
58+
59+ it ( 'build() returns an array with an OrCondition if one valid item is provided' , ( ) => {
60+ const faultyItemType = conditionBuilder . build ( [ [ createConditionSet ( ) ] ] ) ;
61+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
62+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( OrCondition ) ;
63+ } ) ;
64+
65+ it ( 'build() returns an array with an AndConditions if more than one valid item is provided' , ( ) => {
66+ const faultyItemType = conditionBuilder . build ( [ [ createConditionSet ( ) , createConditionSet ( ) ] ] ) ;
67+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
68+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( AndCondition ) ;
69+ } ) ;
70+
71+ it ( 'build() returns an array with an AndConditions as long as one item is valid' , ( ) => {
72+ const faultyItemType = conditionBuilder . build ( [ [ createConditionSet ( ) , createConditionSet ( 'unknownField' ) ] ] ) ;
73+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
74+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( AndCondition ) ;
75+ } ) ;
76+
77+ it ( 'build() returns an array with a NullCondition if multiple invalid items' , ( ) => {
78+ const faultyItemType = conditionBuilder . build ( [ [ createConditionSet ( 'unknownField' ) , createConditionSet ( 'unknownField' ) ] ] ) ;
79+ expect ( faultyItemType ) . toHaveLength ( 1 ) ;
80+ expect ( faultyItemType [ 0 ] ) . toBeInstanceOf ( NullCondition ) ;
81+ } ) ;
82+ } ) ;
83+
84+ function createConditionSet ( field : any = 'testField' , value : any = 1 , operator : any = '==' ) {
85+ return {
86+ field : field ,
87+ value : value ,
88+ operator : operator ,
89+ } ;
90+ }
0 commit comments