|
| 1 | +import React from "react"; |
| 2 | +import { describe, expect, test, beforeEach, afterEach, vi } from "vitest"; |
| 3 | +import { renderHook, act, waitFor } from "@testing-library/react"; |
| 4 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 5 | +import { auth, wipeAuth } from "~/testing-utils"; |
| 6 | +import { createUserWithEmailAndPassword, type User } from "firebase/auth"; |
| 7 | +import { useDeleteUserMutation } from "./useDeleteUserMutation"; |
| 8 | + |
| 9 | +const queryClient = new QueryClient({ |
| 10 | + defaultOptions: { |
| 11 | + queries: { retry: false }, |
| 12 | + mutations: { retry: false }, |
| 13 | + }, |
| 14 | +}); |
| 15 | + |
| 16 | +const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 17 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 18 | +); |
| 19 | + |
| 20 | +describe("useVerifyPasswordResetCodeMutation", () => { |
| 21 | + const email = "[email protected]"; |
| 22 | + const password = "TanstackQueryFirebase#123"; |
| 23 | + let user: User; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + queryClient.clear(); |
| 27 | + await wipeAuth(); |
| 28 | + const userCredential = await createUserWithEmailAndPassword( |
| 29 | + auth, |
| 30 | + email, |
| 31 | + password |
| 32 | + ); |
| 33 | + user = userCredential.user; |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + vi.clearAllMocks(); |
| 38 | + await auth.signOut(); |
| 39 | + }); |
| 40 | + |
| 41 | + test("successfully verifies the reset code", async () => { |
| 42 | + const { result } = renderHook(() => useDeleteUserMutation(auth), { |
| 43 | + wrapper, |
| 44 | + }); |
| 45 | + |
| 46 | + await act(async () => { |
| 47 | + result.current.mutate(user); |
| 48 | + }); |
| 49 | + |
| 50 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 51 | + |
| 52 | + expect(result.current.data).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + test("resets mutation state correctly", async () => { |
| 56 | + const { result } = renderHook(() => useDeleteUserMutation(auth), { |
| 57 | + wrapper, |
| 58 | + }); |
| 59 | + |
| 60 | + act(() => { |
| 61 | + result.current.mutate(user); |
| 62 | + }); |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(result.current.isSuccess).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + act(() => { |
| 69 | + result.current.reset(); |
| 70 | + }); |
| 71 | + |
| 72 | + await waitFor(() => { |
| 73 | + expect(result.current.isIdle).toBe(true); |
| 74 | + expect(result.current.data).toBeUndefined(); |
| 75 | + expect(result.current.error).toBeNull(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + test("should call onSuccess when the user is successfully deleted", async () => { |
| 80 | + const onSuccess = vi.fn(); |
| 81 | + |
| 82 | + const { result } = renderHook( |
| 83 | + () => |
| 84 | + useDeleteUserMutation(auth, { |
| 85 | + onSuccess, |
| 86 | + }), |
| 87 | + { |
| 88 | + wrapper, |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + result.current.mutate(user); |
| 94 | + }); |
| 95 | + |
| 96 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 97 | + |
| 98 | + expect(onSuccess).toHaveBeenCalledTimes(1); |
| 99 | + expect(result.current.data).toBeUndefined(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments