Initial Commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Shared test utilities for the auth-context test split. Exposed under
|
||||
* `tests/setup/` so they are excluded from the istanbul coverage scope
|
||||
* (`include: src/**`) while still being available to multiple `.test.tsx`
|
||||
* files inside `src/context/`.
|
||||
*/
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
import {
|
||||
useAuth,
|
||||
useIsAuthenticated,
|
||||
useUser,
|
||||
} from "../../src/context/auth-context.js";
|
||||
import type {
|
||||
AuthContextValue,
|
||||
AuthUser,
|
||||
} from "../../src/types/branding.types.js";
|
||||
|
||||
export interface FetchCall {
|
||||
url: string;
|
||||
init?: RequestInit;
|
||||
}
|
||||
|
||||
export interface AuthCaptureBag {
|
||||
value: AuthContextValue | null;
|
||||
}
|
||||
|
||||
export interface UserCaptureBag {
|
||||
value: AuthUser | null | unknown;
|
||||
}
|
||||
|
||||
export interface AuthedCaptureBag {
|
||||
value: boolean;
|
||||
}
|
||||
|
||||
export function makeFetch(
|
||||
handler: (call: FetchCall) => Response | Promise<Response>
|
||||
): {
|
||||
fetch: (
|
||||
input: string | URL | Request,
|
||||
init?: RequestInit
|
||||
) => Promise<Response>;
|
||||
calls: FetchCall[];
|
||||
} {
|
||||
const calls: FetchCall[] = [];
|
||||
return {
|
||||
calls,
|
||||
fetch: async (input, init) => {
|
||||
const url = typeof input === "string" ? input : input.toString();
|
||||
calls.push({ url, init });
|
||||
return handler({ url, init });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function jsonResponse(body: unknown, init?: ResponseInit): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
...init,
|
||||
});
|
||||
}
|
||||
|
||||
export function CaptureAuth({
|
||||
capture,
|
||||
}: {
|
||||
capture: AuthCaptureBag;
|
||||
}): ReactNode {
|
||||
capture.value = useAuth();
|
||||
return null;
|
||||
}
|
||||
|
||||
export function CaptureUser({
|
||||
capture,
|
||||
}: {
|
||||
capture: UserCaptureBag;
|
||||
}): ReactNode {
|
||||
capture.value = useUser();
|
||||
return null;
|
||||
}
|
||||
|
||||
export function CaptureAuthed({
|
||||
capture,
|
||||
}: {
|
||||
capture: AuthedCaptureBag;
|
||||
}): ReactNode {
|
||||
capture.value = useIsAuthenticated();
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user