@c15t/react
Version:
Developer-first CMP for React: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization
1 lines • 2.91 kB
JavaScript
import*as e from"react/jsx-runtime";import*as n from"c15t";import*as t from"vitest";import*as i from"vitest-browser-react";import*as a from"../../index.js";let r=t.vi.fn();window.fetch=r,(0,t.describe)("ConsentManagerProvider Basic Request Behavior",()=>{(0,t.beforeEach)(()=>{t.vi.resetAllMocks(),t.vi.useFakeTimers(),r.mockResolvedValue(new Response(JSON.stringify({showConsentBanner:!0,jurisdiction:{code:"GDPR"},translations:{language:"en",translations:n.defaultTranslationConfig.translations.en}}),{status:200,headers:{"Content-Type":"application/json"}}))}),(0,t.afterEach)(()=>{t.vi.clearAllMocks(),t.vi.useRealTimers()}),(0,t.it)("should only make one initial request for consent banner status",async()=>{(0,i.render)((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"c15t",backendURL:"/api/c15t"},children:(0,e.jsx)("div",{children:"Test Component"})})),await t.vi.runAllTimersAsync(),(0,t.expect)(r).toHaveBeenCalledTimes(1),(0,t.expect)(r).toHaveBeenCalledWith(t.expect.stringContaining("/api/c15t/show-consent-banner"),t.expect.any(Object))}),(0,t.it)("should not make additional requests when props change but core options remain same",async()=>{r.mockClear();let{rerender:n}=(0,i.render)((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"offline",react:{theme:{"banner.root":"light"}}},children:(0,e.jsx)("div",{children:"Light theme"})}));await t.vi.runAllTimersAsync(),(0,t.expect)(r).not.toHaveBeenCalled(),n((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"offline",react:{theme:{"banner.root":"dark"}}},children:(0,e.jsx)("div",{children:"Dark theme"})})),await t.vi.runAllTimersAsync(),(0,t.expect)(r).not.toHaveBeenCalled()}),(0,t.it)("should make a new request when core options change",async()=>{let{rerender:n}=(0,i.render)((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"c15t",backendURL:"/api/c15t-1"},children:(0,e.jsx)("div",{children:"First URL"})}));await t.vi.runAllTimersAsync(),(0,t.expect)(r).toHaveBeenCalledTimes(1),(0,t.expect)(r).toHaveBeenCalledWith(t.expect.stringContaining("/api/c15t-1/show-consent-banner"),t.expect.any(Object)),r.mockClear(),n((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"c15t",backendURL:"/api/c15t-2"},children:(0,e.jsx)("div",{children:"Second URL"})})),await t.vi.runAllTimersAsync(),(0,t.expect)(r).toHaveBeenCalledWith(t.expect.stringContaining("/api/c15t-2/show-consent-banner"),t.expect.any(Object))}),(0,t.it)("should handle rapid re-renders without making duplicate requests",async()=>{r.mockClear();let{rerender:n}=(0,i.render)((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"offline"},children:(0,e.jsx)("div",{children:"Counter: 0"})}));await t.vi.runAllTimersAsync(),(0,t.expect)(r).not.toHaveBeenCalled();for(let i=1;i<=5;i++)n((0,e.jsx)(a.ConsentManagerProvider,{options:{mode:"offline"},children:(0,e.jsxs)("div",{children:["Counter: ",i]})})),await t.vi.runAllTimersAsync();(0,t.expect)(r).not.toHaveBeenCalled()})});