-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/components/UI/NavigationBar/MenuList/LogoutButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react' | ||
import { NavigationBarContextProvider } from 'components/UI/NavigationBar/NavigationBarContext' | ||
import { mockAxios } from 'test/axios-mock' | ||
import { TServerConfig } from 'test/test-values' | ||
import { LoggedInProviders } from 'test/testMocks' | ||
|
||
import { MenuList } from './MenuList' | ||
|
||
describe('LogoutButton', () => { | ||
test('logs user out when clicked', async () => { | ||
mockAxios | ||
.onGet('/config') | ||
.reply(200, Object.assign({}, TServerConfig, { auth_enabled: true })) | ||
|
||
render( | ||
<LoggedInProviders> | ||
<NavigationBarContextProvider | ||
toggleDrawer={(open: boolean) => () => { | ||
// noop | ||
}} | ||
drawerIsOpen={true} | ||
> | ||
<MenuList /> | ||
</NavigationBarContextProvider> | ||
</LoggedInProviders>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Logout')).toBeInTheDocument() | ||
}) | ||
|
||
const logOutButton = screen.getByText('Logout') | ||
fireEvent.click(logOutButton) | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Logout')).not.toBeInTheDocument() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import LogoutIcon from '@mui/icons-material/Logout' | ||
import { ListItemButton, ListItemIcon, ListItemText } from '@mui/material' | ||
import { AuthContainer } from 'containers/AuthContainer' | ||
|
||
const LogoutButton = () => { | ||
const { logout } = AuthContainer.useContainer() | ||
|
||
return ( | ||
<ListItemButton onClick={logout} sx={{ pl: 1 }}> | ||
<ListItemIcon> | ||
<LogoutIcon fontSize="small" /> | ||
</ListItemIcon> | ||
<ListItemText primary={'Logout'} /> | ||
</ListItemButton> | ||
) | ||
|
||
} | ||
export { LogoutButton } |
49 changes: 49 additions & 0 deletions
49
src/components/UI/NavigationBar/MenuList/MenuList.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { render, screen, waitFor } from '@testing-library/react' | ||
import { NavigationBarContextProvider } from 'components/UI/NavigationBar/NavigationBarContext' | ||
import { mockAxios } from 'test/axios-mock' | ||
import { TServerConfig } from 'test/test-values' | ||
import { AllProviders, LoggedInProviders } from 'test/testMocks' | ||
|
||
import { MenuList } from './MenuList' | ||
|
||
describe('Menu List', () => { | ||
test('should hide Logout button when not logged in', async () => { | ||
render( | ||
<AllProviders> | ||
<NavigationBarContextProvider | ||
toggleDrawer={(open: boolean) => () => { | ||
// noop | ||
}} | ||
drawerIsOpen={true} | ||
> | ||
<MenuList /> | ||
</NavigationBarContextProvider> | ||
</AllProviders>, | ||
) | ||
await waitFor(() => { | ||
expect(screen.queryByText('Logout')).not.toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
test('should show logout button when logged in', async () => { | ||
mockAxios | ||
.onGet('/config') | ||
.reply(200, Object.assign({}, TServerConfig, { auth_enabled: true })) | ||
|
||
render( | ||
<LoggedInProviders> | ||
<NavigationBarContextProvider | ||
toggleDrawer={(open: boolean) => () => { | ||
// noop | ||
}} | ||
drawerIsOpen={true} | ||
> | ||
<MenuList /> | ||
</NavigationBarContextProvider> | ||
</LoggedInProviders>, | ||
) | ||
await waitFor(() => { | ||
expect(screen.getByText('Logout')).toBeInTheDocument() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters