-
Notifications
You must be signed in to change notification settings - Fork 71
feat: add useSignOutMutation hook #121
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,134 @@ | ||
| import React from "react"; | ||
| import { describe, expect, test, beforeEach, vi } from "vitest"; | ||
| import { renderHook, act, waitFor } from "@testing-library/react"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { useSignOutMutation } from "./useSignOutMutation"; | ||
| import { auth, wipeAuth } from "~/testing-utils"; | ||
| import { | ||
| createUserWithEmailAndPassword, | ||
| signInWithEmailAndPassword, | ||
| } from "firebase/auth"; | ||
|
|
||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { retry: false }, | ||
| mutations: { retry: false }, | ||
| }, | ||
| }); | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ); | ||
|
|
||
| describe("useSignOutMutation", () => { | ||
| beforeEach(async () => { | ||
| queryClient.clear(); | ||
| await wipeAuth(); | ||
| }); | ||
|
|
||
| test("successfully signs out an authenticated user", async () => { | ||
| const email = "[email protected]"; | ||
| const password = "tanstackQueryFirebase#123"; | ||
|
|
||
| await createUserWithEmailAndPassword(auth, email, password); | ||
| await signInWithEmailAndPassword(auth, email, password); | ||
|
|
||
| const { result } = renderHook(() => useSignOutMutation(auth), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| result.current.mutate(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(auth.currentUser).toBeNull(); | ||
| }); | ||
|
|
||
| test("handles sign out for a non-authenticated user", async () => { | ||
| const email = "[email protected]"; | ||
| const password = "tanstackQueryFirebase#123"; | ||
|
|
||
| await createUserWithEmailAndPassword(auth, email, password); | ||
| await signInWithEmailAndPassword(auth, email, password); | ||
|
|
||
| await auth.signOut(); | ||
|
|
||
| const { result } = renderHook(() => useSignOutMutation(auth), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| result.current.mutate(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(auth.currentUser).toBeNull(); | ||
| }); | ||
|
|
||
| test("calls onSuccess callback after successful sign out", async () => { | ||
| const email = "[email protected]"; | ||
| const password = "tanstackQueryFirebase#123"; | ||
| const onSuccessMock = vi.fn(); | ||
|
|
||
| await createUserWithEmailAndPassword(auth, email, password); | ||
| await signInWithEmailAndPassword(auth, email, password); | ||
|
|
||
| const { result } = renderHook( | ||
| () => useSignOutMutation(auth, { onSuccess: onSuccessMock }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| await act(async () => { | ||
| result.current.mutate(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(onSuccessMock).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("calls onError callback on sign out failure", async () => { | ||
| const email = "[email protected]"; | ||
| const password = "tanstackQueryFirebase#123"; | ||
| const onErrorMock = vi.fn(); | ||
| const error = new Error("Sign out failed"); | ||
|
|
||
| await createUserWithEmailAndPassword(auth, email, password); | ||
| await signInWithEmailAndPassword(auth, email, password); | ||
|
|
||
| const mockSignOut = vi.spyOn(auth, "signOut").mockRejectedValueOnce(error); | ||
|
|
||
| const { result } = renderHook( | ||
| () => useSignOutMutation(auth, { onError: onErrorMock }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| await act(async () => result.current.mutate()); | ||
|
|
||
| await waitFor(() => expect(result.current.isError).toBe(true)); | ||
|
|
||
| expect(onErrorMock).toHaveBeenCalled(); | ||
| expect(result.current.error).toBe(error); | ||
| expect(result.current.isSuccess).toBe(false); | ||
| mockSignOut.mockRestore(); | ||
| }); | ||
|
|
||
| test("handles concurrent sign out attempts", async () => { | ||
| const email = "[email protected]"; | ||
| const password = "tanstackQueryFirebase#123"; | ||
|
|
||
| await createUserWithEmailAndPassword(auth, email, password); | ||
| await signInWithEmailAndPassword(auth, email, password); | ||
|
|
||
| const { result } = renderHook(() => useSignOutMutation(auth), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| // Attempt multiple concurrent sign-outs | ||
| result.current.mutate(); | ||
| result.current.mutate(); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(auth.currentUser).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or 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,18 @@ | ||
| import { useMutation, UseMutationOptions } from "@tanstack/react-query"; | ||
| import { type Auth, signOut } from "firebase/auth"; | ||
|
|
||
| type AuthUseMutationOptions< | ||
| TData = unknown, | ||
| TError = Error, | ||
| TVariables = void | ||
| > = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">; | ||
|
|
||
| export function useSignOutMutation( | ||
| auth: Auth, | ||
| options?: AuthUseMutationOptions | ||
| ) { | ||
| return useMutation<void>({ | ||
| ...options, | ||
| mutationFn: () => signOut(auth), | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.