Skip to content

Commit 4e0470e

Browse files
committed
refactor(useSignInWithEmailAndPasswordMutation): pass args directly into the mutation function
1 parent fd4b470 commit 4e0470e

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

packages/react/src/auth/useSignInWithEmailAndPasswordMutation.test.tsx

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import React from "react";
2-
import {
3-
describe,
4-
expect,
5-
test,
6-
beforeEach,
7-
afterEach,
8-
vi,
9-
type MockInstance,
10-
} from "vitest";
2+
import { describe, expect, test, beforeEach, afterEach, vi } from "vitest";
113
import { renderHook, act, waitFor } from "@testing-library/react";
124
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
135
import { useSignInWithEmailAndPasswordMutation } from "./useSignInWithEmailAndPasswordMutation";
@@ -41,11 +33,11 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
4133
await createUserWithEmailAndPassword(auth, email, password);
4234

4335
const { result } = renderHook(
44-
() => useSignInWithEmailAndPasswordMutation(auth, email, password),
36+
() => useSignInWithEmailAndPasswordMutation(auth),
4537
{ wrapper }
4638
);
4739

48-
await act(async () => result.current.mutate());
40+
await act(async () => result.current.mutate({ email, password }));
4941

5042
await waitFor(async () => expect(result.current.isSuccess).toBe(true));
5143

@@ -60,12 +52,12 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
6052
await createUserWithEmailAndPassword(auth, email, password);
6153

6254
const { result } = renderHook(
63-
() => useSignInWithEmailAndPasswordMutation(auth, email, wrongPassword),
55+
() => useSignInWithEmailAndPasswordMutation(auth),
6456
{ wrapper }
6557
);
6658

6759
await act(async () => {
68-
result.current.mutate();
60+
result.current.mutate({ email, password: wrongPassword });
6961
});
7062

7163
await waitFor(() => expect(result.current.isError).toBe(true));
@@ -80,12 +72,12 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
8072
const password = "tanstackQueryFirebase#123";
8173

8274
const { result } = renderHook(
83-
() => useSignInWithEmailAndPasswordMutation(auth, email, password),
75+
() => useSignInWithEmailAndPasswordMutation(auth),
8476
{ wrapper }
8577
);
8678

8779
await act(async () => {
88-
result.current.mutate();
80+
result.current.mutate({ email, password });
8981
});
9082

9183
await waitFor(() => expect(result.current.isError).toBe(true));
@@ -100,12 +92,12 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
10092
const password = "validPassword123";
10193

10294
const { result } = renderHook(
103-
() => useSignInWithEmailAndPasswordMutation(auth, email, password),
95+
() => useSignInWithEmailAndPasswordMutation(auth),
10496
{ wrapper }
10597
);
10698

10799
await act(async () => {
108-
result.current.mutate();
100+
result.current.mutate({ email, password });
109101
});
110102

111103
await waitFor(() => expect(result.current.isError).toBe(true));
@@ -120,12 +112,12 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
120112
const password = "";
121113

122114
const { result } = renderHook(
123-
() => useSignInWithEmailAndPasswordMutation(auth, email, password),
115+
() => useSignInWithEmailAndPasswordMutation(auth),
124116
{ wrapper }
125117
);
126118

127119
await act(async () => {
128-
result.current.mutate();
120+
result.current.mutate({ email, password });
129121
});
130122

131123
await waitFor(() => expect(result.current.isError).toBe(true));
@@ -142,14 +134,14 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
142134
await createUserWithEmailAndPassword(auth, email, password);
143135

144136
const { result } = renderHook(
145-
() => useSignInWithEmailAndPasswordMutation(auth, email, password),
137+
() => useSignInWithEmailAndPasswordMutation(auth),
146138
{ wrapper }
147139
);
148140

149141
// Attempt multiple concurrent sign-ins
150142
await act(async () => {
151-
result.current.mutate();
152-
result.current.mutate();
143+
result.current.mutate({ email, password });
144+
result.current.mutate({ email, password });
153145
});
154146

155147
await waitFor(() => expect(result.current.isSuccess).toBe(true));
@@ -167,14 +159,14 @@ describe("useSignInWithEmailAndPasswordMutation", () => {
167159

168160
const { result } = renderHook(
169161
() =>
170-
useSignInWithEmailAndPasswordMutation(auth, email, password, {
162+
useSignInWithEmailAndPasswordMutation(auth, {
171163
onSuccess: onSuccessMock,
172164
}),
173165
{ wrapper }
174166
);
175167

176168
await act(async () => {
177-
result.current.mutate();
169+
result.current.mutate({ email, password });
178170
});
179171

180172
await waitFor(() => expect(result.current.isSuccess).toBe(true));

packages/react/src/auth/useSignInWithEmailAndPasswordMutation.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@ import {
66
type UserCredential,
77
} from "firebase/auth";
88

9-
type SignInWithEmailAndPassword = Omit<
10-
UseMutationOptions<UserCredential, AuthError, void>,
11-
"mutationFn"
12-
>;
9+
type AuthUseMutationOptions<
10+
TData = unknown,
11+
TError = Error,
12+
TVariables = void
13+
> = Omit<UseMutationOptions<TData, TError, TVariables>, "mutationFn">;
1314

1415
export function useSignInWithEmailAndPasswordMutation(
1516
auth: Auth,
16-
email: string,
17-
password: string,
18-
options?: SignInWithEmailAndPassword
17+
options?: AuthUseMutationOptions<
18+
UserCredential,
19+
AuthError,
20+
{ email: string; password: string }
21+
>
1922
) {
20-
return useMutation<UserCredential, AuthError>({
23+
return useMutation<
24+
UserCredential,
25+
AuthError,
26+
{ email: string; password: string }
27+
>({
2128
...options,
22-
mutationFn: () => signInWithEmailAndPassword(auth, email, password),
29+
mutationFn: ({ email, password }) =>
30+
signInWithEmailAndPassword(auth, email, password),
2331
});
2432
}

0 commit comments

Comments
 (0)