@c15t/nextjs
Version:
Developer-first CMP for Next.js: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization.
1 lines • 2.98 kB
JavaScript
import*as e from"vitest";import*as t from"./headers.js";import*as a from"./initial-data.js";import*as o from"./normalize-url.js";e.vi.mock("next/headers",()=>({headers:()=>new Headers({"x-forwarded-proto":"https","x-forwarded-host":"example.com",cookie:"test=123"})})),e.vi.mock("./headers",()=>({extractRelevantHeaders:e.vi.fn()})),e.vi.mock("./normalize-url",()=>({normalizeBackendURL:e.vi.fn()})),(0,e.describe)("getC15TInitialData",()=>{let n={"x-forwarded-proto":"https","x-forwarded-host":"example.com",cookie:"test=123"},r=new Headers({"x-forwarded-proto":"https","x-forwarded-host":"example.com",cookie:"test=123"}),i=e.vi.fn();(0,e.beforeEach)(()=>{e.vi.clearAllMocks(),e.vi.stubGlobal("fetch",i),t.extractRelevantHeaders.mockReturnValue(n),o.normalizeBackendURL.mockReturnValue("https://api.example.com")}),(0,e.afterEach)(()=>{e.vi.unstubAllGlobals()}),(0,e.it)("should return undefined when normalized URL is not available",async()=>{o.normalizeBackendURL.mockReturnValue(null);let t=await (0,a.getC15TInitialData)("https://example.com",r);(0,e.expect)(t).toBeUndefined()}),(0,e.it)("should return undefined when no relevant headers are present",async()=>{t.extractRelevantHeaders.mockReturnValue({});let o=await (0,a.getC15TInitialData)("https://example.com",r);(0,e.expect)(o).toBeUndefined()}),(0,e.it)("should successfully fetch and return consent banner data",async()=>{let t={showConsentBanner:!0,branding:"c15t",jurisdiction:{message:"Please accept cookies",code:"GDPR"},location:{countryCode:"US",regionCode:"CA"},translations:{language:"en",translations:{common:{acceptAll:"Accept All",rejectAll:"Reject All",customize:"Customize",save:"Save"},cookieBanner:{title:"Cookie Preferences",description:"We use cookies to improve your experience"},consentManagerDialog:{title:"Manage Cookie Preferences",description:"Customize your cookie preferences"},consentTypes:{necessary:{title:"Necessary",description:"Required for the website to function"},experience:{title:"Experience",description:"Enhance your browsing experience"},functionality:{title:"Functionality",description:"Enable specific functionality"},marketing:{title:"Marketing",description:"Help us improve our marketing"},measurement:{title:"Measurement",description:"Help us measure performance"}}}}};i.mockResolvedValueOnce({ok:!0,json:()=>Promise.resolve(t)});let o=await (0,a.getC15TInitialData)("https://example.com",r);(0,e.expect)(i).toHaveBeenCalledWith("https://api.example.com/show-consent-banner",{method:"GET",headers:n}),(0,e.expect)(o).toEqual(t)}),(0,e.it)("should handle fetch failure gracefully",async()=>{i.mockRejectedValueOnce(Error("Network error"));let t=await (0,a.getC15TInitialData)("https://example.com",r);(0,e.expect)(t).toBeUndefined()}),(0,e.it)("should handle non-ok response gracefully",async()=>{i.mockResolvedValueOnce({ok:!1,status:500,statusText:"Internal Server Error"});let t=await (0,a.getC15TInitialData)("https://example.com",r);(0,e.expect)(t).toBeUndefined()})});