Skip to content

Commit

Permalink
Merge pull request #127 from HassanBahati/ft-add-useReloadMutation
Browse files Browse the repository at this point in the history
feat: add useReloadMutation hook
  • Loading branch information
Ehesp authored Dec 4, 2024
2 parents 13f336a + 59c982f commit 8c4dec1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export { useSignInWithCredentialMutation } from "./useSignInWithCredentialMutati
// useReauthenticateWithCredentialMutation
// useReauthenticateWithPopupMutation
// useReauthenticateWithRedirectMutation
// useReloadMutation
export { useReloadMutation } from "./useReloadMutation";
// useSendEmailVerificationMutation
// useUnlinkMutation
// useUpdateEmailMutation
Expand Down
70 changes: 70 additions & 0 deletions packages/react/src/auth/useReloadMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from "react";
import { describe, expect, test, beforeEach, afterEach, vi } from "vitest";
import { renderHook, act, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { auth, wipeAuth } from "~/testing-utils";
import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
type User,
} from "firebase/auth";
import { useReloadMutation } from "./useReloadMutation";

const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
});

const wrapper = ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

describe("useReloadMutation", () => {
const email = "[email protected]";
const password = "TanstackQueryFirebase#123";
let user: User;
beforeEach(async () => {
queryClient.clear();
await wipeAuth();
const userCredential = await createUserWithEmailAndPassword(
auth,
email,
password
);
user = userCredential.user;
});

afterEach(async () => {
vi.clearAllMocks();
await auth.signOut();
});

test.sequential("should successfully reloads user data", async () => {
await signInWithEmailAndPassword(auth, email, password);

const { result } = renderHook(() => useReloadMutation(), { wrapper });

act(() => result.current.mutate(user));

await waitFor(() => expect(result.current.isSuccess).toBe(true));
});

test("should handle onSuccess callback", async () => {
await signInWithEmailAndPassword(auth, email, password);

const onSuccess = vi.fn();
const { result } = renderHook(() => useReloadMutation({ onSuccess }), {
wrapper,
});

act(() => {
result.current.mutate(user);
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(onSuccess).toHaveBeenCalled();
});
});
17 changes: 17 additions & 0 deletions packages/react/src/auth/useReloadMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import { AuthError, reload, type User } from "firebase/auth";

type AuthMutationOptions<
TData = unknown,
TError = Error,
TVariables = void
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">;

export function useReloadMutation(
options?: AuthMutationOptions<void, AuthError, User>
) {
return useMutation<void, AuthError, User>({
...options,
mutationFn: (user: User) => reload(user),
});
}

0 comments on commit 8c4dec1

Please sign in to comment.