adds unit tests to cover 80%
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
describe('DashboardPage', () => {
|
||||
it('renders title and welcome message for authenticated user', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/dashboard');
|
||||
|
||||
expect(await screen.findByTestId('dashboard-title')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('dashboard-welcome')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('displays the availability status badge when data loads', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/dashboard');
|
||||
|
||||
await screen.findByTestId('dashboard-title');
|
||||
|
||||
// Availability badge should eventually appear after status loads
|
||||
expect(await screen.findByTestId('availability-badge')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows retry button when availability fetch fails', async () => {
|
||||
server.use(http.get('*/Availability/status', () => HttpResponse.error()));
|
||||
|
||||
mockAuthenticated();
|
||||
renderApp('/dashboard');
|
||||
|
||||
await screen.findByTestId('dashboard-title');
|
||||
expect(await screen.findByTestId('availability-retry')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('refetches availability when retry button is clicked', async () => {
|
||||
let callCount = 0;
|
||||
server.use(
|
||||
http.get('*/Availability/status', () => {
|
||||
callCount += 1;
|
||||
if (callCount === 1) return HttpResponse.error();
|
||||
return HttpResponse.json({ status: 'Available', checkedAt: new Date().toISOString(), message: '' });
|
||||
}),
|
||||
);
|
||||
|
||||
mockAuthenticated();
|
||||
renderApp('/dashboard');
|
||||
|
||||
const retryButton = await screen.findByTestId('availability-retry');
|
||||
await userEvent.click(retryButton);
|
||||
|
||||
expect(callCount).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('redirects unauthenticated users to login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/dashboard');
|
||||
|
||||
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('dashboard-title')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user