generated from nimblehq/git-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#21] Use nock for mocking API response WIP
- Loading branch information
1 parent
0019700
commit dcca881
Showing
5 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { BrowserRouter } from 'react-router-dom'; | ||
|
||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import nock from 'nock'; | ||
|
||
import LoginScreen from '.'; | ||
|
||
afterAll(() => { | ||
nock.cleanAll(); | ||
nock.restore(); | ||
}); | ||
|
||
describe('LoginScreen', () => { | ||
test('submit an an empty email and password and receive errors', async () => { | ||
render(<LoginScreen />, { wrapper: BrowserRouter }); | ||
|
||
const submitButton = screen.getByRole('button', { name: 'login.sign-in' }); | ||
|
||
await userEvent.click(submitButton); | ||
|
||
expect(screen.getByText('login.invalid-email')).toBeInTheDocument(); | ||
expect(screen.getByText('login.invalid-password')).toBeInTheDocument(); | ||
}); | ||
|
||
test('submit incorrect details', async () => { | ||
render(<LoginScreen />, { wrapper: BrowserRouter }); | ||
|
||
const formData = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.post('/oauth/token', { | ||
email: formData.email, | ||
password: formData.password, | ||
grant_type: 'password', | ||
client_id: process.env.REACT_APP_API_CLIENT_ID, | ||
client_secret: process.env.REACT_APP_API_CLIENT_SECRET, | ||
}) | ||
.reply(400, { | ||
errors: [ | ||
{ | ||
detail: 'Your email or password is incorrect. Please try again.', | ||
code: 'invalid_email_or_password', | ||
}, | ||
], | ||
}); | ||
|
||
const emailField = screen.getByLabelText('login.email'); | ||
const passwordField = screen.getByLabelText('login.password'); | ||
const submitButton = screen.getByRole('button', { name: 'login.sign-in' }); | ||
|
||
await userEvent.type(emailField, formData.email); | ||
await userEvent.type(passwordField, formData.password); | ||
|
||
await userEvent.click(submitButton); | ||
await waitFor(() => { | ||
expect(screen.getByText('Your email or password is incorrect. Please try again.')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
// test('Allows form submission of email and password', async () => { | ||
// const requestData = { | ||
// email: '[email protected]', | ||
// password: 'password123', | ||
// }; | ||
|
||
// render(<LoginScreen />, { wrapper: BrowserRouter }); | ||
|
||
// const emailField = screen.getByLabelText('login.email'); | ||
// const passwordField = screen.getByLabelText('login.password'); | ||
// const submitButton = screen.getByRole('button', { name: 'login.sign-in' }); | ||
|
||
// await userEvent.type(emailField, '[email protected]'); | ||
// await userEvent.type(passwordField, 'password123'); | ||
|
||
// const requestSpy = jest.spyOn(axios, 'request').mockImplementation(() => Promise.resolve({})); | ||
|
||
// await userEvent.click(submitButton); | ||
|
||
// expect(axios.request).toHaveBeenCalledWith(requestData); | ||
|
||
// requestSpy.mockRestore(); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters