-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuseReauthenticateWithPopupMutation.test.tsx
More file actions
142 lines (120 loc) · 3.66 KB
/
Copy pathuseReauthenticateWithPopupMutation.test.tsx
File metadata and controls
142 lines (120 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { renderHook, waitFor } from "@testing-library/react";
import {
GoogleAuthProvider,
type User,
type UserCredential,
reauthenticateWithPopup,
AuthError,
} from "firebase/auth";
import { useReauthenticateWithPopupMutation } from "./useReauthenticateWithPopupMutation";
import { describe, test, expect, beforeEach, vi } from "vitest";
import { wrapper, queryClient } from "../../utils";
vi.mock("firebase/auth", async () => {
const actual = await vi.importActual<typeof import("firebase/auth")>(
"firebase/auth"
);
return {
...actual,
reauthenticateWithPopup: vi.fn(),
};
});
describe("useReauthenticateWithPopupMutation", () => {
const mockUser = { uid: "test-uid", email: "test@example.com" } as User;
const mockCredential = {
user: mockUser,
providerId: "google.com",
operationType: "reauthenticate",
} as UserCredential;
beforeEach(async () => {
queryClient.clear();
vi.clearAllMocks();
});
test("should successfully reauthenticate with popup", async () => {
const provider = new GoogleAuthProvider();
vi.mocked(reauthenticateWithPopup).mockResolvedValueOnce(mockCredential);
const { result } = renderHook(
() => useReauthenticateWithPopupMutation(provider),
{ wrapper }
);
result.current.mutate({
user: mockUser,
});
await waitFor(() => {
expect(reauthenticateWithPopup).toHaveBeenCalledWith(
mockUser,
provider,
undefined
);
expect(result.current.data).toBe(mockCredential);
});
});
test("should handle popup closed by user", async () => {
const provider = new GoogleAuthProvider();
const mockError: Partial<AuthError> = {
code: "auth/popup-closed-by-user",
message: "The popup has been closed by the user",
name: "AuthError",
};
vi.mocked(reauthenticateWithPopup).mockRejectedValueOnce(mockError);
const { result } = renderHook(
() => useReauthenticateWithPopupMutation(provider),
{ wrapper }
);
result.current.mutate({
user: mockUser,
});
await waitFor(() => {
expect(result.current.isError).toBe(true);
expect(result.current.error?.code).toBe("auth/popup-closed-by-user");
});
});
test("should handle optional resolver parameter", async () => {
const provider = new GoogleAuthProvider();
const mockResolver = {
_popupRedirectResolver: "mock",
};
vi.mocked(reauthenticateWithPopup).mockResolvedValueOnce(mockCredential);
const { result } = renderHook(
() => useReauthenticateWithPopupMutation(provider),
{ wrapper }
);
result.current.mutate({
user: mockUser,
resolver: mockResolver,
});
await waitFor(() => {
expect(reauthenticateWithPopup).toHaveBeenCalledWith(
mockUser,
provider,
mockResolver
);
});
});
test("should call lifecycle hooks in correct order", async () => {
const provider = new GoogleAuthProvider();
const lifecycleCalls: string[] = [];
vi.mocked(reauthenticateWithPopup).mockResolvedValueOnce(mockCredential);
const { result } = renderHook(
() =>
useReauthenticateWithPopupMutation(provider, {
onSettled: () => {
lifecycleCalls.push("onSettled");
},
onSuccess: () => {
lifecycleCalls.push("onSuccess");
},
onError: () => {
lifecycleCalls.push("onError");
},
}),
{ wrapper }
);
result.current.mutate({
user: mockUser,
});
await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
expect(lifecycleCalls).toEqual(["onSuccess", "onSettled"]);
});
});
});