|
| 1 | +import { buildQuery } from "../build-query" |
| 2 | +import { SoftDeletableFilterKey } from "../../dal/mikro-orm/mikro-orm-soft-deletable-filter" |
| 3 | + |
| 4 | +describe("buildQuery", () => { |
| 5 | + test("should return empty where and basic options when no filters or config provided", () => { |
| 6 | + const result = buildQuery() |
| 7 | + expect(result).toEqual({ |
| 8 | + where: {}, |
| 9 | + options: { |
| 10 | + populate: [], |
| 11 | + fields: undefined, |
| 12 | + limit: undefined, |
| 13 | + offset: undefined, |
| 14 | + }, |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + test("should build basic where clause", () => { |
| 19 | + const filters = { name: "test", age: 25 } |
| 20 | + const result = buildQuery(filters) |
| 21 | + expect(result.where).toEqual(filters) |
| 22 | + }) |
| 23 | + |
| 24 | + test("should handle array values in filters", () => { |
| 25 | + const filters = { id: [1, 2, 3, 3] } |
| 26 | + const result = buildQuery(filters) |
| 27 | + expect(result.where).toEqual({ id: [1, 2, 3] }) // Deduplication |
| 28 | + }) |
| 29 | + |
| 30 | + test("should handle nested object filters", () => { |
| 31 | + const filters = { user: { name: "John", age: 30 } } |
| 32 | + const result = buildQuery(filters) |
| 33 | + expect(result.where).toEqual(filters) |
| 34 | + }) |
| 35 | + |
| 36 | + test("should handle $or operator", () => { |
| 37 | + const filters = { $or: [{ name: "John" }, { age: 30 }] } |
| 38 | + const result = buildQuery(filters) |
| 39 | + expect(result.where).toEqual(filters) |
| 40 | + }) |
| 41 | + |
| 42 | + test("should handle $and operator", () => { |
| 43 | + const filters = { $and: [{ name: "John" }, { age: 30 }] } |
| 44 | + const result = buildQuery(filters) |
| 45 | + expect(result.where).toEqual(filters) |
| 46 | + }) |
| 47 | + |
| 48 | + test("should apply config options", () => { |
| 49 | + const config = { |
| 50 | + relations: ["user", "order"], |
| 51 | + select: ["id", "name"], |
| 52 | + take: 10, |
| 53 | + skip: 5, |
| 54 | + order: { createdAt: "DESC" }, |
| 55 | + } |
| 56 | + const result = buildQuery({}, config) |
| 57 | + expect(result.options).toMatchObject({ |
| 58 | + populate: ["user", "order"], |
| 59 | + fields: ["id", "name"], |
| 60 | + limit: 10, |
| 61 | + offset: 5, |
| 62 | + orderBy: { createdAt: "DESC" }, |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + test("should handle withDeleted flag", () => { |
| 67 | + const filters = { deleted_at: "some-value" } |
| 68 | + const result = buildQuery(filters) |
| 69 | + expect(result.options.filters).toHaveProperty(SoftDeletableFilterKey) |
| 70 | + expect(result.options.filters?.[SoftDeletableFilterKey]).toEqual({ |
| 71 | + withDeleted: true, |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + test("should apply additional filters from config", () => { |
| 76 | + const config = { |
| 77 | + filters: { |
| 78 | + customFilter: { someOption: true }, |
| 79 | + }, |
| 80 | + } |
| 81 | + const result = buildQuery({}, config) |
| 82 | + expect(result.options.filters).toHaveProperty("customFilter") |
| 83 | + expect(result.options.filters?.["customFilter"]).toEqual({ |
| 84 | + someOption: true, |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + test("should merge options from config", () => { |
| 89 | + const config = { |
| 90 | + options: { |
| 91 | + customOption: "value", |
| 92 | + }, |
| 93 | + } |
| 94 | + const result = buildQuery({}, config) |
| 95 | + expect(result.options).toHaveProperty("customOption", "value") |
| 96 | + }) |
| 97 | +}) |
0 commit comments