-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginForm.test.tsx
More file actions
64 lines (58 loc) · 2.59 KB
/
LoginForm.test.tsx
File metadata and controls
64 lines (58 loc) · 2.59 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
import { render, screen, act } from '@testing-library/react';
import { LoginForm } from './LoginForm';
import userEvent from '@testing-library/user-event';
describe('email validation', () => {
const mockedLogInFunc = jest.spyOn(auth, 'signInWithEmailAndPassword');
it('if email field is empty error message should be displayed', async () => {
render(<LoginForm />);
await act(async () => {
userEvent.click(screen.getByRole('button', { name: /sign in/i }));
});
expect(screen.getByText(/email is required/i)).toBeInTheDocument();
expect(mockedLogInFunc).not.toHaveBeenCalled();
});
it("if email field doesn't contain valid email error message should be displayed", async () => {
render(<LoginForm />);
userEvent.type(screen.getByLabelText(/email/i), 'email');
await act(async () => {
userEvent.click(screen.getByRole('button', { name: /sign in/i }));
});
expect(screen.getByText(/mail must be a valid email/i)).toBeInTheDocument();
expect(mockedLogInFunc).not.toHaveBeenCalled();
});
});
describe('password validation', () => {
const mockedLogInFunc = jest.spyOn(auth, 'signInWithEmailAndPassword');
it('if password field is empty error message should be displayed', async () => {
render(<LoginForm />);
await act(async () => {
userEvent.click(screen.getByRole('button', { name: /sign in/i }));
});
expect(screen.getByText(/password is required/i)).toBeInTheDocument();
expect(mockedLogInFunc).not.toHaveBeenCalled();
});
it("if password field doesn't contain valid password error message should be displayed", async () => {
render(<LoginForm />);
userEvent.type(screen.getByLabelText(/password/i), 'password');
await act(async () => {
userEvent.click(screen.getByRole('button', { name: /sign in/i }));
});
expect(
screen.getByText(
/password must contain an uppercase letter, a special character, a number and must be at least 8 characters long/i,
),
).toBeInTheDocument();
expect(mockedLogInFunc).not.toHaveBeenCalled();
});
});
test('when email and password are valid callback to log in should be called', async () => {
const mockedLogInFunc = jest.spyOn(auth, 'signInWithEmailAndPassword');
render(<LoginForm />);
userEvent.type(screen.getByLabelText(/email/i), 'email@email.com');
userEvent.type(screen.getByLabelText(/password/i), 'Te$$t1ng');
await act(async () => {
userEvent.click(screen.getByRole('button', { name: /sign in/i }));
});
expect(mockedLogInFunc).toHaveBeenCalled();
expect(mockedLogInFunc).toHaveBeenCalledWith('email@email.com', 'Te$$t1ng');
});