-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from HassanBahati/ft-add-useSendSignInLinkToE…
…mailMutation
- Loading branch information
Showing
4 changed files
with
150 additions
and
2 deletions.
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
119 changes: 119 additions & 0 deletions
119
packages/react/src/auth/useSendSignInLinkToEmailMutation.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,119 @@ | ||
import React from "react"; | ||
import { describe, expect, test, beforeEach } from "vitest"; | ||
import { renderHook, act, waitFor } from "@testing-library/react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { useSendSignInLinkToEmailMutation } from "./useSendSignInLinkToEmailMutation"; | ||
import { wipeAuth, auth } from "~/testing-utils"; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { retry: false }, | ||
mutations: { retry: false }, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe("useSendSignInLinkToEmailMutation", () => { | ||
const email = "[email protected]"; | ||
const actionCodeSettings = { | ||
url: `https://invertase.io/?email=${email}`, | ||
iOS: { | ||
bundleId: "com.example.ios", | ||
}, | ||
android: { | ||
packageName: "com.example.android", | ||
installApp: true, | ||
minimumVersion: "12", | ||
}, | ||
handleCodeInApp: true, | ||
}; | ||
|
||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await wipeAuth(); | ||
}); | ||
|
||
test("resets mutation state correctly", async () => { | ||
const { result } = renderHook( | ||
() => useSendSignInLinkToEmailMutation(auth), | ||
{ wrapper } | ||
); | ||
|
||
act(() => { | ||
result.current.mutate({ email, actionCodeSettings }); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
act(() => { | ||
result.current.reset(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isIdle).toBe(true); | ||
expect(result.current.data).toBeUndefined(); | ||
expect(result.current.error).toBeNull(); | ||
}); | ||
}); | ||
|
||
test("successfully sends sign-in link to email", async () => { | ||
const { result } = renderHook( | ||
() => useSendSignInLinkToEmailMutation(auth), | ||
{ wrapper } | ||
); | ||
|
||
act(() => { | ||
result.current.mutate({ email, actionCodeSettings }); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result.current.isSuccess).toBe(true); | ||
expect(result.current.error).toBeNull(); | ||
}); | ||
|
||
test("allows multiple sequential send attempts", async () => { | ||
const { result } = renderHook( | ||
() => useSendSignInLinkToEmailMutation(auth), | ||
{ wrapper } | ||
); | ||
|
||
// First attempt | ||
act(() => { | ||
result.current.mutate({ email, actionCodeSettings }); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
// Reset state | ||
act(() => { | ||
result.current.reset(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isIdle).toBe(true); | ||
expect(result.current.data).toBeUndefined(); | ||
expect(result.current.error).toBeNull(); | ||
}); | ||
|
||
// Second attempt | ||
act(() => { | ||
result.current.mutate({ email, actionCodeSettings }); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result.current.error).toBeNull(); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/react/src/auth/useSendSignInLinkToEmailMutation.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,29 @@ | ||
import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; | ||
import { | ||
type ActionCodeSettings, | ||
type Auth, | ||
AuthError, | ||
sendSignInLinkToEmail, | ||
} from "firebase/auth"; | ||
|
||
type SendSignInLinkParams = { | ||
email: string; | ||
actionCodeSettings: ActionCodeSettings; | ||
}; | ||
|
||
type AuthUseMutationOptions< | ||
TData = unknown, | ||
TError = Error, | ||
TVariables = void | ||
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">; | ||
|
||
export function useSendSignInLinkToEmailMutation( | ||
auth: Auth, | ||
options?: AuthUseMutationOptions<void, AuthError, SendSignInLinkParams> | ||
) { | ||
return useMutation<void, AuthError, SendSignInLinkParams>({ | ||
...options, | ||
mutationFn: ({ email, actionCodeSettings }) => | ||
sendSignInLinkToEmail(auth, email, actionCodeSettings), | ||
}); | ||
} |
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