| 
 | 1 | +import { renderHook, waitFor } from "@testing-library/react";  | 
 | 2 | +import { createUserWithEmailAndPassword } from "firebase/auth";  | 
 | 3 | +import { useFetchSignInMethodsForEmailQuery } from "./useFetchSignInMethodsForEmailQuery";  | 
 | 4 | +import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";  | 
 | 5 | +import { auth, expectFirebaseError, wipeAuth } from "~/testing-utils";  | 
 | 6 | +import { queryClient, wrapper } from "../../utils";  | 
 | 7 | + | 
 | 8 | +describe("useFetchSignInMethodsForEmailQuery", () => {  | 
 | 9 | +  const email = "tqf@invertase.io";  | 
 | 10 | +  const password = "TanstackQueryFirebase#123";  | 
 | 11 | + | 
 | 12 | +  beforeEach(async () => {  | 
 | 13 | +    queryClient.clear();  | 
 | 14 | +    await wipeAuth();  | 
 | 15 | +    const userCredential = await createUserWithEmailAndPassword(  | 
 | 16 | +      auth,  | 
 | 17 | +      email,  | 
 | 18 | +      password  | 
 | 19 | +    );  | 
 | 20 | +  });  | 
 | 21 | + | 
 | 22 | +  afterEach(async () => {  | 
 | 23 | +    vi.clearAllMocks();  | 
 | 24 | +    await auth.signOut();  | 
 | 25 | +  });  | 
 | 26 | + | 
 | 27 | +  test("should fetch sign in methods for existing user", async () => {  | 
 | 28 | +    const { result } = renderHook(  | 
 | 29 | +      () =>  | 
 | 30 | +        useFetchSignInMethodsForEmailQuery(auth, email, {  | 
 | 31 | +          queryKey: ["signInMethods", email],  | 
 | 32 | +        }),  | 
 | 33 | +      { wrapper }  | 
 | 34 | +    );  | 
 | 35 | + | 
 | 36 | +    expect(result.current.isLoading).toBe(true);  | 
 | 37 | + | 
 | 38 | +    await waitFor(() => {  | 
 | 39 | +      expect(result.current.isSuccess).toBe(true);  | 
 | 40 | +    });  | 
 | 41 | + | 
 | 42 | +    expect(result.current.data).toContain("password");  | 
 | 43 | +  });  | 
 | 44 | + | 
 | 45 | +  test("should return empty array for non-existent user", async () => {  | 
 | 46 | +    const nonExistentEmail = "nonexistent@example.com";  | 
 | 47 | + | 
 | 48 | +    const { result } = renderHook(  | 
 | 49 | +      () =>  | 
 | 50 | +        useFetchSignInMethodsForEmailQuery(auth, nonExistentEmail, {  | 
 | 51 | +          queryKey: ["signInMethods", nonExistentEmail],  | 
 | 52 | +        }),  | 
 | 53 | +      { wrapper }  | 
 | 54 | +    );  | 
 | 55 | + | 
 | 56 | +    await waitFor(() => {  | 
 | 57 | +      expect(result.current.isSuccess).toBe(true);  | 
 | 58 | +    });  | 
 | 59 | + | 
 | 60 | +    expect(result.current.data).toEqual([]);  | 
 | 61 | +  });  | 
 | 62 | + | 
 | 63 | +  test("should not fetch when enabled is false", () => {  | 
 | 64 | +    const { result } = renderHook(  | 
 | 65 | +      () =>  | 
 | 66 | +        useFetchSignInMethodsForEmailQuery(auth, email, {  | 
 | 67 | +          queryKey: ["signInMethods", email],  | 
 | 68 | +          enabled: false,  | 
 | 69 | +        }),  | 
 | 70 | +      { wrapper }  | 
 | 71 | +    );  | 
 | 72 | + | 
 | 73 | +    expect(result.current.isLoading).toBe(false);  | 
 | 74 | +    expect(result.current.isSuccess).toBe(false);  | 
 | 75 | +    expect(result.current.isError).toBe(false);  | 
 | 76 | +    expect(result.current.data).toBeUndefined();  | 
 | 77 | +  });  | 
 | 78 | + | 
 | 79 | +  test("should refetch when email changes", async () => {  | 
 | 80 | +    const newEmail = "another@example.com";  | 
 | 81 | + | 
 | 82 | +    await createUserWithEmailAndPassword(auth, newEmail, email);  | 
 | 83 | + | 
 | 84 | +    const { result, rerender } = renderHook(  | 
 | 85 | +      ({ email }) =>  | 
 | 86 | +        useFetchSignInMethodsForEmailQuery(auth, email, {  | 
 | 87 | +          queryKey: ["signInMethods", email],  | 
 | 88 | +        }),  | 
 | 89 | +      {  | 
 | 90 | +        wrapper,  | 
 | 91 | +        initialProps: { email: email },  | 
 | 92 | +      }  | 
 | 93 | +    );  | 
 | 94 | + | 
 | 95 | +    expect(result.current.isLoading).toBe(true);  | 
 | 96 | + | 
 | 97 | +    await waitFor(() => {  | 
 | 98 | +      expect(result.current.isSuccess).toBe(true);  | 
 | 99 | +      expect(result.current.isLoading).toBe(false);  | 
 | 100 | +    });  | 
 | 101 | +    expect(result.current.data).toContain("password");  | 
 | 102 | + | 
 | 103 | +    rerender({ email: newEmail });  | 
 | 104 | + | 
 | 105 | +    expect(result.current.isLoading).toBe(true);  | 
 | 106 | + | 
 | 107 | +    await waitFor(() => {  | 
 | 108 | +      expect(result.current.data).toContain("password");  | 
 | 109 | +      expect(result.current.isLoading).toBe(false);  | 
 | 110 | +    });  | 
 | 111 | +  });  | 
 | 112 | + | 
 | 113 | +  test("should handle invalid email error", async () => {  | 
 | 114 | +    const invalidEmail = "not-an-email";  | 
 | 115 | + | 
 | 116 | +    const { result } = renderHook(  | 
 | 117 | +      () =>  | 
 | 118 | +        useFetchSignInMethodsForEmailQuery(auth, invalidEmail, {  | 
 | 119 | +          queryKey: ["signInMethods", invalidEmail],  | 
 | 120 | +        }),  | 
 | 121 | +      { wrapper }  | 
 | 122 | +    );  | 
 | 123 | + | 
 | 124 | +    await waitFor(() => {  | 
 | 125 | +      expect(result.current.isError).toBe(true);  | 
 | 126 | +    });  | 
 | 127 | + | 
 | 128 | +    expectFirebaseError(result.current.error, "auth/invalid-email");  | 
 | 129 | +  });  | 
 | 130 | +});  | 
0 commit comments