diff --git a/packages/api/tests/company.test.ts b/packages/api/tests/company.test.ts index 18314009..e998cc0e 100644 --- a/packages/api/tests/company.test.ts +++ b/packages/api/tests/company.test.ts @@ -1,80 +1,106 @@ -// /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -// /* eslint-disable @typescript-eslint/unbound-method */ -// import { beforeEach, describe, expect, test, vi } from "vitest"; - -import { describe, expect, test } from "vitest"; - -// import type { Session } from "@cooper/auth"; -// import type { CompanyType } from "@cooper/db/schema"; -// import { and, eq } from "@cooper/db"; -// import { db } from "@cooper/db/client"; -// import { Company } from "@cooper/db/schema"; - -// import { appRouter } from "../src/root"; -// import { createCallerFactory, createTRPCContext } from "../src/trpc"; -// import { data } from "./mocks/company"; - -// vi.mock("@cooper/db/client", () => ({ -// db: { -// query: { -// Review: { -// findMany: vi.fn(), -// }, -// }, -// }, -// })); - -// vi.mock("@cooper/auth", () => ({ -// auth: vi.fn(), -// })); - -// describe("Company Router", async () => { -// beforeEach(() => { -// vi.restoreAllMocks(); -// vi.mocked(db.query.Company.findMany).mockResolvedValue(data as CompanyType[]); -// }); - -// 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 companies", async () => { -// const companies = await caller.review.list({}); - -// expect(companies).toEqual(data); - -// expect(db.query.Review.findMany).toHaveBeenCalledWith({ -// orderBy: expect.anything(), -// where: undefined, -// }); -// }); - -// test("list endpoint with filtered companies", async () => { -// await caller.review.list({ -// search: "kin" -// }); - -// expect(db.query.Review.findMany).toHaveBeenCalledWith({ -// orderBy: expect.anything(), -// where: and(eq(Review.workTerm, "SPRING")), -// }); -// }); - -// }); - -describe("Company Router", () => { - test("github action bandaage", () => { - expect(true).toBe(true); +import { beforeEach, describe, expect, test, vi } from "vitest"; + +import type { Session } from "@cooper/auth"; +import type { CompanyType, ReviewType } from "@cooper/db/schema"; +import { and, eq, inArray } from "@cooper/db"; +import { db } from "@cooper/db/client"; +import { Company, Review } from "@cooper/db/schema"; + +import { appRouter } from "../src/root"; +import { createCallerFactory, createTRPCContext } from "../src/trpc"; +import { data } from "./mocks/review"; + +const chain: any = { + from: vi.fn(() => chain), + innerJoin: vi.fn(() => chain), + where: vi.fn(() => chain), +}; + +vi.mock("@cooper/db/client", () => ({ + db: { + select: vi.fn(() => chain), + execute: vi.fn(), + query: { + Review: { + findMany: vi.fn(), + findFirst: vi.fn(), + }, + Company: { + findMany: vi.fn(), + findFirst: vi.fn(), + }, + }, + }, +})); + +vi.mock("@cooper/auth", () => ({ + auth: vi.fn(), +})); + +describe("Company 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 companies", async () => { + // const companies = await caller.company.list({}); + + // expect(companies).toEqual(data); + + // expect(db.query.Company.findMany).toHaveBeenCalledWith({ + // orderBy: expect.anything(), + // where: undefined, + // }); + // }) + + test("getByName endpoint returns company by name", async () => { + const companyName = "Test Company"; + await caller.company.getByName({ name: companyName }); + + expect(db.query.Company.findFirst).toHaveBeenCalledWith({ + where: eq(Company.name, companyName), + }); + }); + + test("getById endpoint returns company by id", async () => { + const companyId = "123"; + await caller.company.getById({ id: companyId }); + + expect(db.query.Company.findFirst).toHaveBeenCalledWith({ + where: eq(Company.id, "123"), + }); + }); + + // test("getLocationsById endpoint returns locations by company id", async () => { + // const companyId = "123"; + // await caller.company.getLocationsById({ id: companyId }); + + // expect(db.query.Company.findFirst).toHaveBeenCalledWith({ + // where: eq(Company.id, "123"), + // }); + // }) + + test("getAverageById endpoint returns average ratings by company id", async () => { + const companyId = "123"; + await caller.company.getAverageById({ companyId }); + + expect(db.query.Review.findMany).toHaveBeenCalledWith({ + where: eq(Review.companyId, companyId), + }); }); - // Add your tests here }); diff --git a/packages/api/tests/review.test.ts b/packages/api/tests/review.test.ts index 7ba690c1..5effa26a 100644 --- a/packages/api/tests/review.test.ts +++ b/packages/api/tests/review.test.ts @@ -103,6 +103,33 @@ describe("Review Router", async () => { }); }); + test("getByRole endpoint returns reviews by role", async () => { + const roleId = "role-123"; + await caller.review.getByRole({ id: roleId }); + + expect(db.query.Review.findMany).toHaveBeenCalledWith({ + where: eq(Review.roleId, "role-123"), + }); + }); + + test("getByCompany endpoint returns reviews by company", async () => { + const companyId = "company-123"; + await caller.review.getByCompany({ id: companyId }); + + expect(db.query.Review.findMany).toHaveBeenCalledWith({ + where: eq(Review.companyId, "company-123"), + }); + }); + + test("getByProfile endpoint returns reviews by profile", async () => { + const profileId = "profile-123"; + await caller.review.getByProfile({ id: profileId }); + + expect(db.query.Review.findMany).toHaveBeenCalledWith({ + where: eq(Review.profileId, "profile-123"), + }); + }); + test("get average by industry", async () => { vi.resetAllMocks(); const mockHeaders = new Headers();