test: add auth service tests
Made-with: Cursor
This commit is contained in:
@@ -10,10 +10,52 @@ export type MockDb = {
|
||||
delete: ReturnType<NodePgDatabase<typeof schema>['delete']>;
|
||||
};
|
||||
|
||||
/** Build a select chain that resolves to the given rows at .limit(n) */
|
||||
export function selectChain(resolveAtLimit: unknown[] = []) {
|
||||
const limitFn = vi.fn().mockResolvedValue(resolveAtLimit);
|
||||
return {
|
||||
from: vi.fn().mockReturnValue({
|
||||
where: vi.fn().mockReturnValue({
|
||||
limit: limitFn,
|
||||
orderBy: vi.fn().mockReturnValue({ limit: limitFn }),
|
||||
}),
|
||||
limit: limitFn,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/** Build an insert chain that resolves at .returning() or .values() */
|
||||
export function insertChain(resolveAtReturning: unknown[] = []) {
|
||||
const returningFn = vi.fn().mockResolvedValue(resolveAtReturning);
|
||||
const chainFromValues = {
|
||||
returning: returningFn,
|
||||
then: (resolve: (v?: unknown) => void) => resolve(undefined),
|
||||
};
|
||||
return {
|
||||
values: vi.fn().mockReturnValue(chainFromValues),
|
||||
returning: returningFn,
|
||||
};
|
||||
}
|
||||
|
||||
/** Build an update chain that resolves at .where() */
|
||||
export function updateChain(resolveAtWhere: unknown[] = []) {
|
||||
return {
|
||||
set: vi.fn().mockReturnValue({
|
||||
where: vi.fn().mockResolvedValue(resolveAtWhere),
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/** Build a delete chain that resolves at .where() */
|
||||
export function deleteChain() {
|
||||
return {
|
||||
where: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a chainable mock for Drizzle DB operations.
|
||||
* Configure chain terminals (limit, returning) to resolve to desired values.
|
||||
* Example: mockDb.select.mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockReturnValue({ limit: vi.fn().mockResolvedValue([user]) }) }) })
|
||||
* Use mockReturnValue with selectChain/insertChain/updateChain/deleteChain.
|
||||
*/
|
||||
export function createMockDb(): MockDb {
|
||||
const chain = {
|
||||
|
||||
Reference in New Issue
Block a user