diff options
| author | MohamedBassem <me@mbassem.com> | 2024-02-28 15:43:32 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-02-28 15:43:32 +0000 |
| commit | f67ae821230da9bc92a3c9ff6c550a36d48c0ee9 (patch) | |
| tree | 75ff0d4e07bd066d3acc7e7cfa6ef126ea0eebc7 /packages/web/lib | |
| parent | 0f0e7ca8d134c2cfc02ac62539ad10c811319b38 (diff) | |
| download | karakeep-f67ae821230da9bc92a3c9ff6c550a36d48c0ee9.tar.zst | |
tests: Add tests for the bookmarks routes
Diffstat (limited to 'packages/web/lib')
| -rw-r--r-- | packages/web/lib/testUtils.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/packages/web/lib/testUtils.ts b/packages/web/lib/testUtils.ts new file mode 100644 index 00000000..ca9a6474 --- /dev/null +++ b/packages/web/lib/testUtils.ts @@ -0,0 +1,59 @@ +import { users } from "@hoarder/db/schema"; +import { getInMemoryDB } from "@hoarder/db/drizzle"; +import { appRouter } from "@/server/api/routers/_app"; +import { createCallerFactory } from "@/server/api/trpc"; +import { beforeEach } from "vitest"; + +export function getTestDB() { + return getInMemoryDB(true); +} + +export type TestDB = ReturnType<typeof getTestDB>; + +export async function seedUsers(db: TestDB) { + return await db + .insert(users) + .values([ + { + name: "Test User 1", + email: "test1@test.com", + }, + { + name: "Test User 2", + email: "test2@test.com", + }, + ]) + .returning(); +} + +export function getApiCaller(db: TestDB, userId: string) { + const createCaller = createCallerFactory(appRouter); + return createCaller({ + user: { + id: userId, + }, + db, + }); +} + +export type APICallerType = ReturnType<typeof getApiCaller>; + +export interface CustomTestContext { + apiCallers: APICallerType[]; + db: TestDB; +} + +export async function buildTestContext(): Promise<CustomTestContext> { + const db = getTestDB(); + const users = await seedUsers(db); + const callers = users.map((u) => getApiCaller(db, u.id)); + + return { + apiCallers: callers, + db, + }; +} + +export const defaultBeforeEach = async (context: object) => { + Object.assign(context, await buildTestContext()); +}; |
