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.61 kB
import*as e from"react/jsx-runtime";import*as r from"vitest";import*as s from"vitest-browser-react";import*as t from"../../hooks/use-consent-manager.js";import*as n from"../consent-manager-provider.js";import*as a from"./test-helpers.js";let{mockFetch:o}=(0,a.setupMocks)();r.vi.mock("c15t",async()=>({...await r.vi.importActual("c15t"),configureConsentManager:e=>{let r=e.backendURL||"";return"c15t"===e.mode&&o(`${r}/show-consent-banner`,{headers:{"Content-Type":"application/json"}}),{showConsentBanner:async()=>({ok:!1,data:null,error:{message:"API error"},response:new Response(JSON.stringify({error:"API error"}),{status:500,headers:{"Content-Type":"application/json"}})}),setConsent:async()=>({ok:!0,data:{success:!0},error:null,response:null}),verifyConsent:async()=>({ok:!0,data:{valid:!0},error:null,response:null})}}})),(0,r.describe)("ConsentManagerProvider Error Handling",()=>{(0,r.beforeEach)(()=>{r.vi.resetAllMocks(),o.mockResolvedValueOnce(new Response(JSON.stringify({error:"API error"}),{status:500,headers:{"Content-Type":"application/json"}}))}),(0,r.afterEach)(()=>{r.vi.clearAllMocks()}),(0,r.it)("should handle errors from API responses",async()=>{let{getByTestId:a}=(0,s.render)((0,e.jsx)(n.ConsentManagerProvider,{options:{mode:"c15t",backendURL:"/api/c15t"},children:(0,e.jsx)(()=>{let r=(0,t.useConsentManager)();return(0,e.jsx)("div",{"data-testid":"request-state",children:r.manager?"Manager Ready":"Manager Not Ready"})},{})}));await r.vi.waitFor(()=>{let e=a("request-state");(0,r.expect)(e).toHaveTextContent(/Manager (Not )?Ready/)}),(0,r.expect)(o).toHaveBeenCalledTimes(1)})});