Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions frontend/components/tests/CookieBanner.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, act, fireEvent } from '@testing-library/react';
import { CookieBanner } from '@/components/shared/CookieBanner';

vi.mock('next-intl', () => ({
useTranslations: () => (key: string) => key,
}));

vi.mock('@/i18n/routing', () => ({
Link: ({ children }: { children: React.ReactNode }) => (
<a href="#">{children}</a>
),
}));
Comment on lines +9 to +13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add React import for JSX in the mock.

The Link mock returns JSX but doesn't import React. While React 17+ automatic JSX transform handles this in regular files, hoisted vi.mock calls may not have access to the transform context.

🐛 Proposed fix
+import React from 'react';
 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
 import { render, screen, act, fireEvent } from '@testing-library/react';
 import { CookieBanner } from '@/components/shared/CookieBanner';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
vi.mock('@/i18n/routing', () => ({
Link: ({ children }: { children: React.ReactNode }) => (
<a href="#">{children}</a>
),
}));
import React from 'react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, act, fireEvent } from '@testing-library/react';
import { CookieBanner } from '@/components/shared/CookieBanner';
vi.mock('@/i18n/routing', () => ({
Link: ({ children }: { children: React.ReactNode }) => (
<a href="#">{children}</a>
),
}));
🤖 Prompt for AI Agents
In @frontend/components/tests/CookieBanner.test.tsx around lines 9 - 13, The
hoisted vi.mock callback returns JSX but lacks the React binding, causing
failures under the mock's transform context; fix by importing React into the
test module scope (e.g., add import React from 'react' at the top of the test)
so the Link mock (the vi.mock(...) implementation that returns <a
href="#">{children}</a>) has React available for JSX, or alternatively rewrite
the mock to use React.createElement for the Link implementation.


vi.mock('lucide-react', () => ({
X: () => <span data-testid="close-icon">X</span>,
}));

describe('CookieBanner UI', () => {
beforeEach(() => {
localStorage.clear();
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it('Wait 500ms -> Banner Appears', () => {
render(<CookieBanner />);
expect(screen.queryByText('title')).toBeNull();

act(() => {
vi.advanceTimersByTime(500);
});

expect(screen.getByText('title')).toBeDefined();
});

it('Click Accept -> Saves to LocalStorage -> Hides Banner', () => {
render(<CookieBanner />);

act(() => {
vi.advanceTimersByTime(500);
});

const acceptButton = screen.getByText('accept');
fireEvent.click(acceptButton);

expect(localStorage.getItem('cookie-consent')).toBe('accepted');
expect(screen.queryByText('title')).toBeNull();
});
});
3 changes: 3 additions & 0 deletions frontend/drizzle/0014_steep_kabuki.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "quiz_attempts" ADD CONSTRAINT "quiz_attempts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "quiz_attempts_user_id_idx" ON "quiz_attempts" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "quiz_attempts_quiz_id_idx" ON "quiz_attempts" USING btree ("quiz_id");
Loading