Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cooper Testing is CURSED #94

Merged
merged 10 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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