test: Add Vitest + React Testing Library setup (Refs #2)
Some checks failed
CI/CD Pipeline / backend-build (push) Successful in 2m30s
CI/CD Pipeline / frontend-build (push) Failing after 2m0s
CI/CD Pipeline / docker-build (push) Has been skipped

This commit is contained in:
2026-04-01 16:14:06 -03:00
parent d7481323f9
commit f8e5060278
3 changed files with 43 additions and 6 deletions

View File

@@ -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 <div data-testid="auth-user">{auth.user?.username ?? 'null'}</div>;
};
describe('useAuth hook', () => {
test('throws error when used outside AuthProvider', () => {
// Render component without AuthProvider wrapper
const renderTestComponent = () => render(<TestComponent />);
// 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(
<AuthProvider>
<TestComponent />
</AuthProvider>
);
// Should initially show null since no user is set
const authContextElement = screen.getByTestId('auth-user');
expect(authContextElement).toHaveTextContent('null');
});
});

View File

@@ -0,0 +1,2 @@
// Setup file for Vitest with React Testing Library
import '@testing-library/jest-dom'