Repository navigation
fix(clerk-react): Added useAuth test and made getToken, and signOut stable #455
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { render } from '@clerk/shared/testUtils'; | ||
| import React, { useEffect } from 'react'; | ||
|
|
||
| import { useAuth } from './useAuth'; | ||
| import { ClerkProvider } from '../contexts/ClerkProvider'; | ||
|
|
||
| import { withClerk } from '../components/withClerk'; | ||
|
|
||
| jest.mock('../components/withClerk', () => ({ | ||
| withClerk: (Component: any) => (props: any) => { | ||
| return ( | ||
| <Component | ||
| {...props} | ||
| clerk={{}} | ||
| /> | ||
| ); | ||
| }, | ||
| })); | ||
|
|
||
| describe('useAuth()', () => { | ||
| it('getToken is stable between re-renders', async () => { | ||
| let renderCount = 0; | ||
| const MyDemo = ({ id }: { id: number }) => { | ||
| const { getToken } = useAuth(); | ||
| useEffect(() => { | ||
| renderCount++; | ||
| }, [getToken]); | ||
| return <button>Hi {id}</button>; | ||
| }; | ||
| const App = withClerk(({ id }: { id: number; clerk }) => ( | ||
| <ClerkProvider frontendApi='https://example.com'> | ||
| <MyDemo id={id} /> | ||
| </ClerkProvider> | ||
| )); | ||
| expect(renderCount).toBe(0); | ||
| const { rerender } = render(<App id={0} />); | ||
| expect(renderCount).toBe(1); | ||
| rerender(<App id={1} />); | ||
| expect(renderCount).toBe(1); | ||
| }); | ||
|
|
||
| it('signOut is stable between re-renders', async () => { | ||
| let renderCount = 0; | ||
| const MyDemo = ({ id }: { id: number }) => { | ||
| const { signOut } = useAuth(); | ||
| useEffect(() => { | ||
| renderCount++; | ||
| }, [signOut]); | ||
| return <button>Hi {id}</button>; | ||
| }; | ||
| const App = withClerk(({ id }: { id: number; clerk }) => ( | ||
| <ClerkProvider frontendApi='https://example.com'> | ||
| <MyDemo id={id} /> | ||
| </ClerkProvider> | ||
| )); | ||
| expect(renderCount).toBe(0); | ||
| const { rerender } = render(<App id={0} />); | ||
| expect(renderCount).toBe(1); | ||
| rerender(<App id={1} />); | ||
| expect(renderCount).toBe(1); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,43 @@ | ||
| import type { ActJWTClaim, GetToken, SignOut } from '@clerk/types'; | ||
| import { useCallback, useMemo } from 'react'; | ||
|
|
||
| import { useAuthContext } from '../contexts/AuthContext'; | ||
| import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext'; | ||
| import { invalidStateError } from '../errors'; | ||
| import IsomorphicClerk from '../isomorphicClerk'; | ||
| import { createGetToken, createSignOut } from './utils'; | ||
|
|
||
| type UseAuthReturn = | ||
| | { | ||
| isLoaded: false; | ||
| isSignedIn: undefined; | ||
| userId: undefined; | ||
| sessionId: undefined; | ||
| actor: null; | ||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| } | ||
| | { | ||
| isLoaded: true; | ||
| isSignedIn: false; | ||
| userId: null; | ||
| sessionId: null; | ||
| actor: null; | ||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| } | ||
| | { | ||
| isLoaded: true; | ||
| isSignedIn: true; | ||
| userId: string; | ||
| sessionId: string; | ||
| actor: ActJWTClaim | null; | ||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| }; | ||
| type InitReturn = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better naming for this interface is the |
||
| isLoaded: false; | ||
| isSignedIn: undefined; | ||
| userId: undefined; | ||
| sessionId: undefined; | ||
| actor: null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 This should also be an undefined. It was a mistake to make it null in this scenario in the first place. |
||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| }; | ||
|
|
||
| type LoadedReturn = { | ||
| isLoaded: true; | ||
| isSignedIn: false; | ||
| userId: null; | ||
| sessionId: null; | ||
| actor: null; | ||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| }; | ||
|
|
||
| type UserReturn = { | ||
| isLoaded: true; | ||
| isSignedIn: true; | ||
| userId: string; | ||
| sessionId: string; | ||
| actor: ActJWTClaim | null; | ||
| signOut: SignOut; | ||
| getToken: GetToken; | ||
| }; | ||
|
|
||
| type UseAuth = () => UseAuthReturn; | ||
| type UseAuth = () => InitReturn | LoadedReturn | UserReturn; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's avoid creating these three different states and stick to the original type definition to avoid the extra assertions.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I use the original, TS doesn't seem to differentiate them properly and I get this error: |
||
|
|
||
| /** | ||
| * Returns the current auth state, the user and session ids and the `getToken` | ||
|
|
@@ -79,8 +82,8 @@ export const useAuth: UseAuth = () => { | |
| const { sessionId, userId, actor } = useAuthContext(); | ||
| const isomorphicClerk = useIsomorphicClerkContext(); | ||
|
|
||
| const getToken: GetToken = createGetToken(isomorphicClerk); | ||
| const signOut: SignOut = createSignOut(isomorphicClerk); | ||
| const getToken: GetToken = useCallback(createGetToken(isomorphicClerk), [isomorphicClerk]); | ||
| const signOut: SignOut = useCallback(createSignOut(isomorphicClerk), [isomorphicClerk]); | ||
|
|
||
| if (sessionId === undefined && userId === undefined) { | ||
| return { | ||
|
|
@@ -91,7 +94,7 @@ export const useAuth: UseAuth = () => { | |
| actor: null, | ||
| signOut, | ||
| getToken, | ||
| }; | ||
| } as InitReturn; | ||
| } | ||
|
|
||
| if (sessionId === null && userId === null) { | ||
|
|
@@ -103,7 +106,7 @@ export const useAuth: UseAuth = () => { | |
| actor: null, | ||
| signOut, | ||
| getToken, | ||
| }; | ||
| } as LoadedReturn; | ||
| } | ||
|
|
||
| if (!!sessionId && !!userId) { | ||
|
|
@@ -115,7 +118,7 @@ export const useAuth: UseAuth = () => { | |
| actor, | ||
| signOut, | ||
| getToken, | ||
| }; | ||
| } as UserReturn; | ||
| } | ||
|
|
||
| throw new Error(invalidStateError); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Is this type assertion required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this,
npm testwill give me this issue:Alternatively these also work: