Skip to content

Commit

Permalink
feat: added test for get-grant-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquecweiss committed Nov 21, 2024
1 parent ddc0aa5 commit 11a3705
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/jobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"lint": "biome lint ./trigger ./lib",
"dev": "bunx trigger.dev dev",
"format": "biome format --write .",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"test": "vitest"
},
"dependencies": {
"@eda/supabase": "workspace:*",
Expand Down
17 changes: 17 additions & 0 deletions packages/jobs/schemas/grant-calls.schema.ts
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" },
);
82 changes: 82 additions & 0 deletions packages/jobs/tests/get-grant-calls.test.ts
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",
);
});
});
});
13 changes: 2 additions & 11 deletions packages/jobs/trigger/get-grant-calls.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { logger, task } from "@trigger.dev/sdk/v3";
import { z } from "zod";
import type { z } from "zod";
import { supabase } from "../lib/supabase";

const querySchema = z.object({
userId: z.string().optional(),
limit: z.number().optional(),
offset: z.number().optional(),
status: z.enum(["open", "closed", "draft"]).optional(),
focusArea: z.string().optional(),
minAmount: z.number().optional(),
maxAmount: z.number().optional(),
});
import type { querySchema } from "../schemas/grant-calls.schema";

export const getGrantCallsTask = task({
id: "get-grant-calls",
Expand Down
7 changes: 7 additions & 0 deletions packages/jobs/vitest.config.ts
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,
},
});

0 comments on commit 11a3705

Please sign in to comment.