From e73941cdfed6addf19ab3aef22679fdf7b15fa44 Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Wed, 22 Mar 2023 08:44:47 +0700 Subject: [PATCH] [#19] Cleanup --- src/adapters/authAdapter.ts | 2 +- src/components/PrivateRoute/index.test.tsx | 9 +++++--- src/components/PrivateRoute/index.tsx | 2 +- src/components/Sidebar/index.test.tsx | 25 ++++++++++++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/adapters/authAdapter.ts b/src/adapters/authAdapter.ts index 3ba83b0..4e79b0c 100644 --- a/src/adapters/authAdapter.ts +++ b/src/adapters/authAdapter.ts @@ -39,7 +39,7 @@ class AuthAdapter extends BaseAdapter { static logout(accessToken: string) { /* eslint-disable camelcase */ const requestParams = { - ...commonParams, + ...OauthParams, token: accessToken, }; /* eslint-enable camelcase */ diff --git a/src/components/PrivateRoute/index.test.tsx b/src/components/PrivateRoute/index.test.tsx index f01da45..0b6e97a 100644 --- a/src/components/PrivateRoute/index.test.tsx +++ b/src/components/PrivateRoute/index.test.tsx @@ -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'; @@ -31,14 +31,17 @@ describe('PrivateRoute', () => { render(, { 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(, { wrapper: MemoryRouter }); - await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading')); + expect(screen.getByTestId('loading')).toHaveTextContent('Loading'); - expect(screen.getByTestId('loading')); + // expect(screen.getByTestId('loading')).not.toBeVisible(); }); }); diff --git a/src/components/PrivateRoute/index.tsx b/src/components/PrivateRoute/index.tsx index 00e1280..23e6ec3 100644 --- a/src/components/PrivateRoute/index.tsx +++ b/src/components/PrivateRoute/index.tsx @@ -37,4 +37,4 @@ function PrivateRoute() { ); } -export default PrivateRoutes; +export default PrivateRoute; diff --git a/src/components/Sidebar/index.test.tsx b/src/components/Sidebar/index.test.tsx index d0f4cbf..0e258f9 100644 --- a/src/components/Sidebar/index.test.tsx +++ b/src/components/Sidebar/index.test.tsx @@ -8,7 +8,7 @@ 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: 'testemail@gmail.com', @@ -16,6 +16,14 @@ const mockUserProfileData = { 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: 'testemail@email.com', avatarUrl: mockUserProfileData.avatar_url }; @@ -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(, { wrapper: BrowserRouter }); @@ -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(); }); });