diff --git a/Frontend/package.json b/Frontend/package.json index 6bf66f2..572c3d7 100644 --- a/Frontend/package.json +++ b/Frontend/package.json @@ -3,12 +3,14 @@ "private": true, "version": "0.0.0", "type": "module", - "scripts": { - "dev": "vite", - "build": "tsc -b && vite build", - "lint": "eslint .", - "preview": "vite preview" - }, +"scripts": { + "dev": "vite", + "build": "tsc -b && vite build", + "lint": "eslint .", + "preview": "vite preview", + "test": "vitest", + "test:run": "vitest run" +}, "dependencies": { "react": "^19.2.4", "react-dom": "^19.2.4", diff --git a/Frontend/src/hooks/__tests__/useAuth.test.tsx b/Frontend/src/hooks/__tests__/useAuth.test.tsx new file mode 100644 index 0000000..aac36fa --- /dev/null +++ b/Frontend/src/hooks/__tests__/useAuth.test.tsx @@ -0,0 +1,33 @@ +import { render, screen } from '@testing-library/react'; +import { AuthProvider } from '../useAuth'; +import { useAuth } from '../useAuth'; +import { ReactNode } from 'react'; + +// Test component that uses useAuth hook +const TestComponent = () => { + const auth = useAuth(); + return
{auth.user?.username ?? 'null'}
; +}; + +describe('useAuth hook', () => { + test('throws error when used outside AuthProvider', () => { + // Render component without AuthProvider wrapper + const renderTestComponent = () => render(); + + // Expect it to throw an error + expect(renderTestComponent).toThrow('useAuth must be used within an AuthProvider'); + }); + + test('provides context through AuthProvider', () => { + // Render component with AuthProvider wrapper + render( + + + + ); + + // Should initially show null since no user is set + const authContextElement = screen.getByTestId('auth-user'); + expect(authContextElement).toHaveTextContent('null'); + }); +}); \ No newline at end of file diff --git a/Frontend/src/test/setup.ts b/Frontend/src/test/setup.ts new file mode 100644 index 0000000..201f63d --- /dev/null +++ b/Frontend/src/test/setup.ts @@ -0,0 +1,2 @@ +// Setup file for Vitest with React Testing Library +import '@testing-library/jest-dom' \ No newline at end of file