mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 11:59:11 +00:00
add tests to new-session
This commit is contained in:
parent
a99a6e7ffd
commit
6a327b477a
2 changed files with 41 additions and 4 deletions
34
packages/web/src/pages/machinery/new-session.spec.tsx
Normal file
34
packages/web/src/pages/machinery/new-session.spec.tsx
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import { useSessionContext } from '../../contexts/session/SessionContext';
|
||||
import NewSession from './new-session';
|
||||
|
||||
const setupSessionMock = jest.fn();
|
||||
(useSessionContext as jest.Mock) = jest.fn(() => ({
|
||||
setupSession: setupSessionMock,
|
||||
isAuthenticated: true,
|
||||
}));
|
||||
|
||||
const testSessionID = 'sessionid1234';
|
||||
|
||||
it('sets up the session', () => {
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
expect(useSessionContext).toBeCalled();
|
||||
expect(setupSessionMock).toBeCalledWith('sessionid1234');
|
||||
});
|
||||
|
||||
it('redirects to the correct location when rp_postauth_redirect is set', async () => {
|
||||
localStorage.setItem('rp_postauth_redirect', '/hello_world');
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
const bounceLink = screen.getByText("If you aren't redirected soon, click here.");
|
||||
expect(bounceLink.getAttribute('href')).toBe('/hello_world');
|
||||
});
|
||||
|
||||
it('redirects to the correct location by default', async () => {
|
||||
localStorage.setItem('rp_postauth_redirect', '/servers');
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
const bounceLink = screen.getByText("If you aren't redirected soon, click here.");
|
||||
expect(bounceLink.getAttribute('href')).toBe('/servers');
|
||||
});
|
|
@ -8,10 +8,6 @@ const NewSession = (props: { sessionID: string }) => {
|
|||
const { setupSession, isAuthenticated } = useSessionContext();
|
||||
const [postauthUrl, setPostauthUrl] = React.useState('/servers');
|
||||
|
||||
React.useEffect(() => {
|
||||
setupSession(props.sessionID);
|
||||
}, [props.sessionID, setupSession]);
|
||||
|
||||
React.useEffect(() => {
|
||||
const storedPostauthUrl = localStorage.getItem('rp_postauth_redirect');
|
||||
if (storedPostauthUrl) {
|
||||
|
@ -20,6 +16,13 @@ const NewSession = (props: { sessionID: string }) => {
|
|||
}
|
||||
}, [setPostauthUrl]);
|
||||
|
||||
React.useCallback(
|
||||
(sessionID) => {
|
||||
setupSession(sessionID);
|
||||
},
|
||||
[setupSession]
|
||||
)(props.sessionID);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Logging you into Roleypoly..." />
|
||||
|
|
Loading…
Add table
Reference in a new issue