File size: 7,968 Bytes
cd99321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, waitFor } from '../../tests/helpers/render';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { server } from '../../tests/helpers/msw/server';
import { resetAllStores } from '../../tests/helpers/store';
import RegisterPage from './RegisterPage';

const mockNavigate = vi.fn();
vi.mock('react-router-dom', async () => {
  const actual = await vi.importActual('react-router-dom');
  return { ...actual, useNavigate: () => mockNavigate };
});

const USERNAME_PLACEHOLDER = 'johndoe';
const EMAIL_PLACEHOLDER = 'your@email.com';
const PASSWORD_PLACEHOLDER = 'Min. 6 characters';
const CONFIRM_PASSWORD_PLACEHOLDER = 'Repeat password';

beforeEach(() => {
  resetAllStores();
  vi.clearAllMocks();
});

describe('RegisterPage', () => {
  describe('FE-PAGE-REG-001: Renders registration form with all fields', () => {
    it('shows username, email, password, confirm-password inputs and submit button', () => {
      render(<RegisterPage />);
      expect(screen.getByPlaceholderText(USERNAME_PLACEHOLDER)).toBeInTheDocument();
      expect(screen.getByPlaceholderText(EMAIL_PLACEHOLDER)).toBeInTheDocument();
      expect(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER)).toBeInTheDocument();
      expect(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER)).toBeInTheDocument();
      expect(screen.getByRole('button', { name: /register/i })).toBeInTheDocument();
    });
  });

  describe('FE-PAGE-REG-002: Password mismatch shows error', () => {
    it('displays mismatch error without calling API', async () => {
      const user = userEvent.setup();
      render(<RegisterPage />);

      await user.type(screen.getByPlaceholderText(USERNAME_PLACEHOLDER), 'testuser');
      await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'test@example.com');
      await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password1');
      await user.type(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER), 'password2');
      await user.click(screen.getByRole('button', { name: /^register$/i }));

      await waitFor(() => {
        expect(screen.getByText(/do not match/i)).toBeInTheDocument();
      });
    });
  });

  describe('FE-PAGE-REG-003: Password too short shows error', () => {
    it('displays length error when passwords are the same but too short', async () => {
      const user = userEvent.setup();
      render(<RegisterPage />);

      await user.type(screen.getByPlaceholderText(USERNAME_PLACEHOLDER), 'testuser');
      await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'test@example.com');
      await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'abc');
      await user.type(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER), 'abc');
      await user.click(screen.getByRole('button', { name: /^register$/i }));

      await waitFor(() => {
        expect(screen.getByText(/at least 8/i)).toBeInTheDocument();
      });
    });
  });

  describe('FE-PAGE-REG-004: Successful registration navigates to /dashboard', () => {
    it('calls navigate("/dashboard") after successful registration', async () => {
      const user = userEvent.setup();
      render(<RegisterPage />);

      await user.type(screen.getByPlaceholderText(USERNAME_PLACEHOLDER), 'testuser');
      await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'test@example.com');
      await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
      await user.type(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER), 'password123');
      await user.click(screen.getByRole('button', { name: /^register$/i }));

      await waitFor(() => {
        expect(mockNavigate).toHaveBeenCalledWith('/dashboard');
      });
    });
  });

  describe('FE-PAGE-REG-005: Loading state during submission', () => {
    it('disables submit button and shows loading text while registering', async () => {
      server.use(
        http.post('/api/auth/register', async () => {
          await new Promise(resolve => setTimeout(resolve, 100));
          return HttpResponse.json({ user: { id: 1, username: 'newuser' } });
        }),
      );

      const user = userEvent.setup();
      render(<RegisterPage />);

      await user.type(screen.getByPlaceholderText(USERNAME_PLACEHOLDER), 'testuser');
      await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'test@example.com');
      await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
      await user.type(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER), 'password123');
      await user.click(screen.getByRole('button', { name: /^register$/i }));

      await waitFor(() => {
        const btn = screen.getByRole('button', { name: /registering/i });
        expect(btn).toBeDisabled();
      });
    });
  });

  describe('FE-PAGE-REG-006: API error displayed', () => {
    it('shows error message returned by the API', async () => {
      server.use(
        http.post('/api/auth/register', () => {
          return HttpResponse.json({ error: 'Username already taken' }, { status: 409 });
        }),
      );

      const user = userEvent.setup();
      render(<RegisterPage />);

      await user.type(screen.getByPlaceholderText(USERNAME_PLACEHOLDER), 'testuser');
      await user.type(screen.getByPlaceholderText(EMAIL_PLACEHOLDER), 'test@example.com');
      await user.type(screen.getByPlaceholderText(PASSWORD_PLACEHOLDER), 'password123');
      await user.type(screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER), 'password123');
      await user.click(screen.getByRole('button', { name: /^register$/i }));

      await waitFor(() => {
        expect(screen.getByText('Username already taken')).toBeInTheDocument();
      });
    });
  });

  describe('FE-PAGE-REG-007: Show/hide password toggle', () => {
    it('toggles password input type between password and text', async () => {
      const user = userEvent.setup();
      render(<RegisterPage />);

      const passwordInput = screen.getByPlaceholderText(PASSWORD_PLACEHOLDER);
      const confirmInput = screen.getByPlaceholderText(CONFIRM_PASSWORD_PLACEHOLDER);

      expect(passwordInput).toHaveAttribute('type', 'password');
      expect(confirmInput).toHaveAttribute('type', 'password');

      // The toggle button is the only button of type "button" (not submit) before form submission
      const toggleButton = screen.getByRole('button', { name: '' });
      await user.click(toggleButton);

      expect(passwordInput).toHaveAttribute('type', 'text');
      expect(confirmInput).toHaveAttribute('type', 'text');

      await user.click(toggleButton);

      expect(passwordInput).toHaveAttribute('type', 'password');
      expect(confirmInput).toHaveAttribute('type', 'password');
    });
  });

  describe('FE-PAGE-REG-008: Link to login page is present', () => {
    it('renders a Sign In link pointing to /login', () => {
      render(<RegisterPage />);
      const link = screen.getByRole('link', { name: /sign in/i });
      expect(link).toBeInTheDocument();
      expect(link).toHaveAttribute('href', '/login');
    });
  });

  describe('FE-PAGE-REG-009: Feature list rendered', () => {
    it('renders feature list items in the DOM', () => {
      render(<RegisterPage />);
      // Features are always in the DOM (hidden via CSS on mobile)
      expect(screen.getByText(/Unlimited trip plans/i)).toBeInTheDocument();
      expect(screen.getByText(/Interactive map view/i)).toBeInTheDocument();
      expect(screen.getByText(/Track reservations/i)).toBeInTheDocument();
    });
  });

  describe('FE-PAGE-REG-010: Required attribute on username input', () => {
    it('username input has required attribute', () => {
      render(<RegisterPage />);
      expect(screen.getByPlaceholderText(USERNAME_PLACEHOLDER)).toBeRequired();
    });
  });
});