-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added test for get-grant-calls
- Loading branch information
1 parent
ddc0aa5
commit 11a3705
Showing
5 changed files
with
110 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { z } from "zod"; | ||
|
||
export const querySchema = z | ||
.object({ | ||
userId: z.string().optional(), | ||
limit: z.number().int().min(0).optional(), | ||
offset: z.number().int().min(0).optional(), | ||
status: z.enum(["open", "closed", "draft"]).optional(), | ||
focusArea: z.string().optional(), | ||
minAmount: z.number().min(0).optional(), | ||
maxAmount: z.number().min(0).optional(), | ||
}) | ||
.refine( | ||
(data) => | ||
!data.minAmount || !data.maxAmount || data.maxAmount >= data.minAmount, | ||
{ message: "maxAmount must be greater than or equal to minAmount" }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { querySchema } from "../schemas/grant-calls.schema"; | ||
|
||
interface QueryParams { | ||
userId?: string; | ||
limit?: number; | ||
offset?: number; | ||
status?: "open" | "closed" | "draft"; | ||
focusArea?: string; | ||
minAmount?: number; | ||
maxAmount?: number; | ||
} | ||
|
||
// Test factories | ||
function createValidQuery(overrides?: Partial<QueryParams>): QueryParams { | ||
return { | ||
userId: "123", | ||
limit: 10, | ||
offset: 0, | ||
status: "open", | ||
focusArea: "tech", | ||
minAmount: 100, | ||
maxAmount: 1000, | ||
...overrides, | ||
}; | ||
} | ||
|
||
describe("Query Schema Validation", () => { | ||
describe("valid queries", () => { | ||
it("should parse a complete valid query", () => { | ||
const validQuery = createValidQuery(); | ||
const result = querySchema.parse(validQuery); | ||
expect(result).toEqual(validQuery); | ||
}); | ||
|
||
it("should allow optional fields to be omitted", () => { | ||
const validQuery = { userId: "456" }; | ||
const result = querySchema.parse(validQuery); | ||
expect(result).toEqual(validQuery); | ||
}); | ||
|
||
it("should accept zero values for numeric fields", () => { | ||
const validQuery = createValidQuery({ minAmount: 0, maxAmount: 0 }); | ||
const result = querySchema.parse(validQuery); | ||
expect(result).toEqual(validQuery); | ||
}); | ||
}); | ||
|
||
describe("invalid queries", () => { | ||
it("should throw an error for invalid enum value", () => { | ||
const invalidQuery = { status: "invalidStatus" }; | ||
|
||
expect(() => querySchema.parse(invalidQuery)).toThrow( | ||
/Invalid enum value/, | ||
); | ||
}); | ||
|
||
it("should throw an error for invalid data types", () => { | ||
const invalidQuery = { limit: "10" }; | ||
|
||
expect(() => querySchema.parse(invalidQuery)).toThrow( | ||
"Expected number, received string", | ||
); | ||
}); | ||
|
||
it("should throw an error for negative amounts", () => { | ||
expect(() => querySchema.parse({ minAmount: -100 })).toThrow(); | ||
expect(() => querySchema.parse({ maxAmount: -50 })).toThrow(); | ||
}); | ||
|
||
it("should throw when maxAmount is less than minAmount", () => { | ||
const invalidQuery = createValidQuery({ | ||
minAmount: 1000, | ||
maxAmount: 100, | ||
}); | ||
|
||
expect(() => querySchema.parse(invalidQuery)).toThrow( | ||
"maxAmount must be greater than or equal to minAmount", | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
}, | ||
}); |