test(shared): add mock supabase client helper for auth unit tests
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
import { vi } from 'vitest';
|
||||||
|
import type { AppSupabaseClient } from '../../supabase/client';
|
||||||
|
|
||||||
|
export interface MockedRpcCall { name: string; params: unknown }
|
||||||
|
|
||||||
|
export interface MockClient {
|
||||||
|
client: AppSupabaseClient;
|
||||||
|
rpcCalls: MockedRpcCall[];
|
||||||
|
setRpcResponse: (name: string, response: { data?: unknown; error?: unknown }) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeMockClient(initialUserId = '11111111-1111-1111-1111-111111111111'): MockClient {
|
||||||
|
const rpcCalls: MockedRpcCall[] = [];
|
||||||
|
const rpcResponses = new Map<string, { data?: unknown; error?: unknown }>();
|
||||||
|
const client = {
|
||||||
|
auth: {
|
||||||
|
getUser: vi.fn().mockResolvedValue({ data: { user: { id: initialUserId } }, error: null }),
|
||||||
|
},
|
||||||
|
rpc: vi.fn().mockImplementation((name: string, params: unknown) => {
|
||||||
|
rpcCalls.push({ name, params });
|
||||||
|
const r = rpcResponses.get(name) ?? { data: null, error: null };
|
||||||
|
return Promise.resolve(r);
|
||||||
|
}),
|
||||||
|
} as unknown as AppSupabaseClient;
|
||||||
|
return {
|
||||||
|
client,
|
||||||
|
rpcCalls,
|
||||||
|
setRpcResponse: (name, response) => rpcResponses.set(name, response),
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user