-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathuserUserGetIdTokenResultMutation.test.tsx
More file actions
139 lines (116 loc) · 4.04 KB
/
userUserGetIdTokenResultMutation.test.tsx
File metadata and controls
139 lines (116 loc) · 4.04 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
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, renderHook, waitFor } from "@testing-library/react";
import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from "firebase/auth";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { auth, wipeAuth } from "~/testing-utils";
import { userUserGetIdTokenResultMutation } from "./userUserGetIdTokenResultMutation";
import { queryClient, wrapper } from "../../utils";
describe("userUserGetIdTokenResultMutation", () => {
const email = "tqf@invertase.io";
const password = "TanstackQueryFirebase#123";
beforeEach(async () => {
queryClient.clear();
await wipeAuth();
await createUserWithEmailAndPassword(auth, email, password);
});
afterEach(async () => {
vi.clearAllMocks();
await auth.signOut();
});
test("successfully retrieves ID token result with all properties", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const { result } = renderHook(
() => userUserGetIdTokenResultMutation(user),
{ wrapper }
);
await act(async () => {
await result.current.mutateAsync();
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
const tokenResult = result.current.data;
// Verify all IdTokenResult properties exist and have correct types
expect(tokenResult?.authTime).toBeTypeOf("string");
expect(tokenResult?.issuedAtTime).toBeTypeOf("string");
expect(tokenResult?.expirationTime).toBeTypeOf("string");
expect(tokenResult?.token).toBeTypeOf("string");
expect(tokenResult?.claims).toBeTypeOf("object");
expect(
typeof tokenResult?.signInProvider === "string" ||
tokenResult?.signInProvider === null
).toBe(true);
expect(
typeof tokenResult?.signInSecondFactor === "string" ||
tokenResult?.signInSecondFactor === null
).toBe(true);
});
test("can get token result with forceRefresh option", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const { result } = renderHook(
() =>
userUserGetIdTokenResultMutation(user, {
auth: { forceRefresh: true },
}),
{ wrapper }
);
await act(async () => {
await result.current.mutateAsync();
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
const tokenResult = result.current.data;
expect(tokenResult?.token).toBeTypeOf("string");
expect(tokenResult?.claims).toBeTypeOf("object");
});
test("executes onSuccess callback with token result", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const onSuccess = vi.fn();
const { result } = renderHook(
() => userUserGetIdTokenResultMutation(user, { onSuccess }),
{ wrapper }
);
await act(async () => {
await result.current.mutateAsync();
});
await waitFor(() => expect(onSuccess).toHaveBeenCalled());
const tokenResult = onSuccess.mock.calls[0][0];
expect(tokenResult.token).toBeTypeOf("string");
expect(tokenResult.claims).toBeTypeOf("object");
expect(tokenResult.authTime).toBeTypeOf("string");
});
test("verifies signInProvider for password authentication", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const { result } = renderHook(
() => userUserGetIdTokenResultMutation(user),
{ wrapper }
);
await act(async () => {
await result.current.mutateAsync();
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
const tokenResult = result.current.data;
expect(tokenResult?.signInProvider).toBe("password");
expect(tokenResult?.signInSecondFactor).toBeNull();
});
});