|
1 | 1 | jest.mock("mendix/filters/builders");
|
2 | 2 |
|
3 |
| -import { AndCondition } from "mendix/filters"; |
| 3 | +import { AndCondition, FilterCondition } from "mendix/filters"; |
4 | 4 | import { equals, literal } from "mendix/filters/builders";
|
5 |
| -import { compactArray, fromCompactArray, reduceMap, restoreMap, tag } from "../condition-utils"; |
| 5 | +import { |
| 6 | + compactArray, |
| 7 | + fromCompactArray, |
| 8 | + reduceArray, |
| 9 | + reduceMap, |
| 10 | + restoreArray, |
| 11 | + restoreMap, |
| 12 | + tag |
| 13 | +} from "../condition-utils"; |
6 | 14 |
|
7 | 15 | describe("condition-utils", () => {
|
8 | 16 | describe("compactArray", () => {
|
@@ -265,4 +273,194 @@ describe("condition-utils", () => {
|
265 | 273 | });
|
266 | 274 | });
|
267 | 275 | });
|
| 276 | + |
| 277 | + describe("reduceArray", () => { |
| 278 | + it("returns undefined condition and correct metadata for array with undefined values", () => { |
| 279 | + const input = [undefined, undefined, undefined]; |
| 280 | + const [condition, metadata] = reduceArray(input); |
| 281 | + |
| 282 | + expect(condition).toBeUndefined(); |
| 283 | + |
| 284 | + const parsedMeta = JSON.parse(metadata); |
| 285 | + expect(parsedMeta).toEqual([3, []]); |
| 286 | + }); |
| 287 | + |
| 288 | + it("returns single condition and correct metadata for array with one condition", () => { |
| 289 | + const tagCondition = tag("test"); |
| 290 | + const input = [undefined, tagCondition, undefined]; |
| 291 | + const [condition, metadata] = reduceArray(input); |
| 292 | + |
| 293 | + expect(condition).toBe(tagCondition); |
| 294 | + |
| 295 | + const parsedMeta = JSON.parse(metadata); |
| 296 | + expect(parsedMeta).toEqual([3, [1]]); |
| 297 | + }); |
| 298 | + |
| 299 | + it("returns 'and' condition and correct metadata for array with multiple conditions", () => { |
| 300 | + const tagCondition1 = tag("test1"); |
| 301 | + const tagCondition2 = tag("test2"); |
| 302 | + const input = [tagCondition1, undefined, tagCondition2, undefined]; |
| 303 | + const [condition, metadata] = reduceArray(input); |
| 304 | + |
| 305 | + expect(condition).toMatchObject({ name: "and", type: "function" }); |
| 306 | + expect((condition as AndCondition).args).toHaveLength(2); |
| 307 | + expect((condition as AndCondition).args[0]).toBe(tagCondition1); |
| 308 | + expect((condition as AndCondition).args[1]).toBe(tagCondition2); |
| 309 | + |
| 310 | + const parsedMeta = JSON.parse(metadata); |
| 311 | + expect(parsedMeta).toEqual([4, [0, 2]]); |
| 312 | + }); |
| 313 | + |
| 314 | + it("returns undefined condition for empty array", () => { |
| 315 | + const input: Array<FilterCondition | undefined> = []; |
| 316 | + const [condition, metadata] = reduceArray(input); |
| 317 | + |
| 318 | + expect(condition).toBeUndefined(); |
| 319 | + |
| 320 | + const parsedMeta = JSON.parse(metadata); |
| 321 | + expect(parsedMeta).toEqual([0, []]); |
| 322 | + }); |
| 323 | + |
| 324 | + it("handles array with mixed condition types", () => { |
| 325 | + const tagCondition = tag("tag-test"); |
| 326 | + const equalsCondition = equals(literal("field"), literal("value")); |
| 327 | + const input = [tagCondition, undefined, equalsCondition, undefined, undefined]; |
| 328 | + const [condition, metadata] = reduceArray(input); |
| 329 | + |
| 330 | + expect(condition).toMatchObject({ name: "and", type: "function" }); |
| 331 | + expect((condition as AndCondition).args).toHaveLength(2); |
| 332 | + expect((condition as AndCondition).args[0]).toBe(tagCondition); |
| 333 | + expect((condition as AndCondition).args[1]).toBe(equalsCondition); |
| 334 | + |
| 335 | + const parsedMeta = JSON.parse(metadata); |
| 336 | + expect(parsedMeta).toEqual([5, [0, 2]]); |
| 337 | + }); |
| 338 | + |
| 339 | + it("handles array with single condition at beginning", () => { |
| 340 | + const tagCondition = tag("first"); |
| 341 | + const input = [tagCondition, undefined, undefined]; |
| 342 | + const [condition, metadata] = reduceArray(input); |
| 343 | + |
| 344 | + expect(condition).toBe(tagCondition); |
| 345 | + |
| 346 | + const parsedMeta = JSON.parse(metadata); |
| 347 | + expect(parsedMeta).toEqual([3, [0]]); |
| 348 | + }); |
| 349 | + |
| 350 | + it("handles array with single condition at end", () => { |
| 351 | + const tagCondition = tag("last"); |
| 352 | + const input = [undefined, undefined, tagCondition]; |
| 353 | + const [condition, metadata] = reduceArray(input); |
| 354 | + |
| 355 | + expect(condition).toBe(tagCondition); |
| 356 | + |
| 357 | + const parsedMeta = JSON.parse(metadata); |
| 358 | + expect(parsedMeta).toEqual([3, [2]]); |
| 359 | + }); |
| 360 | + }); |
| 361 | + |
| 362 | + describe("restoreArray", () => { |
| 363 | + it("restores array with undefined values from undefined condition", () => { |
| 364 | + const originalInput = [undefined, undefined, undefined]; |
| 365 | + const [condition, metadata] = reduceArray(originalInput); |
| 366 | + |
| 367 | + const restored = restoreArray(condition, metadata); |
| 368 | + |
| 369 | + expect(restored).toEqual(originalInput); |
| 370 | + }); |
| 371 | + |
| 372 | + it("restores array with single condition", () => { |
| 373 | + const tagCondition = tag("test"); |
| 374 | + const originalInput = [undefined, tagCondition, undefined]; |
| 375 | + const [condition, metadata] = reduceArray(originalInput); |
| 376 | + |
| 377 | + const restored = restoreArray(condition, metadata); |
| 378 | + |
| 379 | + expect(restored).toEqual(originalInput); |
| 380 | + }); |
| 381 | + |
| 382 | + it("restores array with multiple conditions", () => { |
| 383 | + const tagCondition1 = tag("test1"); |
| 384 | + const tagCondition2 = tag("test2"); |
| 385 | + const originalInput = [tagCondition1, undefined, tagCondition2, undefined]; |
| 386 | + const [condition, metadata] = reduceArray(originalInput); |
| 387 | + |
| 388 | + const restored = restoreArray(condition, metadata); |
| 389 | + |
| 390 | + expect(restored).toEqual(originalInput); |
| 391 | + }); |
| 392 | + |
| 393 | + it("restores empty array", () => { |
| 394 | + const originalInput: Array<FilterCondition | undefined> = []; |
| 395 | + const [condition, metadata] = reduceArray(originalInput); |
| 396 | + |
| 397 | + const restored = restoreArray(condition, metadata); |
| 398 | + |
| 399 | + expect(restored).toEqual(originalInput); |
| 400 | + }); |
| 401 | + |
| 402 | + it("restores array with mixed condition types", () => { |
| 403 | + const tagCondition = tag("tag-test"); |
| 404 | + const equalsCondition = equals(literal("field"), literal("value")); |
| 405 | + const originalInput = [tagCondition, undefined, equalsCondition, undefined, undefined]; |
| 406 | + const [condition, metadata] = reduceArray(originalInput); |
| 407 | + |
| 408 | + const restored = restoreArray(condition, metadata); |
| 409 | + |
| 410 | + expect(restored).toEqual(originalInput); |
| 411 | + }); |
| 412 | + |
| 413 | + it("handles manual metadata for single condition case", () => { |
| 414 | + const tagCondition = tag("manual-test"); |
| 415 | + const metadata = JSON.stringify([4, [2]]); |
| 416 | + |
| 417 | + const restored = restoreArray(tagCondition, metadata); |
| 418 | + |
| 419 | + expect(restored).toEqual([undefined, undefined, tagCondition, undefined]); |
| 420 | + }); |
| 421 | + |
| 422 | + it("handles manual metadata for multiple conditions case", () => { |
| 423 | + const tagCondition1 = tag("manual1"); |
| 424 | + const tagCondition2 = tag("manual2"); |
| 425 | + const andCondition = { |
| 426 | + name: "and", |
| 427 | + type: "function", |
| 428 | + args: [tagCondition1, tagCondition2] |
| 429 | + } as AndCondition; |
| 430 | + const metadata = JSON.stringify([5, [1, 3]]); |
| 431 | + |
| 432 | + const restored = restoreArray(andCondition, metadata); |
| 433 | + |
| 434 | + expect(restored).toEqual([undefined, tagCondition1, undefined, tagCondition2, undefined]); |
| 435 | + }); |
| 436 | + |
| 437 | + it("handles edge case with undefined condition and non-empty indexes", () => { |
| 438 | + const metadata = JSON.stringify([3, [1]]); |
| 439 | + |
| 440 | + const restored = restoreArray(undefined, metadata); |
| 441 | + |
| 442 | + expect(restored).toEqual([undefined, undefined, undefined]); |
| 443 | + }); |
| 444 | + |
| 445 | + it("handles edge case with non-and condition but multiple indexes", () => { |
| 446 | + const tagCondition = tag("single"); |
| 447 | + const metadata = JSON.stringify([4, [0, 2]]); |
| 448 | + |
| 449 | + const restored = restoreArray(tagCondition, metadata); |
| 450 | + |
| 451 | + expect(restored).toEqual([undefined, undefined, undefined, undefined]); |
| 452 | + }); |
| 453 | + |
| 454 | + it("round-trip test with complex array", () => { |
| 455 | + const tag1 = tag("one"); |
| 456 | + const tag2 = tag("two"); |
| 457 | + const eq1 = equals(literal("a"), literal("b")); |
| 458 | + const originalInput = [tag1, undefined, eq1, undefined, tag2, undefined, undefined]; |
| 459 | + |
| 460 | + const [condition, metadata] = reduceArray(originalInput); |
| 461 | + const restored = restoreArray(condition, metadata); |
| 462 | + |
| 463 | + expect(restored).toEqual(originalInput); |
| 464 | + }); |
| 465 | + }); |
268 | 466 | });
|
0 commit comments