import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { render, screen, waitFor } from '@testing-library/react'; import { MemoryRouter } from 'react-router-dom'; import { afterEach, describe, expect, it, vi } from 'vitest'; import App from './App.jsx'; describe('home route', () => { afterEach(() => { vi.unstubAllGlobals(); }); it('redirects to the physician login page', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => ({ ok: false, status: 401, json: async () => ({ error: { message: 'Authentication required' } }), })), ); const queryClient = new QueryClient(); render( , ); expect(await screen.findByRole('heading', { name: 'Sign in' })).toBeInTheDocument(); expect(screen.getByLabelText('Email')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Send code' })).toBeInTheDocument(); expect(screen.getByRole('img', { name: 'CREST' })).toBeInTheDocument(); }); it('loads the dashboard after a session is found', async () => { vi.stubGlobal( 'fetch', vi.fn(async (path) => { if (path === '/api/me') { return { ok: true, status: 200, json: async () => ({ user: { id: 1, email: 'doctor@example.com', name: null }, application: { id: 1, status: 'DRAFT', email: 'doctor@example.com' }, steps: { consent: { done: false }, agreement: { done: false }, survey: { done: false, status: null }, }, nextStep: 'consent', }), }; } return { ok: false, status: 404, json: async () => ({}), }; }), ); const queryClient = new QueryClient(); render( , ); await waitFor(() => { expect(screen.getByRole('heading', { name: 'Dashboard' })).toBeInTheDocument(); }); expect(screen.getByText('Signed in as doctor@example.com')).toBeInTheDocument(); expect(screen.getByText('1. Consent (PIS / ICF)')).toBeInTheDocument(); expect(screen.getByRole('link', { name: /Continue to consent/i })).toHaveAttribute( 'href', '/consent', ); }); });