Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hide <Logout /> button if irrelevant. #7842

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/ra-ui-materialui/src/auth/Logout.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { AdminContext } from '../AdminContext';
import { Logout } from './Logout';
import { UserAuthenticated, UserUnauthenticated } from './Logout.stories';

export default { title: 'ra-ui-materialui/input/AutocompleteArrayInput' };

const MinimalAdmin = authProvider => {
return (
<AdminContext authProvider={authProvider}>
<Logout />
</AdminContext>
);
};

it('should display logout button if the auth succeeds', async () => {
render(<UserAuthenticated />);

await waitFor(() => {
const logoutButton = screen.queryByText(
'ra.auth.logout'
) as HTMLInputElement;
expect(logoutButton).not.toBeNull();
});
});

it('should not display logout button if the auth fails', async () => {
render(<UserUnauthenticated />);

await waitFor(() => {
const logoutButton = screen.queryByText(
'ra.auth.logout'
) as HTMLInputElement;
expect(logoutButton).toBeNull();
});
});
29 changes: 29 additions & 0 deletions packages/ra-ui-materialui/src/auth/Logout.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Typography } from '@mui/material';
import * as React from 'react';
import { AdminContext } from '../AdminContext';
import { Logout } from './Logout';

export default { title: 'ra-ui-materialui/auth/Logout' };

const MinimalAdmin = (props: { authenticated: boolean }) => {
const authProvider = {
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
checkAuth: () =>
props.authenticated ? Promise.resolve() : Promise.reject(),
checkError: () => Promise.resolve(),
getPermissions: () => Promise.resolve(),
};
return (
<AdminContext authProvider={authProvider}>
<Typography variant="h6">
Should {props.authenticated ? '' : 'not '}display logout button
</Typography>
<Logout />
</AdminContext>
);
};

export const UserAuthenticated = () => <MinimalAdmin authenticated={true} />;

export const UserUnauthenticated = () => <MinimalAdmin authenticated={false} />;
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/auth/Logout.tsx
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export const Logout: FunctionComponent<
> = React.forwardRef(function Logout(props, ref) {
const { className, redirectTo, icon, ...rest } = props;

const { authenticated } = useAuthState();
const isXSmall = useMediaQuery((theme: Theme) =>
theme.breakpoints.down('sm')
);
@@ -34,7 +35,6 @@ export const Logout: FunctionComponent<
redirectTo,
logout,
]);
const { authenticated } = useAuthState();

if (!authenticated) return null;