|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Bayan Flow |
| 3 | + * Licensed under Elastic License 2.0 OR Commercial |
| 4 | + * See LICENSE for details. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 8 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 9 | +import { MemoryRouter } from 'react-router-dom'; |
| 10 | +import { I18nextProvider } from 'react-i18next'; |
| 11 | +import i18n from '../i18n'; |
| 12 | +import { ConsentProvider } from '../contexts/ConsentContext'; |
| 13 | +import CookieConsentBanner from './CookieConsentBanner'; |
| 14 | + |
| 15 | +const STORAGE_KEY = 'bayanflow:cookie-consent'; |
| 16 | + |
| 17 | +function renderBanner() { |
| 18 | + return render( |
| 19 | + <MemoryRouter> |
| 20 | + <I18nextProvider i18n={i18n}> |
| 21 | + <ConsentProvider> |
| 22 | + <CookieConsentBanner /> |
| 23 | + </ConsentProvider> |
| 24 | + </I18nextProvider> |
| 25 | + </MemoryRouter> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +describe('CookieConsentBanner', () => { |
| 30 | + beforeEach(async () => { |
| 31 | + localStorage.clear(); |
| 32 | + await i18n.changeLanguage('en'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders banner when no consent stored', () => { |
| 36 | + renderBanner(); |
| 37 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText(i18n.t('consent.message'))).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not render banner when consent is already stored', () => { |
| 42 | + localStorage.setItem( |
| 43 | + STORAGE_KEY, |
| 44 | + JSON.stringify({ analytics: true, timestamp: Date.now() }) |
| 45 | + ); |
| 46 | + renderBanner(); |
| 47 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('renders accept and decline buttons', () => { |
| 51 | + renderBanner(); |
| 52 | + expect(screen.getByText(i18n.t('consent.acceptAll'))).toBeInTheDocument(); |
| 53 | + expect(screen.getByText(i18n.t('consent.declineAll'))).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders privacy policy link', () => { |
| 57 | + renderBanner(); |
| 58 | + const link = screen.getByText(i18n.t('consent.privacyPolicy')); |
| 59 | + expect(link).toHaveAttribute('href', '/privacy'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('hides banner after accepting', async () => { |
| 63 | + renderBanner(); |
| 64 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 65 | + |
| 66 | + fireEvent.click(screen.getByText(i18n.t('consent.acceptAll'))); |
| 67 | + |
| 68 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 69 | + const stored = JSON.parse(localStorage.getItem(STORAGE_KEY)); |
| 70 | + expect(stored.analytics).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('hides banner after declining', async () => { |
| 74 | + renderBanner(); |
| 75 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 76 | + |
| 77 | + fireEvent.click(screen.getByText(i18n.t('consent.declineAll'))); |
| 78 | + |
| 79 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 80 | + const stored = JSON.parse(localStorage.getItem(STORAGE_KEY)); |
| 81 | + expect(stored.analytics).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('banner is accessible with proper role', () => { |
| 85 | + renderBanner(); |
| 86 | + const dialog = screen.getByRole('dialog'); |
| 87 | + expect(dialog).toHaveAttribute('aria-label'); |
| 88 | + }); |
| 89 | +}); |
0 commit comments