-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ensureSignedIn option to useAuth hook (#176)
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,6 +165,34 @@ describe('AuthKitProvider', () => { | |
}); | ||
|
||
describe('useAuth', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call getAuth when a user is not returned when ensureSignedIn is true', async () => { | ||
// First and second calls return no user, second call returns a user | ||
(getAuthAction as jest.Mock) | ||
.mockResolvedValueOnce({ user: null, loading: true }) | ||
.mockResolvedValueOnce({ user: { email: '[email protected]' }, loading: false }); | ||
|
||
const TestComponent = () => { | ||
const auth = useAuth({ ensureSignedIn: true }); | ||
return <div data-testid="email">{auth.user?.email}</div>; | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<AuthKitProvider> | ||
<TestComponent /> | ||
</AuthKitProvider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(getAuthAction).toHaveBeenCalledTimes(2); | ||
expect(getAuthAction).toHaveBeenLastCalledWith(true); | ||
expect(getByTestId('email')).toHaveTextContent('[email protected]'); | ||
}); | ||
}); | ||
|
||
it('should throw error when used outside of AuthKitProvider', () => { | ||
const TestComponent = () => { | ||
const auth = useAuth(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters