Allowing local lemonsqueezy payment skip and emulate success response
This commit is contained in:
@@ -13,6 +13,8 @@ import { Switch } from '@/components/ui/switch';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { useConsent, ConsentPreferences } from '@/contexts/consent';
|
||||
|
||||
const SKIP_BANNER_STORAGE_KEY = 'fotospiel.consent.skip';
|
||||
|
||||
const CookieBanner: React.FC = () => {
|
||||
const { t } = useTranslation('common');
|
||||
const {
|
||||
@@ -28,6 +30,35 @@ const CookieBanner: React.FC = () => {
|
||||
|
||||
const [draftPreferences, setDraftPreferences] = useState<ConsentPreferences>(preferences);
|
||||
|
||||
const shouldSkipBanner = useMemo(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!import.meta.env.DEV) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const hasSkipParam = params.get('consent') === 'skip' || params.get('cookieBanner') === 'off';
|
||||
|
||||
if (hasSkipParam) {
|
||||
try {
|
||||
window.localStorage.setItem(SKIP_BANNER_STORAGE_KEY, '1');
|
||||
} catch (error) {
|
||||
console.warn('[Consent] Failed to persist skip flag', error);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return window.localStorage.getItem(SKIP_BANNER_STORAGE_KEY) === '1';
|
||||
} catch (error) {
|
||||
console.warn('[Consent] Failed to read skip flag', error);
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isPreferencesOpen) {
|
||||
setDraftPreferences(preferences);
|
||||
@@ -51,6 +82,10 @@ const CookieBanner: React.FC = () => {
|
||||
closePreferences();
|
||||
};
|
||||
|
||||
if (shouldSkipBanner) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{showBanner && !isPreferencesOpen && (
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import React from 'react';
|
||||
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
|
||||
const consentMock = vi.fn();
|
||||
|
||||
vi.mock('@/contexts/consent', () => ({
|
||||
useConsent: () => consentMock(),
|
||||
}));
|
||||
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/components/ui/button', () => ({
|
||||
Button: ({ children, ...props }: { children: React.ReactNode }) => (
|
||||
<button type="button" {...props}>
|
||||
{children}
|
||||
</button>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock('@/components/ui/dialog', () => ({
|
||||
Dialog: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
DialogContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
DialogDescription: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
DialogFooter: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
DialogHeader: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
DialogTitle: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
}));
|
||||
|
||||
vi.mock('@/components/ui/switch', () => ({
|
||||
Switch: () => <input type="checkbox" />,
|
||||
}));
|
||||
|
||||
vi.mock('@/components/ui/separator', () => ({
|
||||
Separator: () => <hr />,
|
||||
}));
|
||||
|
||||
import CookieBanner from '../CookieBanner';
|
||||
|
||||
describe('CookieBanner', () => {
|
||||
beforeEach(() => {
|
||||
consentMock.mockReturnValue({
|
||||
showBanner: true,
|
||||
acceptAll: vi.fn(),
|
||||
rejectAll: vi.fn(),
|
||||
preferences: { analytics: false, functional: true },
|
||||
savePreferences: vi.fn(),
|
||||
isPreferencesOpen: false,
|
||||
openPreferences: vi.fn(),
|
||||
closePreferences: vi.fn(),
|
||||
});
|
||||
|
||||
window.localStorage.removeItem('fotospiel.consent.skip');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
window.localStorage.removeItem('fotospiel.consent.skip');
|
||||
});
|
||||
|
||||
it('renders the banner by default', () => {
|
||||
render(<CookieBanner />);
|
||||
|
||||
expect(screen.getByText('consent.banner.title')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('hides the banner when the dev skip flag is set', () => {
|
||||
window.localStorage.setItem('fotospiel.consent.skip', '1');
|
||||
|
||||
render(<CookieBanner />);
|
||||
|
||||
expect(screen.queryByText('consent.banner.title')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user