Skip to content

Commit

Permalink
initialize session without loading state (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
hboylan committed Jun 14, 2021
1 parent 47c17a8 commit dd12181
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/client/__tests__/client-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ test("fetches the session once and re-uses it for different consumers", async ()
)

render(<ProviderFlow />)
expect(screen.getByTestId("session-consumer-1")).toHaveTextContent("loading")
expect(screen.getByTestId("session-consumer-2")).toHaveTextContent("loading")

await waitFor(() => {
expect(sessionRouteCall).toHaveBeenCalledTimes(1)
Expand All @@ -38,6 +40,17 @@ test("fetches the session once and re-uses it for different consumers", async ()
})
})

test("initialize provider with session", () => {
render(<ProviderFlow session={mockSession} />)

expect(screen.getByTestId("session-consumer-1")).not.toHaveTextContent(
"loading"
)
expect(screen.getByTestId("session-consumer-2")).not.toHaveTextContent(
"loading"
)
})

function ProviderFlow({ options = {} }) {
return (
<>
Expand All @@ -52,7 +65,8 @@ function ProviderFlow({ options = {} }) {
function SessionConsumer({ testId = 1 }) {
const [session, loading] = useSession()

if (loading) return <span>loading</span>
if (loading)
return <span data-testid={`session-consumer-${testId}`}>loading</span>

return (
<div data-testid={`session-consumer-${testId}`}>
Expand Down
2 changes: 1 addition & 1 deletion src/client/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export function SessionProvider(props) {
})

/** If session was passed, initialize as not loading */
const [loading, setLoading] = React.useState(hasInitialSession)
const [loading, setLoading] = React.useState(!hasInitialSession)

React.useEffect(() => {
__NEXTAUTH._getSession = async ({ event } = {}) => {
Expand Down

0 comments on commit dd12181

Please sign in to comment.