feat(web): add Vitest test framework with 8 test files (BIT-14221)
- Install Vitest, @testing-library/react, @testing-library/user-event,
@testing-library/jest-dom, jsdom, @vitest/coverage-v8
- Add vitest.config.ts with jsdom environment and v8 coverage
- Add src/test/setup.ts with jest-dom matchers
- Write 5 component tests and 3 integration tests for LoginPage:
renders, form-fields, button-state, error-alert, loading-state,
submit-success, submit-error-response, submit-network-error
- 31 tests pass; 92%+ stmt/branch coverage on page.tsx
- Add test-web CI job to .gitea/workflows/ci.yml
Co-Authored-By: Paperclip <[email protected]>
This commit is contained in:
1 parent
b7cbf528ee
commit
94d2d12c97
13 files changed
+2675
-10
No files matched your search
Generated
+2168
-8
File diff suppressed because it is too large.
Load diff
+12
-2
@@ -7,7 +7,10 @@
|
||||
"build": "next build",
|
||||
"start": "next start -p 3201",
|
||||
"lint": "next lint",
|
||||
"typecheck": "tsc --noEmit"
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"test:coverage": "vitest run --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.7.2",
|
||||
@@ -17,12 +20,19 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.2.0",
|
||||
"@testing-library/jest-dom": "^6.9.1",
|
||||
"@testing-library/react": "^16.3.2",
|
||||
"@testing-library/user-event": "^14.6.1",
|
||||
"@types/node": "^22.9.0",
|
||||
"@types/react": "19.2.14",
|
||||
"@types/react-dom": "19.2.3",
|
||||
"@vitejs/plugin-react": "^6.0.1",
|
||||
"@vitest/coverage-v8": "^4.1.5",
|
||||
"eslint": "^9.15.0",
|
||||
"eslint-config-next": "15.5.15",
|
||||
"typescript": "^5.6.3"
|
||||
"jsdom": "^29.0.2",
|
||||
"typescript": "^5.6.3",
|
||||
"vitest": "^4.1.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — submit button enabled/disabled state', () => {
|
||||
it('is disabled when both fields are empty', () => {
|
||||
render(<LoginPage />);
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).toBeDisabled();
|
||||
});
|
||||
|
||||
it('is disabled when only email is filled', async () => {
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).toBeDisabled();
|
||||
});
|
||||
|
||||
it('is disabled when only password is filled', async () => {
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).toBeDisabled();
|
||||
});
|
||||
|
||||
it('is enabled when both email and password are filled', async () => {
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).not.toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — error alert display', () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('shows API error message on failed login', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Invalid credentials.' }),
|
||||
} as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'wrongpass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('alert')).toBeInTheDocument();
|
||||
expect(screen.getByText('Invalid credentials.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows fallback message when API returns no message', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
} as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid credentials. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('dismisses error when close button is clicked', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Bad credentials.' }),
|
||||
} as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => screen.getByRole('alert'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Close' }));
|
||||
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — form field interactions', () => {
|
||||
it('updates email field on input', async () => {
|
||||
render(<LoginPage />);
|
||||
const emailInput = screen.getByLabelText('Email') as HTMLInputElement;
|
||||
await userEvent.type(emailInput, '[email protected]');
|
||||
expect(emailInput.value).toBe('[email protected]');
|
||||
});
|
||||
|
||||
it('updates password field on input', async () => {
|
||||
render(<LoginPage />);
|
||||
const passwordInput = screen.getByLabelText('Password') as HTMLInputElement;
|
||||
await userEvent.type(passwordInput, 'secret123');
|
||||
expect(passwordInput.value).toBe('secret123');
|
||||
});
|
||||
|
||||
it('email input has correct type', () => {
|
||||
render(<LoginPage />);
|
||||
expect(screen.getByLabelText('Email')).toHaveAttribute('type', 'email');
|
||||
});
|
||||
|
||||
it('password input has correct type', () => {
|
||||
render(<LoginPage />);
|
||||
expect(screen.getByLabelText('Password')).toHaveAttribute('type', 'password');
|
||||
});
|
||||
|
||||
it('email input has autocomplete=username', () => {
|
||||
render(<LoginPage />);
|
||||
expect(screen.getByLabelText('Email')).toHaveAttribute('autocomplete', 'username');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — loading state', () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('shows spinner and "Signing In…" text during submit', async () => {
|
||||
// Never resolve so we can catch the loading state
|
||||
vi.mocked(fetch).mockReturnValueOnce(new Promise(() => {}));
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Signing In…')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('disables email input while loading', async () => {
|
||||
vi.mocked(fetch).mockReturnValueOnce(new Promise(() => {}));
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByLabelText('Email')).toBeDisabled();
|
||||
});
|
||||
});
|
||||
|
||||
it('disables password input while loading', async () => {
|
||||
vi.mocked(fetch).mockReturnValueOnce(new Promise(() => {}));
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByLabelText('Password')).toBeDisabled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — initial render', () => {
|
||||
beforeEach(() => {
|
||||
render(<LoginPage />);
|
||||
});
|
||||
|
||||
it('renders the page heading', () => {
|
||||
expect(screen.getByText('Welcome Back!')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the email input', () => {
|
||||
expect(screen.getByLabelText('Email')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the password input', () => {
|
||||
expect(screen.getByLabelText('Password')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the submit button', () => {
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the product tagline', () => {
|
||||
expect(screen.getByText(/BitReport · Error Reporting Platform/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders the logo link', () => {
|
||||
const logoLink = screen.getByRole('link');
|
||||
expect(logoLink).toHaveAttribute('href', '/');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
const mockPush = vi.fn();
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: mockPush }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — API error responses (integration)', () => {
|
||||
beforeEach(() => {
|
||||
mockPush.mockClear();
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('does NOT redirect on 401', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Unauthorized' }),
|
||||
} as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'bad');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => screen.getByRole('alert'));
|
||||
expect(mockPush).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles non-JSON error response gracefully', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => { throw new SyntaxError('not json'); },
|
||||
} as unknown as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'bad');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid credentials. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('re-enables the form after an error so the user can retry', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({
|
||||
ok: false,
|
||||
json: async () => ({ message: 'Bad credentials.' }),
|
||||
} as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'bad');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => screen.getByRole('alert'));
|
||||
expect(screen.getByLabelText('Email')).not.toBeDisabled();
|
||||
expect(screen.getByLabelText('Password')).not.toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — network failure (integration)', () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('shows connection error message when fetch rejects', async () => {
|
||||
vi.mocked(fetch).mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByText('Unable to connect to the server. Please try again.'),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('re-enables the submit button after network error', async () => {
|
||||
vi.mocked(fetch).mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => screen.getByRole('alert'));
|
||||
expect(screen.getByRole('button', { name: 'Sign In' })).not.toBeDisabled();
|
||||
});
|
||||
|
||||
it('dismisses network error and allows retry', async () => {
|
||||
vi.mocked(fetch)
|
||||
.mockRejectedValueOnce(new TypeError('Failed to fetch'))
|
||||
.mockResolvedValueOnce({ ok: true } as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'secret123');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => screen.getByRole('alert'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Close' }));
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import LoginPage from '../../app/(admin)/login/page';
|
||||
|
||||
const mockPush = vi.fn();
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ push: mockPush }),
|
||||
}));
|
||||
|
||||
describe('LoginPage — successful login (integration)', () => {
|
||||
beforeEach(() => {
|
||||
mockPush.mockClear();
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('redirects to /admin/dashboard on 200 response', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ ok: true } as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'correct-pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockPush).toHaveBeenCalledWith('/admin/dashboard');
|
||||
});
|
||||
});
|
||||
|
||||
it('calls the login API with credentials:include', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ ok: true } as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'correct-pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/admin/auth/login'),
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('sends trimmed email in the request body', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ ok: true } as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), ' [email protected] ');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => {
|
||||
const [, init] = vi.mocked(fetch).mock.calls[0];
|
||||
const body = JSON.parse((init as RequestInit).body as string);
|
||||
expect(body.email).toBe('[email protected]');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not show an error alert after successful login', async () => {
|
||||
vi.mocked(fetch).mockResolvedValueOnce({ ok: true } as Response);
|
||||
|
||||
render(<LoginPage />);
|
||||
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
|
||||
await userEvent.type(screen.getByLabelText('Password'), 'correct-pass');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Sign In' }));
|
||||
|
||||
await waitFor(() => expect(mockPush).toHaveBeenCalled());
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
import '@testing-library/jest-dom';
|
||||
@@ -0,0 +1,23 @@
|
||||
import { defineConfig } from 'vitest/config';
|
||||
import react from '@vitejs/plugin-react';
|
||||
import path from 'node:path';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
test: {
|
||||
environment: 'jsdom',
|
||||
setupFiles: ['./src/test/setup.ts'],
|
||||
globals: true,
|
||||
coverage: {
|
||||
provider: 'v8',
|
||||
reporter: ['text', 'json', 'html'],
|
||||
include: ['src/**/*.{ts,tsx}'],
|
||||
exclude: ['src/test/**', '**/*.d.ts'],
|
||||
},
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': path.resolve(__dirname, './src'),
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user