test(worker): Account — fill gaps + lifecycle test
This commit is contained in:
@@ -3,6 +3,7 @@ import userEvent from '@testing-library/user-event';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import Account from './Account';
|
||||
import App from '../App';
|
||||
import { AuthProvider } from '../auth/AuthContext';
|
||||
import { apiFetch } from '../lib/api';
|
||||
import { clearToken, getToken, setToken } from '../lib/auth-storage';
|
||||
@@ -25,6 +26,15 @@ function renderAccount() {
|
||||
);
|
||||
}
|
||||
|
||||
function renderApp() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
return render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<App />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe('Account', () => {
|
||||
beforeEach(() => {
|
||||
mockedApiFetch.mockReset();
|
||||
@@ -44,6 +54,13 @@ describe('Account', () => {
|
||||
expect(screen.getByText('worker@solelog.local')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the fallback message when the account cannot be loaded', async () => {
|
||||
mockedApiFetch.mockResolvedValue({});
|
||||
renderAccount();
|
||||
expect(await screen.findByText('Kon accountgegevens niet laden.')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Test Werker')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('logs out: clicking Uitloggen clears the token', async () => {
|
||||
setToken('tok');
|
||||
const user = userEvent.setup();
|
||||
@@ -51,4 +68,21 @@ describe('Account', () => {
|
||||
await user.click(screen.getByRole('button', { name: 'Uitloggen' }));
|
||||
expect(getToken()).toBeNull();
|
||||
});
|
||||
|
||||
it('lifecycle: logging out returns to the login screen', async () => {
|
||||
setToken('tok');
|
||||
window.history.replaceState(null, '', '/account');
|
||||
const user = userEvent.setup();
|
||||
renderApp();
|
||||
|
||||
// On the Account screen while authenticated.
|
||||
expect(await screen.findByRole('button', { name: 'Uitloggen' })).toBeInTheDocument();
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Uitloggen' }));
|
||||
|
||||
// Context flips to unauthenticated → the login form renders.
|
||||
expect(await screen.findByText('E-mailadres')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: 'Uitloggen' })).not.toBeInTheDocument();
|
||||
expect(getToken()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user