@@ -2,7 +2,7 @@ jest.mock("mendix/filters/builders");
2
2
3
3
import { AndCondition } from "mendix/filters" ;
4
4
import { equals , literal } from "mendix/filters/builders" ;
5
- import { compactArray , fromCompactArray , tag } from "../condition-utils" ;
5
+ import { compactArray , fromCompactArray , reduceMap , tag } from "../condition-utils" ;
6
6
7
7
describe ( "condition-utils" , ( ) => {
8
8
describe ( "compactArray" , ( ) => {
@@ -47,4 +47,92 @@ describe("condition-utils", () => {
47
47
expect ( result ) . toEqual ( input ) ;
48
48
} ) ;
49
49
} ) ;
50
+
51
+ describe ( "reduceMap" , ( ) => {
52
+ it ( "returns undefined condition and correct metadata for map with undefined values" , ( ) => {
53
+ const input = { x : undefined , y : undefined } ;
54
+ const [ condition , metadata ] = reduceMap ( input ) ;
55
+
56
+ expect ( condition ) . toBeUndefined ( ) ;
57
+
58
+ const parsedMeta = JSON . parse ( metadata ) ;
59
+ expect ( parsedMeta ) . toEqual ( {
60
+ length : 0 ,
61
+ keys : [ "x" , "y" ]
62
+ } ) ;
63
+ } ) ;
64
+
65
+ it ( "returns single condition and correct metadata for map with one condition" , ( ) => {
66
+ const tagCondition = tag ( "test" ) ;
67
+ const input = { a : tagCondition , b : undefined , c : undefined } ;
68
+ const [ condition , metadata ] = reduceMap ( input ) ;
69
+
70
+ expect ( condition ) . toBe ( tagCondition ) ;
71
+
72
+ const parsedMeta = JSON . parse ( metadata ) ;
73
+ expect ( parsedMeta ) . toEqual ( {
74
+ length : 1 ,
75
+ keys : [ "a" , "b" , "c" ] ,
76
+ 0 : "a"
77
+ } ) ;
78
+ } ) ;
79
+
80
+ it ( "returns 'and' condition and correct metadata for map with multiple conditions" , ( ) => {
81
+ const tagCondition1 = tag ( "test1" ) ;
82
+ const tagCondition2 = tag ( "test2" ) ;
83
+ const input = { x : tagCondition1 , y : undefined , z : tagCondition2 , w : undefined } ;
84
+ const [ condition , metadata ] = reduceMap ( input ) ;
85
+
86
+ expect ( condition ) . toMatchObject ( { name : "and" , type : "function" } ) ;
87
+ expect ( ( condition as AndCondition ) . args ) . toHaveLength ( 2 ) ;
88
+ expect ( ( condition as AndCondition ) . args [ 0 ] ) . toBe ( tagCondition1 ) ;
89
+ expect ( ( condition as AndCondition ) . args [ 1 ] ) . toBe ( tagCondition2 ) ;
90
+
91
+ const parsedMeta = JSON . parse ( metadata ) ;
92
+ expect ( parsedMeta ) . toEqual ( {
93
+ length : 2 ,
94
+ keys : [ "x" , "y" , "z" , "w" ] ,
95
+ 0 : "x" ,
96
+ 1 : "z"
97
+ } ) ;
98
+ } ) ;
99
+
100
+ it ( "returns undefined condition for empty map" , ( ) => {
101
+ const input = { } ;
102
+ const [ condition , metadata ] = reduceMap ( input ) ;
103
+
104
+ expect ( condition ) . toBeUndefined ( ) ;
105
+
106
+ const parsedMeta = JSON . parse ( metadata ) ;
107
+ expect ( parsedMeta ) . toEqual ( {
108
+ length : 0 ,
109
+ keys : [ ]
110
+ } ) ;
111
+ } ) ;
112
+
113
+ it ( "handles map with mixed condition types" , ( ) => {
114
+ const tagCondition = tag ( "tag-test" ) ;
115
+ const equalsCondition = equals ( literal ( "field" ) , literal ( "value" ) ) ;
116
+ const input = {
117
+ tag : tagCondition ,
118
+ equals : equalsCondition ,
119
+ empty : undefined ,
120
+ another : undefined
121
+ } ;
122
+ const [ condition , metadata ] = reduceMap ( input ) ;
123
+
124
+ expect ( condition ) . toMatchObject ( { name : "and" , type : "function" } ) ;
125
+ expect ( ( condition as AndCondition ) . args ) . toHaveLength ( 2 ) ;
126
+ expect ( ( condition as AndCondition ) . args [ 0 ] ) . toBe ( tagCondition ) ;
127
+ expect ( ( condition as AndCondition ) . args [ 1 ] ) . toBe ( equalsCondition ) ;
128
+
129
+ const parsedMeta = JSON . parse ( metadata ) ;
130
+ expect ( parsedMeta ) . toEqual ( {
131
+ length : 2 ,
132
+ keys : [ "tag" , "equals" , "empty" , "another" ] ,
133
+ 0 : "tag" ,
134
+ 1 : "equals"
135
+ } ) ;
136
+ } ) ;
137
+ } ) ;
50
138
} ) ;
0 commit comments