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

isLoading should default to true even when doing SSR #193

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 15 additions & 10 deletions __tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/**
* @jest-environment node
*/
import { renderHook } from '@testing-library/react-hooks';
import { createWrapper } from './helpers';
import { useAuth0 } from '../src';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { Auth0Provider, Auth0Context } from '../src';

jest.unmock('@auth0/auth0-spa-js');

describe('In a Node SSR environment', () => {
it('auth state is initialised', async () => {
const wrapper = createWrapper();
const {
result: {
current: { isLoading, isAuthenticated, user, loginWithRedirect },
},
} = renderHook(useAuth0, { wrapper });
expect(isLoading).toBeFalsy();
let isLoading, isAuthenticated, user, loginWithRedirect;
ReactDOMServer.renderToString(
<Auth0Provider clientId="__client_id__" domain="__domain__">
<Auth0Context.Consumer>
{(value): JSX.Element => {
({ isLoading, isAuthenticated, user, loginWithRedirect } = value);
return <div>App</div>;
}}
</Auth0Context.Consumer>
</Auth0Provider>
);
expect(isLoading).toBeTruthy();
expect(isAuthenticated).toBeFalsy();
expect(user).toBeUndefined();
await expect(loginWithRedirect).rejects.toThrowError(
Expand Down
3 changes: 1 addition & 2 deletions src/auth-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ export interface AuthState {
*/
export const initialAuthState: AuthState = {
isAuthenticated: false,
// In SSR mode the library will never check the session, so loading should be initialised as false
isLoading: typeof window !== 'undefined',
isLoading: true,
};