Skip to content

Commit 144bdf5

Browse files
Linda Penglpatmo
authored andcommitted
Refactored some tests
1 parent 9be83d0 commit 144bdf5

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# misc
1616
.DS_Store
17+
.env
1718
.env.local
1819
.env.development.local
1920
.env.test.local

src/components/Auth/AuthForm.spec.js

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { fireEvent, render, act } from '@testing-library/react';
2+
import { fireEvent, render, act, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44
import { BrowserRouter } from 'react-router-dom';
55
import AuthForm from './AuthForm';
@@ -13,35 +13,36 @@ describe('AuthForm', () => {
1313
});
1414

1515
it('should only show the Sign Up form by default', () => {
16-
const { getByText, queryByTestId } = render(<AuthForm />);
16+
render(<AuthForm />);
17+
screen.debug();
1718

18-
expect(getByText('Create an account')).toBeInTheDocument();
19-
expect(queryByTestId('loginForm')).toBeNull();
19+
expect(screen.getByText('Create an account')).toBeInTheDocument();
20+
expect(screen.queryByTestId('loginForm')).toBeNull();
2021
});
2122

2223
describe('when clicking on the Log in button in the Sign Up Form', () => {
23-
it('should show the Login Form', () => {
24-
const { getByText, queryByTestId, getByTestId } = render(<AuthForm />);
25-
fireEvent.click(getByText('Log in'));
26-
expect(getByTestId('loginForm')).toBeInTheDocument();
27-
expect(queryByTestId('signupForm')).toBeNull();
24+
it('should show the Login Form', async () => {
25+
render(<AuthForm />);
26+
fireEvent.click(screen.getByText('Log in'));
27+
expect(await screen.findByTestId('loginForm')).toBeInTheDocument();
28+
expect(await screen.queryByTestId('signupForm')).toBeNull();
2829
});
2930
});
3031

3132
describe('when clicking on the Sign up button in the Log In Form', () => {
32-
it('should show the Sign Up form', () => {
33-
const { getByText, queryByTestId } = render(<AuthForm />);
34-
fireEvent.click(getByText('Log in'));
35-
fireEvent.click(getByText('Sign up'));
36-
expect(queryByTestId('loginForm')).toBeNull();
37-
expect(getByText('Create an account')).toBeInTheDocument();
33+
it('should show the Sign Up form', async () => {
34+
render(<AuthForm />);
35+
fireEvent.click(screen.getByText('Log in'));
36+
fireEvent.click(screen.getByText('Sign up'));
37+
expect(await screen.queryByTestId('loginForm')).toBeNull();
38+
expect(await screen.findByText('Create an account')).toBeInTheDocument();
3839
});
3940
});
4041
});
4142

4243
describe('Signup', () => {
4344
it('Register a new user on the signup form', async () => {
44-
const { getByText, getByLabelText } = render(
45+
render(
4546
<BrowserRouter>
4647
<SignUpForm />
4748
</BrowserRouter>
@@ -58,38 +59,27 @@ describe('Signup', () => {
5859
},
5960
});
6061

61-
await act(async () =>
62-
fireEvent.change(getByLabelText(/username/i), {
63-
target: { value: 'Carolyne.Carter' },
64-
})
65-
);
62+
fireEvent.change(screen.getByLabelText(/username/i), {
63+
target: { value: 'Carolyne.Carter' },
64+
});
6665

67-
await act(async () =>
68-
fireEvent.change(getByLabelText(/password/i), {
69-
target: { value: 'password' },
70-
})
71-
);
66+
fireEvent.change(screen.getByLabelText(/password/i), {
67+
target: { value: 'password' },
68+
});
7269

73-
await act(async () =>
74-
fireEvent.change(getByLabelText(/email/i), {
75-
target: { value: '[email protected]' },
76-
})
77-
);
70+
fireEvent.change(screen.getByLabelText(/email/i), {
71+
target: { value: '[email protected]' },
72+
});
7873

79-
await act(async () =>
80-
fireEvent.change(getByLabelText(/first name/i), {
81-
target: { value: 'Carolyne' },
82-
})
83-
);
74+
fireEvent.change(screen.getByLabelText(/first name/i), {
75+
target: { value: 'Carolyne' },
76+
});
8477

85-
await act(async () =>
86-
fireEvent.change(getByLabelText(/last name/i), {
87-
target: { value: 'Carter' },
88-
})
89-
);
78+
fireEvent.change(screen.getByLabelText(/last name/i), {
79+
target: { value: 'Carter' },
80+
});
9081

91-
const submit = getByText('Sign Up');
92-
await act(async () => fireEvent.click(submit));
82+
fireEvent.click(screen.getByText('Sign Up'));
9383

9484
await act(async () => mockRegisterResponse());
9585

0 commit comments

Comments
 (0)