-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(react/auth): add useFetchSignInMethodsForEmailQuery #165
Open
HassanBahati
wants to merge
3
commits into
invertase:main
Choose a base branch
from
HassanBahati:useFetchSignInMethodsForEmailQuery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/react/src/auth/useFetchSignInMethodsForEmailQuery.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { renderHook, waitFor } from "@testing-library/react"; | ||
import { createUserWithEmailAndPassword } from "firebase/auth"; | ||
import { useFetchSignInMethodsForEmailQuery } from "./useFetchSignInMethodsForEmailQuery"; | ||
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest"; | ||
import { auth, expectFirebaseError, wipeAuth } from "~/testing-utils"; | ||
import { queryClient, wrapper } from "../../utils"; | ||
|
||
describe("useFetchSignInMethodsForEmailQuery", () => { | ||
const email = "[email protected]"; | ||
const password = "TanstackQueryFirebase#123"; | ||
|
||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await wipeAuth(); | ||
const userCredential = await createUserWithEmailAndPassword( | ||
auth, | ||
email, | ||
password | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
vi.clearAllMocks(); | ||
await auth.signOut(); | ||
}); | ||
|
||
test("should fetch sign in methods for existing user", async () => { | ||
const { result } = renderHook( | ||
() => | ||
useFetchSignInMethodsForEmailQuery(auth, email, { | ||
queryKey: ["signInMethods", email], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result.current.data).toContain("password"); | ||
}); | ||
|
||
test("should return empty array for non-existent user", async () => { | ||
const nonExistentEmail = "[email protected]"; | ||
|
||
const { result } = renderHook( | ||
() => | ||
useFetchSignInMethodsForEmailQuery(auth, nonExistentEmail, { | ||
queryKey: ["signInMethods", nonExistentEmail], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result.current.data).toEqual([]); | ||
}); | ||
|
||
test("should not fetch when enabled is false", () => { | ||
const { result } = renderHook( | ||
() => | ||
useFetchSignInMethodsForEmailQuery(auth, email, { | ||
queryKey: ["signInMethods", email], | ||
enabled: false, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.isSuccess).toBe(false); | ||
expect(result.current.isError).toBe(false); | ||
expect(result.current.data).toBeUndefined(); | ||
}); | ||
|
||
test("should refetch when email changes", async () => { | ||
const newEmail = "[email protected]"; | ||
|
||
await createUserWithEmailAndPassword(auth, newEmail, email); | ||
|
||
const { result, rerender } = renderHook( | ||
({ email }) => | ||
useFetchSignInMethodsForEmailQuery(auth, email, { | ||
queryKey: ["signInMethods", email], | ||
}), | ||
{ | ||
wrapper, | ||
initialProps: { email: email }, | ||
} | ||
); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
expect(result.current.data).toContain("password"); | ||
|
||
rerender({ email: newEmail }); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toContain("password"); | ||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
}); | ||
|
||
test("should handle invalid email error", async () => { | ||
const invalidEmail = "not-an-email"; | ||
|
||
const { result } = renderHook( | ||
() => | ||
useFetchSignInMethodsForEmailQuery(auth, invalidEmail, { | ||
queryKey: ["signInMethods", invalidEmail], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isError).toBe(true); | ||
}); | ||
|
||
expectFirebaseError(result.current.error, "auth/invalid-email"); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/react/src/auth/useFetchSignInMethodsForEmailQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type UseQueryOptions, useQuery } from "@tanstack/react-query"; | ||
import { | ||
fetchSignInMethodsForEmail, | ||
type Auth, | ||
type AuthError, | ||
} from "firebase/auth"; | ||
|
||
type AuthUseQueryOptions< | ||
TData = unknown, | ||
TError = Error, | ||
TVariables = void | ||
> = Omit<UseQueryOptions<TData, TError, TVariables>, "queryFn">; | ||
|
||
export function useFetchSignInMethodsForEmailQuery( | ||
auth: Auth, | ||
email: string, | ||
options: AuthUseQueryOptions<string[], AuthError, void> | ||
) { | ||
return useQuery<string[], AuthError, void>({ | ||
...options, | ||
queryFn: () => fetchSignInMethodsForEmail(auth, email), | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The email should be passed as a mutation value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this have change to be a mutation?
Currently, this is a query versus mutations for which a user can call a
mutate()
or amutateAsync
method.