1+ import { render , screen , fireEvent } from '@testing-library/react'
2+ import { describe , it , expect , vi , beforeEach } from 'vitest'
3+ import Register from './Register'
4+
5+ // Mock auth store
6+ const mockSignUp = vi . fn ( )
7+ vi . mock ( '../store/authStore' , ( ) => ( {
8+ useAuthStore : vi . fn ( ( ) => ( {
9+ signUp : mockSignUp ,
10+ } ) ) ,
11+ } ) )
12+
13+ describe ( 'Register' , ( ) => {
14+ const mockOnSwitchToLogin = vi . fn ( )
15+
16+ beforeEach ( ( ) => {
17+ vi . clearAllMocks ( )
18+ } )
19+
20+ it ( 'renders registration form' , ( ) => {
21+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
22+
23+ expect ( screen . getByText ( 'IT Task Manager' ) ) . toBeInTheDocument ( )
24+ expect ( screen . getByText ( 'Utwórz nowe konto' ) ) . toBeInTheDocument ( )
25+ expect ( screen . getByLabelText ( / I m i ę i n a z w i s k o / ) ) . toBeInTheDocument ( )
26+ expect ( screen . getByLabelText ( / E m a i l / ) ) . toBeInTheDocument ( )
27+ expect ( screen . getByLabelText ( / ^ H a s ł o $ / ) ) . toBeInTheDocument ( )
28+ expect ( screen . getByLabelText ( / P o t w i e r d ź h a s ł o / ) ) . toBeInTheDocument ( )
29+ expect ( screen . getByRole ( 'button' , { name : / Z a r e j e s t r u j s i ę / } ) ) . toBeInTheDocument ( )
30+ } )
31+
32+ it ( 'calls onSwitchToLogin when login link is clicked' , ( ) => {
33+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
34+
35+ const loginButton = screen . getByRole ( 'button' , { name : / Z a l o g u j s i ę / } )
36+ fireEvent . click ( loginButton )
37+
38+ expect ( mockOnSwitchToLogin ) . toHaveBeenCalledTimes ( 1 )
39+ } )
40+
41+ it ( 'updates form inputs' , ( ) => {
42+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
43+
44+ const nameInput = screen . getByLabelText ( / I m i ę i n a z w i s k o / )
45+ const emailInput = screen . getByLabelText ( / E m a i l / )
46+ const passwordInput = screen . getByLabelText ( / ^ H a s ł o $ / )
47+ const confirmPasswordInput = screen . getByLabelText ( / P o t w i e r d ź h a s ł o / )
48+
49+ fireEvent . change ( nameInput , { target : { value : 'Jan Kowalski' } } )
50+ fireEvent . change ( emailInput , { target : { value : 'test@example.com' } } )
51+ fireEvent . change ( passwordInput , { target : { value : 'password123' } } )
52+ fireEvent . change ( confirmPasswordInput , { target : { value : 'password123' } } )
53+
54+ expect ( nameInput ) . toHaveValue ( 'Jan Kowalski' )
55+ expect ( emailInput ) . toHaveValue ( 'test@example.com' )
56+ expect ( passwordInput ) . toHaveValue ( 'password123' )
57+ expect ( confirmPasswordInput ) . toHaveValue ( 'password123' )
58+ } )
59+
60+ it ( 'validates password confirmation' , ( ) => {
61+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
62+
63+ const passwordInput = screen . getByLabelText ( / ^ H a s ł o $ / )
64+ const confirmPasswordInput = screen . getByLabelText ( / P o t w i e r d ź h a s ł o / )
65+
66+ fireEvent . change ( passwordInput , { target : { value : 'password123' } } )
67+ fireEvent . change ( confirmPasswordInput , { target : { value : 'different' } } )
68+
69+ expect ( passwordInput ) . toHaveValue ( 'password123' )
70+ expect ( confirmPasswordInput ) . toHaveValue ( 'different' )
71+ } )
72+
73+ it ( 'validates password length' , ( ) => {
74+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
75+
76+ const passwordInput = screen . getByLabelText ( / ^ H a s ł o $ / )
77+
78+ fireEvent . change ( passwordInput , { target : { value : '123' } } )
79+
80+ expect ( passwordInput ) . toHaveValue ( '123' )
81+ } )
82+
83+ it ( 'submits form when all fields are filled' , ( ) => {
84+ render ( < Register onSwitchToLogin = { mockOnSwitchToLogin } /> )
85+
86+ const nameInput = screen . getByLabelText ( / I m i ę i n a z w i s k o / )
87+ const emailInput = screen . getByLabelText ( / E m a i l / )
88+ const passwordInput = screen . getByLabelText ( / ^ H a s ł o $ / )
89+ const confirmPasswordInput = screen . getByLabelText ( / P o t w i e r d ź h a s ł o / )
90+ const termsCheckbox = screen . getByLabelText ( / A k c e p t u j ę / )
91+ const submitButton = screen . getByRole ( 'button' , { name : / Z a r e j e s t r u j s i ę / } )
92+
93+ fireEvent . change ( nameInput , { target : { value : 'Jan Kowalski' } } )
94+ fireEvent . change ( emailInput , { target : { value : 'test@example.com' } } )
95+ fireEvent . change ( passwordInput , { target : { value : 'password123' } } )
96+ fireEvent . change ( confirmPasswordInput , { target : { value : 'password123' } } )
97+ fireEvent . click ( termsCheckbox )
98+ fireEvent . click ( submitButton )
99+
100+ // Form submission is handled by component, mock prevents actual API call
101+ expect ( nameInput ) . toHaveValue ( 'Jan Kowalski' )
102+ expect ( emailInput ) . toHaveValue ( 'test@example.com' )
103+ } )
104+ } )
0 commit comments