-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): don't return loading state if currentUser is set
- Loading branch information
1 parent
9103f6e
commit fa53819
Showing
3 changed files
with
54 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Auth, onAuthStateChanged, User } from "firebase/auth"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { useAuthState } from "./useAuthState"; | ||
|
||
jest.mock("firebase/auth", () => { | ||
const actual = jest.requireActual("firebase/auth"); | ||
|
||
return { | ||
...actual, | ||
onAuthStateChanged: jest.fn(), | ||
}; | ||
}); | ||
|
||
const onAuthStateChangedMock = onAuthStateChanged as jest.Mock< | ||
ReturnType<typeof onAuthStateChanged>, | ||
Parameters<typeof onAuthStateChanged> | ||
>; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe("initial state", () => { | ||
it("should return currentUser when defined", () => { | ||
const currentUser = Symbol("Current User") as unknown as User; | ||
const mockAuth = { | ||
currentUser, | ||
} as Auth; | ||
|
||
onAuthStateChangedMock.mockImplementation(() => () => {}); | ||
|
||
const { result } = renderHook(() => useAuthState(mockAuth)); | ||
expect(result.current).toStrictEqual([currentUser, false, undefined]); | ||
}); | ||
|
||
it("should return undefined when currentUser is undefined", () => { | ||
const mockAuth = {} as Auth; | ||
|
||
onAuthStateChangedMock.mockImplementation(() => () => {}); | ||
|
||
const { result } = renderHook(() => useAuthState(mockAuth)); | ||
expect(result.current).toStrictEqual([undefined, true, undefined]); | ||
}); | ||
}); |
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