From 682636b2fe7019cfcd474c51c70c4800db93c271 Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Sun, 10 Nov 2024 19:01:53 +0300 Subject: [PATCH] feat(useLinkWithCredentialMutation): add useLinkWithCredentialMutation hook --- packages/react/src/auth/index.ts | 2 +- .../useLinkWithCredentialMutation.test.tsx | 41 +++++++++++++++++++ .../src/auth/useLinkWithCredentialMutation.ts | 25 +++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 packages/react/src/auth/useLinkWithCredentialMutation.test.tsx create mode 100644 packages/react/src/auth/useLinkWithCredentialMutation.ts diff --git a/packages/react/src/auth/index.ts b/packages/react/src/auth/index.ts index 24520331..a24a5582 100644 --- a/packages/react/src/auth/index.ts +++ b/packages/react/src/auth/index.ts @@ -31,7 +31,7 @@ export { useSignInAnonymouslyMutation } from "./useSignInAnonymouslyMutation"; // useValidatePasswordMutation // useVerifyPasswordResetCodeMutation // useDeleteUserMutation -// useLinkWithCredentialMutation +export { useLinkWithCredentialMutation } from "./useLinkWithCredentialMutation"; // useLinkWithPhoneNumberMutation // useLinkWithPopupMutation // useLinkWithRedirectMutation diff --git a/packages/react/src/auth/useLinkWithCredentialMutation.test.tsx b/packages/react/src/auth/useLinkWithCredentialMutation.test.tsx new file mode 100644 index 00000000..844f0b75 --- /dev/null +++ b/packages/react/src/auth/useLinkWithCredentialMutation.test.tsx @@ -0,0 +1,41 @@ +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, type User } from "firebase/auth"; + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + }, +}); + +const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} +); + +describe("useLinkWithCredentialMutation", () => { + const email = "tqf@invertase.io"; + 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("", async () => {}); +}); diff --git a/packages/react/src/auth/useLinkWithCredentialMutation.ts b/packages/react/src/auth/useLinkWithCredentialMutation.ts new file mode 100644 index 00000000..9c7bce7a --- /dev/null +++ b/packages/react/src/auth/useLinkWithCredentialMutation.ts @@ -0,0 +1,25 @@ +import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; +import { + linkWithCredential, + type UserCredential, + type User, + type AuthCredential, + type AuthError, +} from "firebase/auth"; + +type AuthMutationOptions< + TData = unknown, + TError = Error, + TVariables = void +> = Omit, "mutationFn">; + +export function useLinkWithCredentialMutation( + user: User, + options?: AuthMutationOptions +) { + return useMutation({ + ...options, + mutationFn: (credential: AuthCredential) => + linkWithCredential(user, credential), + }); +}