Skip to content

Commit a5a8d2b

Browse files
authored
Merge pull request #125 from HassanBahati/ft-add-useDeleteUserMutation
feat: add useDeleteUserMutation hook
2 parents 8c4dec1 + 9c4d36f commit a5a8d2b

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

packages/react/src/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export { useSignInWithCredentialMutation } from "./useSignInWithCredentialMutati
3131
// useValidatePasswordMutation
3232
// useVerifyPasswordResetCodeMutation
3333
// useDeleteUserMutation
34+
export { useDeleteUserMutation } from "./useDeleteUserMutation";
3435
// useLinkWithCredentialMutation
3536
// useLinkWithPhoneNumberMutation
3637
// useLinkWithPopupMutation
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from "react";
2+
import { describe, expect, test, beforeEach, afterEach, vi } from "vitest";
3+
import { renderHook, act, waitFor } from "@testing-library/react";
4+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5+
import { auth, wipeAuth } from "~/testing-utils";
6+
import { createUserWithEmailAndPassword, type User } from "firebase/auth";
7+
import { useDeleteUserMutation } from "./useDeleteUserMutation";
8+
9+
const queryClient = new QueryClient({
10+
defaultOptions: {
11+
queries: { retry: false },
12+
mutations: { retry: false },
13+
},
14+
});
15+
16+
const wrapper = ({ children }: { children: React.ReactNode }) => (
17+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
18+
);
19+
20+
describe("useVerifyPasswordResetCodeMutation", () => {
21+
const email = "[email protected]";
22+
const password = "TanstackQueryFirebase#123";
23+
let user: User;
24+
25+
beforeEach(async () => {
26+
queryClient.clear();
27+
await wipeAuth();
28+
const userCredential = await createUserWithEmailAndPassword(
29+
auth,
30+
email,
31+
password
32+
);
33+
user = userCredential.user;
34+
});
35+
36+
afterEach(async () => {
37+
vi.clearAllMocks();
38+
await auth.signOut();
39+
});
40+
41+
test("successfully verifies the reset code", async () => {
42+
const { result } = renderHook(() => useDeleteUserMutation(auth), {
43+
wrapper,
44+
});
45+
46+
await act(async () => {
47+
result.current.mutate(user);
48+
});
49+
50+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
51+
52+
expect(result.current.data).toBeUndefined();
53+
});
54+
55+
test("resets mutation state correctly", async () => {
56+
const { result } = renderHook(() => useDeleteUserMutation(auth), {
57+
wrapper,
58+
});
59+
60+
act(() => {
61+
result.current.mutate(user);
62+
});
63+
64+
await waitFor(() => {
65+
expect(result.current.isSuccess).toBe(true);
66+
});
67+
68+
act(() => {
69+
result.current.reset();
70+
});
71+
72+
await waitFor(() => {
73+
expect(result.current.isIdle).toBe(true);
74+
expect(result.current.data).toBeUndefined();
75+
expect(result.current.error).toBeNull();
76+
});
77+
});
78+
79+
test("should call onSuccess when the user is successfully deleted", async () => {
80+
const onSuccess = vi.fn();
81+
82+
const { result } = renderHook(
83+
() =>
84+
useDeleteUserMutation(auth, {
85+
onSuccess,
86+
}),
87+
{
88+
wrapper,
89+
}
90+
);
91+
92+
act(() => {
93+
result.current.mutate(user);
94+
});
95+
96+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
97+
98+
expect(onSuccess).toHaveBeenCalledTimes(1);
99+
expect(result.current.data).toBeUndefined();
100+
});
101+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
2+
import { Auth, type AuthError, deleteUser, type User } from "firebase/auth";
3+
4+
type AuthUMutationOptions<
5+
TData = unknown,
6+
TError = Error,
7+
TVariables = void
8+
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">;
9+
10+
export function useDeleteUserMutation(
11+
auth: Auth,
12+
options?: AuthUMutationOptions<void, AuthError, User>
13+
) {
14+
return useMutation<void, AuthError, User>({
15+
...options,
16+
mutationFn: (user: User) => deleteUser(user),
17+
});
18+
}

0 commit comments

Comments
 (0)