add tests to new-session

This commit is contained in:
41666 2021-03-14 18:54:42 -04:00
parent a99a6e7ffd
commit 6a327b477a
2 changed files with 41 additions and 4 deletions

View 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');
});

View file

@ -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..." />