Skip to content
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

feat: add useSignOutMutation hook #121

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export { useSignInWithEmailAndPasswordMutation } from "./useSignInWithEmailAndPa
// useSignInWithPhoneNumberMutation
// useSignInWithPopupMutation
// useSignInWithRedirectMutation
export { useSignOutMutation } from "./useSignOutMutation";
// useUpdateCurrentUserMutation
// useSignOutMutation
export { useUpdateCurrentUserMutation } from "./useUpdateCurrentUserMutation";
// useValidatePasswordMutation
Expand Down
134 changes: 134 additions & 0 deletions packages/react/src/auth/useSignOutMutation.test.tsx
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();
});
});
18 changes: 18 additions & 0 deletions packages/react/src/auth/useSignOutMutation.ts
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,
Ehesp marked this conversation as resolved.
Show resolved Hide resolved
TVariables = void
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">;

export function useSignOutMutation(
auth: Auth,
options?: AuthUseMutationOptions
) {
return useMutation<void>({
...options,
mutationFn: () => signOut(auth),
});
}