|
| 1 | +import { act, renderHook, waitFor } from "@testing-library/react"; |
| 2 | +import { |
| 3 | + createUserWithEmailAndPassword, |
| 4 | + sendPasswordResetEmail, |
| 5 | +} from "firebase/auth"; |
| 6 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 7 | +import { auth, expectFirebaseError, wipeAuth } from "~/testing-utils"; |
| 8 | +import { useConfirmPasswordResetMutation } from "./useConfirmPasswordResetMutation"; |
| 9 | +import { waitForPasswordResetCode } from "./utils"; |
| 10 | +import { queryClient, wrapper } from "../../utils"; |
| 11 | + |
| 12 | +describe("useConfirmPasswordResetMutation", () => { |
| 13 | + const email = "[email protected]"; |
| 14 | + const password = "TanstackQueryFirebase#123"; |
| 15 | + const newPassword = "NewSecurePassword#456"; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + queryClient.clear(); |
| 19 | + await wipeAuth(); |
| 20 | + await createUserWithEmailAndPassword(auth, email, password); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + await auth.signOut(); |
| 26 | + }); |
| 27 | + |
| 28 | + test("successfully resets password", async () => { |
| 29 | + await sendPasswordResetEmail(auth, email); |
| 30 | + const oobCode = await waitForPasswordResetCode(email); |
| 31 | + |
| 32 | + const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), { |
| 33 | + wrapper, |
| 34 | + }); |
| 35 | + |
| 36 | + await act(async () => { |
| 37 | + await result.current.mutateAsync({ oobCode: oobCode!, newPassword }); |
| 38 | + }); |
| 39 | + |
| 40 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 41 | + }); |
| 42 | + |
| 43 | + test("handles invalid action code", async () => { |
| 44 | + const invalidCode = "invalid-action-code"; |
| 45 | + |
| 46 | + const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), { |
| 47 | + wrapper, |
| 48 | + }); |
| 49 | + |
| 50 | + await act(async () => { |
| 51 | + try { |
| 52 | + await result.current.mutateAsync({ oobCode: invalidCode, newPassword }); |
| 53 | + } catch (error) { |
| 54 | + expectFirebaseError(error, "auth/invalid-action-code"); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 59 | + expect(result.current.error).toBeDefined(); |
| 60 | + expectFirebaseError(result.current.error, "auth/invalid-action-code"); |
| 61 | + }); |
| 62 | + |
| 63 | + test("handles empty action code", async () => { |
| 64 | + const { result } = renderHook(() => useConfirmPasswordResetMutation(auth), { |
| 65 | + wrapper, |
| 66 | + }); |
| 67 | + |
| 68 | + await act(async () => { |
| 69 | + try { |
| 70 | + await result.current.mutateAsync({ oobCode: "", newPassword }); |
| 71 | + } catch (error) { |
| 72 | + expectFirebaseError(error, "auth/internal-error"); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 77 | + expect(result.current.error).toBeDefined(); |
| 78 | + expectFirebaseError(result.current.error, "auth/internal-error"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("executes onSuccess callback", async () => { |
| 82 | + await sendPasswordResetEmail(auth, email); |
| 83 | + const oobCode = await waitForPasswordResetCode(email); |
| 84 | + const onSuccess = vi.fn(); |
| 85 | + |
| 86 | + const { result } = renderHook( |
| 87 | + () => useConfirmPasswordResetMutation(auth, { onSuccess }), |
| 88 | + { wrapper } |
| 89 | + ); |
| 90 | + |
| 91 | + await act(async () => { |
| 92 | + await result.current.mutateAsync({ oobCode: oobCode!, newPassword }); |
| 93 | + }); |
| 94 | + |
| 95 | + await waitFor(() => expect(onSuccess).toHaveBeenCalled()); |
| 96 | + }); |
| 97 | + |
| 98 | + test("executes onError callback", async () => { |
| 99 | + const invalidCode = "invalid-action-code"; |
| 100 | + const onError = vi.fn(); |
| 101 | + |
| 102 | + const { result } = renderHook( |
| 103 | + () => useConfirmPasswordResetMutation(auth, { onError }), |
| 104 | + { wrapper } |
| 105 | + ); |
| 106 | + |
| 107 | + await act(async () => { |
| 108 | + try { |
| 109 | + await result.current.mutateAsync({ oobCode: invalidCode, newPassword }); |
| 110 | + } catch (error) { |
| 111 | + expectFirebaseError(error, "auth/invalid-action-code"); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + await waitFor(() => expect(onError).toHaveBeenCalled()); |
| 116 | + expect(onError.mock.calls[0][0]).toBeDefined(); |
| 117 | + expectFirebaseError(result.current.error, "auth/invalid-action-code"); |
| 118 | + }); |
| 119 | +}); |
0 commit comments