Permalink
Newer
100644
87 lines (70 sloc)
2.89 KB
3
import { LoginModule } from './';
4
5
describe('LoginModule', () => {
6
test('initial state', () => {
10
const emailField = screen.getByRole('textbox', { name: 'Email' });
11
expect(emailField).toHaveValue('');
12
const passwordField = screen.getByLabelText('Password');
13
expect(passwordField).toHaveValue('');
16
const button = screen.getByRole('button');
17
expect(button).not.toBeDisabled();
18
expect(button).toHaveTextContent('Submit');
19
});
20
21
test('successful login', async () => {
22
jest
23
.spyOn(window, 'fetch')
24
.mockResolvedValue({ json: () => ({ token: '123' }) });
25
28
const emailField = screen.getByRole('textbox', { name: 'Email' });
29
const passwordField = screen.getByLabelText('Password');
30
const button = screen.getByRole('button');
31
32
// fill out and submit form
33
fireEvent.change(emailField, { target: { value: 'test@email.com' } });
34
fireEvent.change(passwordField, { target: { value: 'password' } });
35
fireEvent.click(button);
36
37
// it sets loading state
40
41
await waitFor(() => {
42
// it hides form elements
43
expect(button).not.toBeInTheDocument();
44
expect(emailField).not.toBeInTheDocument();
45
expect(passwordField).not.toBeInTheDocument();
46
47
// it displays success text and email address
51
expect(emailAddressText).toBeInTheDocument();
52
});
53
});
54
55
test('error login', async () => {
56
jest
57
.spyOn(window, 'fetch')
58
.mockResolvedValue({ json: () => ({ error: 'invalid password' }) });
59
62
const emailField = screen.getByRole('textbox', { name: 'Email' });
63
const passwordField = screen.getByLabelText('Password');
64
const button = screen.getByRole('button');
65
66
// fill out and submit form
67
fireEvent.change(emailField, { target: { value: 'test@email.com' } });
68
fireEvent.change(passwordField, { target: { value: 'password' } });
69
fireEvent.click(button);
70
71
// it sets loading state