Skip to content

Commit

Permalink
[#19] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 22, 2023
1 parent 3ea12fc commit e73941c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/adapters/authAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AuthAdapter extends BaseAdapter {
static logout(accessToken: string) {
/* eslint-disable camelcase */
const requestParams = {
...commonParams,
...OauthParams,
token: accessToken,
};
/* eslint-enable camelcase */
Expand Down
9 changes: 6 additions & 3 deletions src/components/PrivateRoute/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */
import { MemoryRouter } from 'react-router-dom';

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

import PrivateRoute from '.';
import { setItem, clearItem } from '../../helpers/localStorage';
Expand Down Expand Up @@ -31,14 +31,17 @@ describe('PrivateRoute', () => {
render(<PrivateRoute />, { wrapper: MemoryRouter });

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

// Only the header is rendered and not the outlet (home page)
// expect(screen.getByTestId('app-main-heading')).toBeVisible();
});

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

await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading'));
expect(screen.getByTestId('loading')).toHaveTextContent('Loading');

expect(screen.getByTestId('loading'));
// expect(screen.getByTestId('loading')).not.toBeVisible();
});
});
2 changes: 1 addition & 1 deletion src/components/PrivateRoute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ function PrivateRoute() {
);
}

export default PrivateRoutes;
export default PrivateRoute;
25 changes: 19 additions & 6 deletions src/components/Sidebar/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ import AuthAdapter from 'adapters/authAdapter';
import { User } from 'types/User';

import Sidebar from '.';
// import * as localStorage from '../../helpers/localStorage';
import * as myStorage from '../../helpers/localStorage';

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

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

describe('Sidebar', () => {
const user: User = { name: 'Test User', email: '[email protected]', avatarUrl: mockUserProfileData.avatar_url };

Expand All @@ -31,11 +39,13 @@ describe('Sidebar', () => {
test('renders a sidebar on the page with a logout button that when clicked, calls Logout adapter and removes storage', async () => {
const mockLogout = jest.spyOn(AuthAdapter, 'logout');

// jest.spyOn(localStorage, 'getItem').mockImplementation(() => {
// return { auth: 'mockTokenData', user: mockUserProfileData };
// });
// const mockClearToken = jest.spyOn(myStorage, 'clearItem');

// const mockStorageClear = jest.spyOn(localStorage, 'clearItem');
const storageMock = jest.spyOn(myStorage, 'getItem').mockImplementationOnce(() => {
return { auth: mockTokenData, user: mockUserProfileData };
});

expect(myStorage.getItem('UserProfile')).not.toBeNull();

render(<Sidebar user={user} />, { wrapper: BrowserRouter });

Expand All @@ -45,8 +55,11 @@ describe('Sidebar', () => {

expect(mockLogout).toBeCalledTimes(1);

// expect(mockStorageClear).toBeCalled();
expect(myStorage.getItem('UserProfile')).toBeNull();

// navigates to LOGIN URL
// useNavigate is not called because redirect is handled in axios interceptor? Mock window.location.href instead?

storageMock.mockRestore();
});
});

0 comments on commit e73941c

Please sign in to comment.