-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSignIn.test.tsx
More file actions
148 lines (122 loc) · 4.09 KB
/
SignIn.test.tsx
File metadata and controls
148 lines (122 loc) · 4.09 KB
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
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SignIn } from '../../../components/auth/SignIn';
jest.mock('../../../contexts/AuthContext', () => ({
useAuth: () => ({
signIn: mockSignIn,
}),
}));
jest.mock('../../../services/api', () => ({
apiService: {
forgotPassword: jest.fn(),
},
}));
const mockSignIn = jest.fn();
const mockOnSignInSuccess = jest.fn();
const mockOnSwitchToSignUp = jest.fn();
describe('SignIn component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders username, password fields and sign in button', () => {
render(
<SignIn
onSignInSuccess={mockOnSignInSuccess}
onSwitchToSignUp={mockOnSwitchToSignUp}
/>,
);
expect(screen.getByLabelText(/Username or Email/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Password/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Sign In/i })).toBeInTheDocument();
});
it('shows validation error when submitting with empty fields', async () => {
render(
<SignIn
onSignInSuccess={mockOnSignInSuccess}
onSwitchToSignUp={mockOnSwitchToSignUp}
/>,
);
await userEvent.click(screen.getByRole('button', { name: /Sign In/i }));
expect(
await screen.findByText(/Please fill in all fields/i),
).toBeInTheDocument();
expect(mockSignIn).not.toHaveBeenCalled();
});
it('calls signIn and onSignInSuccess on successful submit', async () => {
const fakeUser = { id: '1', emailVerified: true };
mockSignIn.mockResolvedValueOnce(fakeUser);
render(
<SignIn
onSignInSuccess={mockOnSignInSuccess}
onSwitchToSignUp={mockOnSwitchToSignUp}
/>,
);
await userEvent.type(
screen.getByLabelText(/Username or Email/i),
'john@example.com',
);
await userEvent.type(screen.getByLabelText(/Password/i), 'password123!');
await userEvent.click(screen.getByRole('button', { name: /Sign In/i }));
await waitFor(() => {
expect(mockSignIn).toHaveBeenCalledWith(
'john@example.com',
'password123!',
);
});
expect(mockOnSignInSuccess).toHaveBeenCalledWith(fakeUser);
});
it('shows error when signIn throws', async () => {
mockSignIn.mockRejectedValueOnce(new Error('Invalid credentials'));
render(
<SignIn
onSignInSuccess={mockOnSignInSuccess}
onSwitchToSignUp={mockOnSwitchToSignUp}
/>,
);
await userEvent.type(
screen.getByLabelText(/Username or Email/i),
'john@example.com',
);
await userEvent.type(screen.getByLabelText(/Password/i), 'wrong');
await userEvent.click(screen.getByRole('button', { name: /Sign In/i }));
expect(
await screen.findByText(/Invalid credentials/i),
).toBeInTheDocument();
});
it('opens forgot password modal and validates email', async () => {
const { apiService } = jest.requireMock('../../../services/api') as {
apiService: { forgotPassword: jest.Mock };
};
render(
<SignIn
onSignInSuccess={mockOnSignInSuccess}
onSwitchToSignUp={mockOnSwitchToSignUp}
/>,
);
await userEvent.click(
screen.getByRole('button', { name: /Forgot your password\?/i }),
);
const emailInput = await screen.findByLabelText(/Registered Email Address/i);
const submitButton = screen.getByRole('button', {
name: /Submit Request/i,
});
// Invalid email
await userEvent.type(emailInput, 'not-an-email');
await userEvent.click(submitButton);
expect(
await screen.findByText(/Please enter a valid email address/i),
).toBeInTheDocument();
// Valid email -> API is called
apiService.forgotPassword.mockResolvedValueOnce({
message: 'Reset email sent',
data: {},
});
await userEvent.clear(emailInput);
await userEvent.type(emailInput, 'user@example.com');
await userEvent.click(submitButton);
await waitFor(() => {
expect(apiService.forgotPassword).toHaveBeenCalledWith('user@example.com');
});
});
});