@c15t/react
Version:
Developer-first CMP for React: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization
1 lines • 5.68 kB
JavaScript
import*as e from"react/jsx-runtime";import*as t from"vitest";import*as n from"vitest-browser-react";import*as o from"../../providers/consent-manager-provider.js";import*as r from"../use-translations.js";(0,t.describe)("useTranslations",()=>{(0,t.beforeEach)(()=>{(0,o.clearConsentManagerCache)()}),(0,t.test)("returns English translations by default",async()=>{let{result:s}=(0,n.renderHook)(()=>(0,r.useTranslations)(),{wrapper:({children:t})=>(0,e.jsx)(o.ConsentManagerProvider,{options:{mode:"offline",react:{noStyle:!1}},children:t})});await new Promise(e=>setTimeout(e,10)),(0,t.expect)(s.current.cookieBanner.title).toBe("We value your privacy"),(0,t.expect)(s.current.cookieBanner.description).toBe("This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content."),(0,t.expect)(s.current.consentManagerDialog.title).toBe("Privacy Settings"),(0,t.expect)(s.current.common.acceptAll).toBe("Accept All"),(0,t.expect)(s.current.common.rejectAll).toBe("Reject All"),(0,t.expect)(s.current.common.customize).toBe("Customize"),(0,t.expect)(s.current.common.save).toBe("Save Settings"),(0,t.expect)(s.current.consentTypes?.necessary?.title).toBe("Strictly Necessary")}),(0,t.test)("returns German translations instead of English when German is selected",async()=>{let{result:s}=(0,n.renderHook)(()=>(0,r.useTranslations)(),{wrapper:({children:t})=>(0,e.jsx)(o.ConsentManagerProvider,{options:{mode:"offline",react:{noStyle:!1},translations:{defaultLanguage:"de",disableAutoLanguageSwitch:!0,translations:{de:{common:{acceptAll:"German Accept All",rejectAll:"German Reject All",customize:"German Customize",save:"German Save"},cookieBanner:{title:"German Title",description:"German Description"},consentManagerDialog:{title:"German Dialog Title"},consentTypes:{necessary:{title:"German Necessary",description:"German Necessary Description"}}}}}},children:t})});await new Promise(e=>setTimeout(e,10)),(0,t.expect)(s.current.cookieBanner.title).toBe("German Title"),(0,t.expect)(s.current.cookieBanner.description).toBe("German Description"),(0,t.expect)(s.current.consentManagerDialog.title).toBe("German Dialog Title"),(0,t.expect)(s.current.common.acceptAll).toBe("German Accept All"),(0,t.expect)(s.current.common.rejectAll).toBe("German Reject All"),(0,t.expect)(s.current.common.customize).toBe("German Customize"),(0,t.expect)(s.current.common.save).toBe("German Save"),(0,t.expect)(s.current.consentTypes?.necessary?.title).toBe("German Necessary")}),(0,t.test)("merges custom translations with defaults",async()=>{let s={translations:{en:{cookieBanner:{title:"Custom Cookie Settings",description:"Custom Description"}}}},{result:c}=(0,n.renderHook)(()=>(0,r.useTranslations)(),{wrapper:({children:t})=>(0,e.jsx)(o.ConsentManagerProvider,{options:{mode:"offline",react:{noStyle:!1},translations:s},children:t})});await new Promise(e=>setTimeout(e,10)),(0,t.expect)(c.current.cookieBanner.title).toBe("Custom Cookie Settings"),(0,t.expect)(c.current.cookieBanner.description).toBe("Custom Description"),(0,t.expect)(c.current.consentManagerDialog.title).toBe("Privacy Settings"),(0,t.expect)(c.current.common.acceptAll).toBe("Accept All"),(0,t.expect)(c.current.common.rejectAll).toBe("Reject All"),(0,t.expect)(c.current.common.customize).toBe("Customize"),(0,t.expect)(c.current.common.save).toBe("Save Settings")}),(0,t.test)("falls back to English when selected language is not available",async()=>{let{result:s}=(0,n.renderHook)(()=>(0,r.useTranslations)(),{wrapper:({children:t})=>(0,e.jsx)(o.ConsentManagerProvider,{options:{mode:"offline",react:{noStyle:!1},translations:{defaultLanguage:"fr"}},children:t})});await new Promise(e=>setTimeout(e,10)),(0,t.expect)(s.current.cookieBanner.title).toBe("We value your privacy"),(0,t.expect)(s.current.cookieBanner.description).toBe("This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content."),(0,t.expect)(s.current.consentManagerDialog.title).toBe("Privacy Settings"),(0,t.expect)(s.current.common.acceptAll).toBe("Accept All"),(0,t.expect)(s.current.common.rejectAll).toBe("Reject All"),(0,t.expect)(s.current.common.customize).toBe("Customize"),(0,t.expect)(s.current.common.save).toBe("Save Settings")}),(0,t.test)("Custom English instead of English when German is selected",async()=>{let{result:s}=(0,n.renderHook)(()=>(0,r.useTranslations)(),{wrapper:({children:t})=>(0,e.jsx)(o.ConsentManagerProvider,{options:{mode:"offline",react:{noStyle:!1},translations:{defaultLanguage:"en",disableAutoLanguageSwitch:!0,translations:{en:{common:{acceptAll:"Custom English Accept All",rejectAll:"Custom English Reject All",customize:"Custom English Customize",save:"Custom English Save"},cookieBanner:{title:"Custom English Title",description:"Custom English Description"},consentManagerDialog:{title:"Custom English Dialog Title"},consentTypes:{necessary:{title:"Custom English Necessary",description:"Custom English Necessary Description"}}}}}},children:t})});await new Promise(e=>setTimeout(e,20)),(0,t.expect)(s.current.common.acceptAll).toBe("Custom English Accept All"),(0,t.expect)(s.current.common.rejectAll).toBe("Custom English Reject All"),(0,t.expect)(s.current.common.customize).toBe("Custom English Customize"),(0,t.expect)(s.current.common.save).toBe("Custom English Save"),(0,t.expect)(s.current.cookieBanner.title).toBe("Custom English Title"),(0,t.expect)(s.current.cookieBanner.description).toBe("Custom English Description"),(0,t.expect)(s.current.consentManagerDialog.title).toBe("Custom English Dialog Title"),(0,t.expect)(s.current.consentTypes?.necessary?.title).toBe("Custom English Necessary")})});