From e8b42ba76977f68129d9c43171e25a14d6761790 Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Fri, 8 Nov 2024 23:46:39 +0300 Subject: [PATCH 1/2] feat(useValidatePasswordMutation): add useValidatePasswordMutation hook --- .../auth/useValidatePasswordMutation.test.tsx | 37 +++++++++++++++++++ .../src/auth/useValidatePasswordMutation.ts | 23 ++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 packages/react/src/auth/useValidatePasswordMutation.test.tsx create mode 100644 packages/react/src/auth/useValidatePasswordMutation.ts diff --git a/packages/react/src/auth/useValidatePasswordMutation.test.tsx b/packages/react/src/auth/useValidatePasswordMutation.test.tsx new file mode 100644 index 0000000..fb66569 --- /dev/null +++ b/packages/react/src/auth/useValidatePasswordMutation.test.tsx @@ -0,0 +1,37 @@ +import React from "react"; +import { describe, expect, test, beforeEach, afterEach } from "vitest"; +import { renderHook, act, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { useValidatePasswordMutation } from "./useValidatePasswordMutation"; +import { auth, wipeAuth } from "~/testing-utils"; + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + }, +}); + +const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} +); + +describe("useValidatePasswordMutation", () => { + const validPassword = "ValidPassword123!"; + const invalidPassword = "short"; + + beforeEach(async () => { + queryClient.clear(); + await wipeAuth(); + }); + + test("validates a password successfully", async () => { + const { result } = renderHook(() => useValidatePasswordMutation(auth), { + wrapper, + }); + + act(() => { + result.current.mutate(validPassword); + }); + }); +}); diff --git a/packages/react/src/auth/useValidatePasswordMutation.ts b/packages/react/src/auth/useValidatePasswordMutation.ts new file mode 100644 index 0000000..d90ec36 --- /dev/null +++ b/packages/react/src/auth/useValidatePasswordMutation.ts @@ -0,0 +1,23 @@ +import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; +import { + type Auth, + type AuthError, + type PasswordValidationStatus, + validatePassword, +} from "firebase/auth"; + +type AuthUseMutationOptions< + TData = unknown, + TError = Error, + TVariables = void +> = Omit, "mutationFn">; + +export function useValidatePasswordMutation( + auth: Auth, + options?: AuthUseMutationOptions +) { + return useMutation({ + ...options, + mutationFn: (password: string) => validatePassword(auth, password), + }); +} From 57e67bfae7ae8586eced99132c5eaded90f4640b Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Sat, 9 Nov 2024 12:41:07 +0300 Subject: [PATCH 2/2] chore(useValidatePasswordMutation): export useValidatePasswordMutation hook --- packages/react/src/auth/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/auth/index.ts b/packages/react/src/auth/index.ts index 2452033..d9b9cdb 100644 --- a/packages/react/src/auth/index.ts +++ b/packages/react/src/auth/index.ts @@ -28,7 +28,7 @@ export { useSignInAnonymouslyMutation } from "./useSignInAnonymouslyMutation"; // useSignInWithRedirectMutation // useSignOutMutation // useUpdateCurrentUserMutation -// useValidatePasswordMutation +export { useValidatePasswordMutation } from "./useValidatePasswordMutation"; // useVerifyPasswordResetCodeMutation // useDeleteUserMutation // useLinkWithCredentialMutation