Skip to content

Commit

Permalink
[#19] Add test for PrivateRoutes but infinie redirect loop if no user
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 21, 2023
1 parent 1c7138c commit cc5b7bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/components/PrivateRoutes/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable camelcase */
import { MemoryRouter } from 'react-router-dom';

import { render, screen } from '@testing-library/react';

import PrivateRoutes from '.';
import { setItem } from '../../helpers/localStorage';

const mockTokenData = {
access_token: 'test_access_token',
refresh_token: 'test_refresh_token',
token_type: 'Bearer',
expires_in: 7200,
created_at: 1677045997,
};

const mockUserProfileData = {
email: '[email protected]',
name: 'TestName',
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d',
};

describe('PrivateRoutes', () => {
test('renders a PrivateRoute given authenticated user', async () => {
setItem('UserProfile', { auth: mockTokenData, user: mockUserProfileData });

render(<PrivateRoutes />, { wrapper: MemoryRouter });

expect(localStorage.getItem).toBeCalledWith('UserProfile');

// expect(screen.getByTestId('app-main-heading'));
});

test.skip('renders a PrivateRoute', async () => {
render(<PrivateRoutes />, { wrapper: MemoryRouter });

expect(screen.getByTestId('loading'));
});
});
2 changes: 1 addition & 1 deletion src/components/PrivateRoutes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function PrivateRoutes() {
}, []);

if (loading) {
return <h3>Loading...</h3>;
return <h3 data-test-id="loading">Loading...</h3>;
}

return user ? (
Expand Down

0 comments on commit cc5b7bf

Please sign in to comment.