login.test.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, it, expect, beforeEach, vi } from 'vitest';
  2. import { render, screen } from '@testing-library/react';
  3. import { MemoryRouter, Routes, Route } from 'react-router-dom';
  4. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  5. import { LoginRoute } from '@/routes/login';
  6. import { AuthProvider } from '@/lib/auth-context';
  7. function renderLogin() {
  8. const qc = new QueryClient({
  9. defaultOptions: { queries: { retry: false } },
  10. });
  11. return render(
  12. <QueryClientProvider client={qc}>
  13. <MemoryRouter initialEntries={['/login']}>
  14. <AuthProvider>
  15. <Routes>
  16. <Route path="/login" element={<LoginRoute />} />
  17. <Route path="/" element={<div>home</div>} />
  18. </Routes>
  19. </AuthProvider>
  20. </MemoryRouter>
  21. </QueryClientProvider>,
  22. );
  23. }
  24. describe('LoginRoute', () => {
  25. beforeEach(() => {
  26. vi.spyOn(globalThis, 'fetch').mockImplementation(async (url) => {
  27. if (String(url).includes('/v1/auth/refresh')) {
  28. return new Response(JSON.stringify({ error: 'no_session' }), {
  29. status: 401,
  30. headers: { 'content-type': 'application/json' },
  31. });
  32. }
  33. return new Response('not stubbed', { status: 501 });
  34. });
  35. });
  36. it('renders the email + password form', async () => {
  37. renderLogin();
  38. expect(await screen.findByLabelText(/email/i)).toBeInTheDocument();
  39. expect(screen.getByLabelText(/password/i)).toBeInTheDocument();
  40. expect(screen.getByRole('button', { name: /sign in/i })).toBeInTheDocument();
  41. });
  42. it('shows the broad-announce brand', async () => {
  43. renderLogin();
  44. expect(await screen.findByText('broad-announce')).toBeInTheDocument();
  45. });
  46. });