forked from invertase/tanstack-query-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into ft-add-useConnectQuery
- Loading branch information
Showing
19 changed files
with
1,256 additions
and
4 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
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,101 @@ | ||
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, type User } from "firebase/auth"; | ||
import { useDeleteUserMutation } from "./useDeleteUserMutation"; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { retry: false }, | ||
mutations: { retry: false }, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe("useVerifyPasswordResetCodeMutation", () => { | ||
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("successfully verifies the reset code", async () => { | ||
const { result } = renderHook(() => useDeleteUserMutation(auth), { | ||
wrapper, | ||
}); | ||
|
||
await act(async () => { | ||
result.current.mutate(user); | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data).toBeUndefined(); | ||
}); | ||
|
||
test("resets mutation state correctly", async () => { | ||
const { result } = renderHook(() => useDeleteUserMutation(auth), { | ||
wrapper, | ||
}); | ||
|
||
act(() => { | ||
result.current.mutate(user); | ||
}); | ||
|
||
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("should call onSuccess when the user is successfully deleted", async () => { | ||
const onSuccess = vi.fn(); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useDeleteUserMutation(auth, { | ||
onSuccess, | ||
}), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
act(() => { | ||
result.current.mutate(user); | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(1); | ||
expect(result.current.data).toBeUndefined(); | ||
}); | ||
}); |
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,18 @@ | ||
import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; | ||
import { Auth, type AuthError, deleteUser, type User } from "firebase/auth"; | ||
|
||
type AuthUMutationOptions< | ||
TData = unknown, | ||
TError = Error, | ||
TVariables = void | ||
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">; | ||
|
||
export function useDeleteUserMutation( | ||
auth: Auth, | ||
options?: AuthUMutationOptions<void, AuthError, User> | ||
) { | ||
return useMutation<void, AuthError, User>({ | ||
...options, | ||
mutationFn: (user: User) => deleteUser(user), | ||
}); | ||
} |
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), | ||
}); | ||
} |
Oops, something went wrong.