UNPKG

@c15t/react

Version:

Developer-first CMP for React: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization

1 lines 1.76 kB
import*as n from"react/jsx-runtime";import*as e from"vitest";import*as s from"../../index.js";function o(){let n=e.vi.fn();window.fetch=n;let s=e.vi.fn(),o=new Map;return(0,e.beforeEach)(()=>{o.clear()}),e.vi.mock("c15t",async()=>({...await e.vi.importActual("c15t"),configureConsentManager:e=>{s(e);let r=e.backendURL||"";if("c15t"===e.mode)return{getCallbacks:()=>e.callbacks,showConsentBanner:async()=>(o.has(r)||(n(`${r}/show-consent-banner`,{headers:{"Content-Type":"application/json"}}),o.set(r,!0)),{ok:!0,data:{showConsentBanner:!0},error:null,response:null}),setConsent:async()=>({ok:!0,data:{success:!0},error:null,response:null}),verifyConsent:async()=>({ok:!0,data:{valid:!0},error:null,response:null})};if("offline"===e.mode)return{getCallbacks:()=>e.callbacks,showConsentBanner:async()=>({ok:!0,data:{showConsentBanner:!0},error:null,response:null}),setConsent:async()=>({ok:!0,data:{success:!0},error:null,response:null}),verifyConsent:async()=>({ok:!0,data:{valid:!0},error:null,response:null})};if("custom"===e.mode&&"endpointHandlers"in e){let n=e.endpointHandlers;return{getCallbacks:()=>e.callbacks,showConsentBanner:async()=>n.showConsentBanner({}),setConsent:async e=>n.setConsent({body:e}),verifyConsent:async e=>n.verifyConsent({body:e})}}return{getCallbacks:()=>e.callbacks,showConsentBanner:async()=>({ok:!0,data:{showConsentBanner:!0},error:null,response:null}),setConsent:async()=>({ok:!0,data:{success:!0},error:null,response:null}),verifyConsent:async()=>({ok:!0,data:{valid:!0},error:null,response:null})}}})),{mockFetch:n,mockConfigureConsentManager:s}}let r=()=>{let e=(0,s.useConsentManager)();return(0,n.jsx)("div",{"data-testid":"consumer",children:e.showPopup?"Show":"Hide"})};export{r as TestConsumer,o as setupMocks};