Skip to content

Commit

Permalink
Cooper Testing is CURSED (#94)
Browse files Browse the repository at this point in the history
* chore: add a mock test

* chore: fix web app linting

* chore: add mock and turbo

* chore: add mocks

* chore: test passing locally fingers crossed

* chore: disable check

* chore: ignore unbound methods

* chore: add another test

* chore: add two more tests

* chore: remove unused import
  • Loading branch information
RishikeshNK authored Nov 23, 2024
1 parent de2260c commit e8a303d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
12 changes: 8 additions & 4 deletions apps/web/src/app/(pages)/(dashboard)/roles/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import { useEffect, useState } from "react";
import { z } from "zod";

import {
import type {
ReviewType,
WorkEnvironment,
WorkEnvironmentType,
WorkTerm,
WorkTermType,
} from "@cooper/db/schema";
import { WorkEnvironment, WorkTerm } from "@cooper/db/schema";
import { cn } from "@cooper/ui";
import { useToast } from "@cooper/ui/hooks/use-toast";

Expand Down Expand Up @@ -60,7 +59,12 @@ export default function Roles({
variant: "destructive",
});
}
}, [toast, mounted]);
}, [
toast,
mounted,
validationResult.success,
validationResult.error?.issues,
]);

const reviews = api.review.list.useQuery({
options: validationResult.success ? validationResult.data : {},
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "vitest",
"clean": "rm -rf .turbo dist node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
Expand Down
5 changes: 5 additions & 0 deletions packages/api/tests/mocks/review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const data = [
{ id: "1", workTerm: "SPRING", workEnvironment: "REMOTE" },
{ id: "2", workTerm: "FALL", workEnvironment: "INPERSON" },
{ id: "3", workTerm: "SUMMER", workEnvironment: "HYBRID" },
];
102 changes: 102 additions & 0 deletions packages/api/tests/review.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/unbound-method */
import { beforeEach, describe, expect, test, vi } from "vitest";

import type { Session } from "@cooper/auth";
import type { ReviewType } from "@cooper/db/schema";
import { and, eq } from "@cooper/db";
import { db } from "@cooper/db/client";
import { Review } from "@cooper/db/schema";

import { appRouter } from "../src/root";
import { createCallerFactory, createTRPCContext } from "../src/trpc";
import { data } from "./mocks/review";

vi.mock("@cooper/db/client", () => ({
db: {
query: {
Review: {
findMany: vi.fn(),
},
},
},
}));

vi.mock("@cooper/auth", () => ({
auth: vi.fn(),
}));

describe("Review Router", async () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.mocked(db.query.Review.findMany).mockResolvedValue(data as ReviewType[]);
});

const session: Session = {
user: {
id: "1",
},
expires: "1",
};

const ctx = await createTRPCContext({
session,
headers: new Headers(),
});

const caller = createCallerFactory(appRouter)(ctx);

test("list endpoint returns all reviews", async () => {
const reviews = await caller.review.list({});

expect(reviews).toEqual(data);

expect(db.query.Review.findMany).toHaveBeenCalledWith({
orderBy: expect.anything(),
where: undefined,
});
});

test("list endpoint with cycle filter", async () => {
await caller.review.list({
options: {
cycle: "SPRING",
},
});

expect(db.query.Review.findMany).toHaveBeenCalledWith({
orderBy: expect.anything(),
where: and(eq(Review.workTerm, "SPRING")),
});
});

test("list endpoint with term filter", async () => {
await caller.review.list({
options: {
term: "REMOTE",
},
});

expect(db.query.Review.findMany).toHaveBeenCalledWith({
orderBy: expect.anything(),
where: and(eq(Review.workEnvironment, "REMOTE")),
});
});

test("list endpoint with cycle and term filter", async () => {
await caller.review.list({
options: {
cycle: "SPRING",
term: "REMOTE",
},
});

expect(db.query.Review.findMany).toHaveBeenCalledWith({
orderBy: expect.anything(),
where: and(
eq(Review.workTerm, "SPRING"),
eq(Review.workEnvironment, "REMOTE"),
),
});
});
});
2 changes: 1 addition & 1 deletion packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"outDir": "dist",
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["src"],
"include": ["src", "tests"],
"exclude": ["node_modules"]
}
4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"start": {
"cache": false,
"interactive": true
},
"test": {
"cache": true,
"interactive": true
}
},
"globalEnv": [
Expand Down

0 comments on commit e8a303d

Please sign in to comment.