Skip to content

Commit

Permalink
[#21] Use nock for mocking API response WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Feb 22, 2023
1 parent 0019700 commit dcca881
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 3 deletions.
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"danger": "10.9.0",
"danger-plugin-istanbul-coverage": "1.6.2",
"eslint": "8.11.0",
"nock": "13.3.0",
"postcss": "8.4.21",
"postcss-import": "14.1.0",
"prettier": "2.6.0",
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { RouteObject } from 'react-router-dom';

import HomeScreen from 'screens/Home';
import LoginScreen from 'screens/Home/login';
import LoginScreen from 'screens/Login';

const routes: RouteObject[] = [
{
Expand Down
91 changes: 91 additions & 0 deletions src/screens/Login/index.test.tsx
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();
// });
});
4 changes: 2 additions & 2 deletions src/screens/Home/login.tsx → src/screens/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ function LoginScreen() {
setToken({ accessToken: accessToken, refreshToken: refreshToken });
navigate('/');
} catch (error) {
let errorMessage = '';
let errorMessage = 'There was a problem receiving a response from the server';

if (error instanceof Error) {
errorMessage = (error as AxiosError).response?.data?.errors[0]?.detail || error.message || 'Internal error';
errorMessage = (error as AxiosError).response?.data?.errors[0]?.detail || error.cause || errorMessage;
}
setErrors([errorMessage]);
} finally {
Expand Down

0 comments on commit dcca881

Please sign in to comment.