-
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 #127 from HassanBahati/ft-add-useReloadMutation
feat: add useReloadMutation hook
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
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
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,70 @@ | ||
import React from "react"; | ||
import { describe, expect, test, beforeEach, afterEach, vi } from "vitest"; | ||
import { renderHook, act, waitFor } from "@testing-library/react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { auth, wipeAuth } from "~/testing-utils"; | ||
import { | ||
createUserWithEmailAndPassword, | ||
signInWithEmailAndPassword, | ||
type User, | ||
} from "firebase/auth"; | ||
import { useReloadMutation } from "./useReloadMutation"; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { retry: false }, | ||
mutations: { retry: false }, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe("useReloadMutation", () => { | ||
const email = "[email protected]"; | ||
const password = "TanstackQueryFirebase#123"; | ||
let user: User; | ||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await wipeAuth(); | ||
const userCredential = await createUserWithEmailAndPassword( | ||
auth, | ||
email, | ||
password | ||
); | ||
user = userCredential.user; | ||
}); | ||
|
||
afterEach(async () => { | ||
vi.clearAllMocks(); | ||
await auth.signOut(); | ||
}); | ||
|
||
test.sequential("should successfully reloads user data", async () => { | ||
await signInWithEmailAndPassword(auth, email, password); | ||
|
||
const { result } = renderHook(() => useReloadMutation(), { wrapper }); | ||
|
||
act(() => result.current.mutate(user)); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
}); | ||
|
||
test("should handle onSuccess callback", async () => { | ||
await signInWithEmailAndPassword(auth, email, password); | ||
|
||
const onSuccess = vi.fn(); | ||
const { result } = renderHook(() => useReloadMutation({ onSuccess }), { | ||
wrapper, | ||
}); | ||
|
||
act(() => { | ||
result.current.mutate(user); | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(onSuccess).toHaveBeenCalled(); | ||
}); | ||
}); |
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,17 @@ | ||
import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; | ||
import { AuthError, reload, type User } from "firebase/auth"; | ||
|
||
type AuthMutationOptions< | ||
TData = unknown, | ||
TError = Error, | ||
TVariables = void | ||
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">; | ||
|
||
export function useReloadMutation( | ||
options?: AuthMutationOptions<void, AuthError, User> | ||
) { | ||
return useMutation<void, AuthError, User>({ | ||
...options, | ||
mutationFn: (user: User) => reload(user), | ||
}); | ||
} |