Skip to content

Commit

Permalink
Merge branch 'master' into STCOR-905
Browse files Browse the repository at this point in the history
  • Loading branch information
aidynoJ authored Nov 14, 2024
2 parents 3acf956 + 0d65a69 commit 354d35a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
14 changes: 12 additions & 2 deletions src/components/Logout/Logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {

import OrganizationLogo from '../OrganizationLogo';
import { useStripes } from '../../StripesContext';
import { logout } from '../../loginServices';
import {
getUnauthorizedPathFromSession,
logout,
removeUnauthorizedPathFromSession,
} from '../../loginServices';

import styles from './Logout.css';

Expand Down Expand Up @@ -51,6 +55,12 @@ const Logout = () => {
[]
);

const handleClick = (_e) => {
removeUnauthorizedPathFromSession();
};

const redirectTo = getUnauthorizedPathFromSession() || '/';

if (!didLogout) {
return <LoadingView />;
}
Expand All @@ -71,7 +81,7 @@ const Logout = () => {
</Row>
<Row center="xs">
<Col xs={12}>
<Button to="/"><FormattedMessage id="stripes-core.rtr.idleSession.logInAgain" /></Button>
<Button to={redirectTo} onClick={handleClick}><FormattedMessage id="stripes-core.rtr.idleSession.logInAgain" /></Button>
</Col>
</Row>
</div>
Expand Down
64 changes: 52 additions & 12 deletions src/components/Logout/Logout.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
import { userEvent } from '@folio/jest-config-stripes/testing-library/user-event';
import { useLocation } from 'react-router';

import Logout from './Logout';
import { useStripes } from '../../StripesContext';
import { logout } from '../../loginServices';
import { getUnauthorizedPathFromSession, logout, setUnauthorizedPathToSession } from '../../loginServices';

jest.mock('../OrganizationLogo');
jest.mock('../../StripesContext');
jest.mock('react-router');

jest.mock('../../loginServices', () => ({
...jest.requireActual('../../loginServices'),
logout: jest.fn(() => Promise.resolve()),
}));

Expand All @@ -36,6 +38,15 @@ describe('Logout', () => {
expect(logout).toHaveBeenCalled();
screen.getByText('stripes-core.logoutComplete');
});

it('"log in again" href points to "/"', async () => {
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: false } });

render(<Logout />);
const button = screen.getByRole('button');
expect(button).toHaveAttribute('href', '/');
});
});

describe('Timeout logout', () => {
Expand All @@ -44,21 +55,50 @@ describe('Logout', () => {
mockUseLocation.mockReturnValue({ pathName: '/logout-timeout' });
});

it('if not authenticated, renders a timeout message', async () => {
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: false } });
describe('if authenticated', () => {
it('calls logout then renders a timeout message', async () => {
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: true } });

render(<Logout />);
screen.getByText('stripes-core.rtr.idleSession.sessionExpiredSoSad');
render(<Logout />);
expect(logout).toHaveBeenCalled();
screen.getByText('stripes-core.rtr.idleSession.sessionExpiredSoSad');
});

it('"login in again" href points to pre-timeout location', () => {
const previousPath = '/monkey?bagel';
setUnauthorizedPathToSession(previousPath);

const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: false } });

render(<Logout />);

expect(screen.getByRole('button')).toHaveAttribute('href', previousPath);
});

it('clicking "log in again" clears pre-timeout location from storage', async () => {
setUnauthorizedPathToSession('/monkey?bagel');

const user = userEvent.setup();
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: false } });

render(<Logout />);

await user.click(screen.getByRole('button'));
expect(getUnauthorizedPathFromSession()).toBeFalsy();
});
});

it('if authenticated, calls logout then renders a timeout message', async () => {
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: true } });
describe('if not authenticated', () => {
it('renders a timeout message', async () => {
const mockUseStripes = useStripes;
mockUseStripes.mockReturnValue({ okapi: { isAuthenticated: false } });

render(<Logout />);
expect(logout).toHaveBeenCalled();
screen.getByText('stripes-core.rtr.idleSession.sessionExpiredSoSad');
render(<Logout />);
screen.getByText('stripes-core.rtr.idleSession.sessionExpiredSoSad');
});
});
});
});
25 changes: 18 additions & 7 deletions test/jest/__mock__/stripesComponents.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ jest.mock('@folio/stripes-components', () => ({
<span>{props.children}</span>
</span>
)),
Button: jest.fn(({ children, onClick = jest.fn() }) => (
<button data-test-button type="button" onClick={onClick}>
<span>
{children}
</span>
</button>
)),
Button: jest.fn(({ children, to, onClick = jest.fn() }) => {
if (to) {
return (
<a href={to} role="button" data-test-button onClick={onClick}>
<span>
{children}
</span>
</a>
);
}
return (
<button data-test-button type="button" onClick={onClick}>
<span>
{children}
</span>
</button>
);
}),
Callout: jest.fn(({ children, ...rest }) => (
<span {...rest}>{children}</span>
)),
Expand Down

0 comments on commit 354d35a

Please sign in to comment.