feat(core-consent): add ConsentProvider + useConsent() React subpath
Adds the ./react subpath to @repo/core-consent following the same pattern as @repo/core-analytics/react: - ConsentProvider wraps IConsent in React context - useConsent() returns the injected IConsent or throws ConsentContextError - RTL tests cover context propagation, grant/withdraw delegation, isGranted state reflection, getCategories, and missing-provider error - package.json: ./react export + React optional peerDep + RTL devDeps - tsconfig: extend react-library.json, include .tsx - vitest.config: jsdom environment for .test.tsx + jsdom setup Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
91
packages/core-consent/src/react/consent-provider.test.tsx
Normal file
91
packages/core-consent/src/react/consent-provider.test.tsx
Normal file
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { act, renderHook } from "@testing-library/react";
|
||||
import { RecordingConsent } from "@repo/core-testing";
|
||||
import {
|
||||
ConsentContextError,
|
||||
ConsentProvider,
|
||||
useConsent,
|
||||
} from "@/react/index";
|
||||
|
||||
function makeWrapper(consent: RecordingConsent) {
|
||||
return function Wrapper({ children }: { children: React.ReactNode }) {
|
||||
return <ConsentProvider value={consent}>{children}</ConsentProvider>;
|
||||
};
|
||||
}
|
||||
|
||||
describe("ConsentProvider / useConsent", () => {
|
||||
it("returns the injected IConsent instance", () => {
|
||||
const recording = new RecordingConsent();
|
||||
const { result } = renderHook(() => useConsent(), {
|
||||
wrapper: makeWrapper(recording),
|
||||
});
|
||||
expect(result.current).toBe(recording);
|
||||
});
|
||||
|
||||
it("propagates grant() to the injected IConsent", async () => {
|
||||
const recording = new RecordingConsent();
|
||||
const { result } = renderHook(() => useConsent(), {
|
||||
wrapper: makeWrapper(recording),
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.grant("analytics");
|
||||
});
|
||||
|
||||
expect(recording.grants).toContainEqual({
|
||||
category: "analytics",
|
||||
meta: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("propagates withdraw() to the injected IConsent", async () => {
|
||||
const recording = new RecordingConsent();
|
||||
await recording.grant("marketing");
|
||||
|
||||
const { result } = renderHook(() => useConsent(), {
|
||||
wrapper: makeWrapper(recording),
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.withdraw("marketing");
|
||||
});
|
||||
|
||||
expect(recording.withdrawals).toContain("marketing");
|
||||
});
|
||||
|
||||
it("isGranted() reflects the injected instance state", async () => {
|
||||
const recording = new RecordingConsent();
|
||||
await recording.grant("functional");
|
||||
|
||||
const { result } = renderHook(() => useConsent(), {
|
||||
wrapper: makeWrapper(recording),
|
||||
});
|
||||
|
||||
expect(result.current.isGranted("functional")).toBe(true);
|
||||
expect(result.current.isGranted("marketing")).toBe(false);
|
||||
});
|
||||
|
||||
it("getCategories() returns categories from the injected IConsent", async () => {
|
||||
const recording = new RecordingConsent();
|
||||
await recording.grant("necessary");
|
||||
|
||||
const { result } = renderHook(() => useConsent(), {
|
||||
wrapper: makeWrapper(recording),
|
||||
});
|
||||
|
||||
const cats = result.current.getCategories();
|
||||
expect(cats).toHaveLength(1);
|
||||
expect(cats[0]).toMatchObject({ category: "necessary", state: "granted" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("useConsent without provider", () => {
|
||||
it("throws ConsentContextError when called outside a provider", () => {
|
||||
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
try {
|
||||
expect(() => renderHook(() => useConsent())).toThrow(ConsentContextError);
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
31
packages/core-consent/src/react/consent-provider.tsx
Normal file
31
packages/core-consent/src/react/consent-provider.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { createContext, useContext, type ReactNode } from "react";
|
||||
import type { IConsent } from "../consent.interface";
|
||||
|
||||
const ConsentContext = createContext<IConsent | null>(null);
|
||||
|
||||
export class ConsentContextError extends Error {
|
||||
constructor() {
|
||||
super("useConsent() must be called within a <ConsentProvider>.");
|
||||
this.name = "ConsentContextError";
|
||||
}
|
||||
}
|
||||
|
||||
export function ConsentProvider({
|
||||
value,
|
||||
children,
|
||||
}: {
|
||||
value: IConsent;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<ConsentContext.Provider value={value}>{children}</ConsentContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useConsent(): IConsent {
|
||||
const consent = useContext(ConsentContext);
|
||||
if (consent === null) {
|
||||
throw new ConsentContextError();
|
||||
}
|
||||
return consent;
|
||||
}
|
||||
5
packages/core-consent/src/react/index.ts
Normal file
5
packages/core-consent/src/react/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export {
|
||||
ConsentProvider,
|
||||
useConsent,
|
||||
ConsentContextError,
|
||||
} from "./consent-provider";
|
||||
Reference in New Issue
Block a user