@@ -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 , reduceMap , tag } from "../condition-utils" ;
5
+ import { compactArray , fromCompactArray , reduceMap , restoreMap , tag } from "../condition-utils" ;
6
6
7
7
describe ( "condition-utils" , ( ) => {
8
8
describe ( "compactArray" , ( ) => {
@@ -135,4 +135,134 @@ describe("condition-utils", () => {
135
135
} ) ;
136
136
} ) ;
137
137
} ) ;
138
+
139
+ describe ( "restoreMap" , ( ) => {
140
+ it ( "restores map with undefined values from undefined condition" , ( ) => {
141
+ const originalInput = { x : undefined , y : undefined } ;
142
+ const [ condition , metadata ] = reduceMap ( originalInput ) ;
143
+
144
+ const restored = restoreMap ( condition , metadata ) ;
145
+
146
+ expect ( restored ) . toEqual ( originalInput ) ;
147
+ } ) ;
148
+
149
+ it ( "restores map with single condition" , ( ) => {
150
+ const tagCondition = tag ( "test" ) ;
151
+ const originalInput = { a : tagCondition , b : undefined , c : undefined } ;
152
+ const [ condition , metadata ] = reduceMap ( originalInput ) ;
153
+
154
+ const restored = restoreMap ( condition , metadata ) ;
155
+
156
+ expect ( restored ) . toEqual ( originalInput ) ;
157
+ } ) ;
158
+
159
+ it ( "restores map with multiple conditions" , ( ) => {
160
+ const tagCondition1 = tag ( "test1" ) ;
161
+ const tagCondition2 = tag ( "test2" ) ;
162
+ const originalInput = { x : tagCondition1 , y : undefined , z : tagCondition2 , w : undefined } ;
163
+ const [ condition , metadata ] = reduceMap ( originalInput ) ;
164
+
165
+ const restored = restoreMap ( condition , metadata ) ;
166
+
167
+ expect ( restored ) . toEqual ( originalInput ) ;
168
+ } ) ;
169
+
170
+ it ( "restores empty map" , ( ) => {
171
+ const originalInput = { } ;
172
+ const [ condition , metadata ] = reduceMap ( originalInput ) ;
173
+
174
+ const restored = restoreMap ( condition , metadata ) ;
175
+
176
+ expect ( restored ) . toEqual ( originalInput ) ;
177
+ } ) ;
178
+
179
+ it ( "restores map with mixed condition types" , ( ) => {
180
+ const tagCondition = tag ( "tag-test" ) ;
181
+ const equalsCondition = equals ( literal ( "field" ) , literal ( "value" ) ) ;
182
+ const originalInput = {
183
+ tag : tagCondition ,
184
+ equals : equalsCondition ,
185
+ empty : undefined ,
186
+ another : undefined
187
+ } ;
188
+ const [ condition , metadata ] = reduceMap ( originalInput ) ;
189
+
190
+ const restored = restoreMap ( condition , metadata ) ;
191
+
192
+ expect ( restored ) . toEqual ( originalInput ) ;
193
+ } ) ;
194
+
195
+ it ( "handles manual metadata for single condition case" , ( ) => {
196
+ const tagCondition = tag ( "manual-test" ) ;
197
+ const metadata = JSON . stringify ( {
198
+ length : 1 ,
199
+ keys : [ "first" , "second" , "third" ] ,
200
+ 0 : "second"
201
+ } ) ;
202
+
203
+ const restored = restoreMap ( tagCondition , metadata ) ;
204
+
205
+ expect ( restored ) . toEqual ( {
206
+ first : undefined ,
207
+ second : tagCondition ,
208
+ third : undefined
209
+ } ) ;
210
+ } ) ;
211
+
212
+ it ( "handles manual metadata for multiple conditions case" , ( ) => {
213
+ const tagCondition1 = tag ( "manual1" ) ;
214
+ const tagCondition2 = tag ( "manual2" ) ;
215
+ const andCondition = {
216
+ name : "and" ,
217
+ type : "function" ,
218
+ args : [ tagCondition1 , tagCondition2 ]
219
+ } as AndCondition ;
220
+ const metadata = JSON . stringify ( {
221
+ length : 2 ,
222
+ keys : [ "a" , "b" , "c" , "d" ] ,
223
+ 0 : "a" ,
224
+ 1 : "c"
225
+ } ) ;
226
+
227
+ const restored = restoreMap ( andCondition , metadata ) ;
228
+
229
+ expect ( restored ) . toEqual ( {
230
+ a : tagCondition1 ,
231
+ b : undefined ,
232
+ c : tagCondition2 ,
233
+ d : undefined
234
+ } ) ;
235
+ } ) ;
236
+
237
+ it ( "handles edge case with undefined condition and non-zero length metadata" , ( ) => {
238
+ const metadata = JSON . stringify ( {
239
+ length : 1 ,
240
+ keys : [ "test" ] ,
241
+ 0 : "test"
242
+ } ) ;
243
+
244
+ const restored = restoreMap ( undefined , metadata ) ;
245
+
246
+ expect ( restored ) . toEqual ( {
247
+ test : undefined
248
+ } ) ;
249
+ } ) ;
250
+
251
+ it ( "handles edge case with non-and condition but multiple length metadata" , ( ) => {
252
+ const tagCondition = tag ( "single" ) ;
253
+ const metadata = JSON . stringify ( {
254
+ length : 2 ,
255
+ keys : [ "a" , "b" ] ,
256
+ 0 : "a" ,
257
+ 1 : "b"
258
+ } ) ;
259
+
260
+ const restored = restoreMap ( tagCondition , metadata ) ;
261
+
262
+ expect ( restored ) . toEqual ( {
263
+ a : undefined ,
264
+ b : undefined
265
+ } ) ;
266
+ } ) ;
267
+ } ) ;
138
268
} ) ;
0 commit comments