-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move useIsSignedIn into a useAuth hook with { user, session, isSigned…
…In } properties
- Loading branch information
1 parent
43aa77f
commit a3c4095
Showing
8 changed files
with
99 additions
and
74 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { useAuth } from "../../src/auth/useAuth"; | ||
import { superAuthApi } from "../apis"; | ||
import { TestWrapper } from "../testWrapper"; | ||
import { expectMockSignedInUser, expectMockSignedOutUser, mockInternalServerError, mockNetworkError } from "../utils"; | ||
|
||
describe("useAuth", () => { | ||
test("returns the correct auth state if the user is signed in", async () => { | ||
const { result, rerender } = renderHook(() => useAuth(), { wrapper: TestWrapper(superAuthApi) }); | ||
|
||
expectMockSignedInUser(); | ||
|
||
rerender(); | ||
expect(result.current.isSignedIn).toBe(true); | ||
expect(result.current.session.id).toBe("123"); | ||
expect(result.current.user!.id).toBe("321"); | ||
}); | ||
|
||
test("returns the correct auth state if the user is signed out", async () => { | ||
const { result, rerender } = renderHook(() => useAuth(), { wrapper: TestWrapper(superAuthApi) }); | ||
|
||
expectMockSignedOutUser(); | ||
|
||
rerender(); | ||
expect(result.current.isSignedIn).toBe(false); | ||
expect(result.current.session.id).toBe("123"); | ||
expect(result.current.user).toBe(null); | ||
}); | ||
|
||
test("it throws when the server responds with an error", async () => { | ||
expect(() => { | ||
const { rerender } = renderHook(() => useAuth(), { wrapper: TestWrapper(superAuthApi) }); | ||
|
||
mockInternalServerError(); | ||
|
||
rerender(); | ||
}).toThrowErrorMatchingInlineSnapshot(` | ||
"[GraphQL] GGT_INTERNAL_SERVER_ERROR | ||
[GraphQL] An error occurred" | ||
`); | ||
}); | ||
|
||
test("it throws when request fails to complete", async () => { | ||
expect(() => { | ||
const { rerender } = renderHook(() => useAuth(), { wrapper: TestWrapper(superAuthApi) }); | ||
|
||
mockNetworkError(); | ||
|
||
rerender(); | ||
}).toThrowErrorMatchingInlineSnapshot(`"[Network] Network error"`); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useSession } from "./useSession"; | ||
import { useUser } from "./useUser"; | ||
|
||
/** | ||
* Used for fetching the current authentication state of the session | ||
* @returns An object with the current authentication state: `session`, `user`, and `isSignedIn` | ||
*/ | ||
export const useAuth = () => { | ||
const session = useSession(); | ||
const user = useUser(); | ||
return { session, user, isSignedIn: !!user }; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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