Skip to content

Commit

Permalink
Merge pull request #63 from psinha25/login-test
Browse files Browse the repository at this point in the history
Login test
  • Loading branch information
rizahassan authored Apr 28, 2021
2 parents 9c982f8 + ea3fa80 commit 54753b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions client/src/components/auth/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const Login = ({ auth: { isAuthenticated, user, loading }, login }) => {
type='submit'
value='Login'
className='btn-outline-info btn-block submitFormButton'
data-testid='login-btn'
/>
</Form>
</Col>
Expand Down
59 changes: 46 additions & 13 deletions client/src/test/Login.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom'
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import store from '../store';
import Login from '../components/auth/Login';
import configureStore from 'redux-mock-store';

import Login from '../components/auth/Login.js';

const mockStore = configureStore([]);

const notAuthenticatedStore = mockStore({
auth: {
token: localStorage.getItem('token'),
isAuthenticated: null,
user: null,
loading: true,
}
});

const authenticatedStore = mockStore({
auth: {
token: localStorage.getItem('token'),
isAuthenticated: true,
user: null,
loading: false,
},
});

describe('Login Component', () => {
it('should be redered without crashing', () => {
render(
<Provider store={notAuthenticatedStore}>
<Login/>
</Provider>, { wrapper: MemoryRouter }
);
expect(screen.getByText('Account Login')).toBeInTheDocument();
});

it('should redirect if the user is logged in', () => {
const {
container,
} = render(
<Provider store={authenticatedStore}>
<Login/>
</Provider>, { wrapper: MemoryRouter }
);
expect(container).toMatchInlineSnapshot(`<div />`);
});

test('renders without crashing', () => {
render(
<Provider store={store}>
<BrowserRouter>
<Login />
</BrowserRouter>
</Provider>
);
expect(screen.getByText('Account Login')).toBeInTheDocument();
});

0 comments on commit 54753b3

Please sign in to comment.