-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathuseUserGetIdTokenMutation.test.tsx
More file actions
92 lines (75 loc) · 2.57 KB
/
useUserGetIdTokenMutation.test.tsx
File metadata and controls
92 lines (75 loc) · 2.57 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
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 { useUserGetIdTokenMutation } from "./useUserGetIdTokenMutation";
import { queryClient, wrapper } from "../../utils";
describe("useUserGetIdTokenMutation", () => {
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 an ID token with forceRefresh true", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const { result } = renderHook(() => useUserGetIdTokenMutation(user), {
wrapper,
});
await act(async () => {
await result.current.mutateAsync(true);
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(typeof result.current.data).toBe("string");
expect(result.current.data?.length).toBeGreaterThan(0);
});
test("successfully retrieves an ID token with forceRefresh false", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const { result } = renderHook(() => useUserGetIdTokenMutation(user), {
wrapper,
});
await act(async () => {
await result.current.mutateAsync(false);
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(typeof result.current.data).toBe("string");
expect(result.current.data?.length).toBeGreaterThan(0);
});
test("executes onSuccess callback with token", async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
const { user } = userCredential;
const onSuccess = vi.fn();
const { result } = renderHook(
() => useUserGetIdTokenMutation(user, { onSuccess }),
{ wrapper }
);
await act(async () => {
await result.current.mutateAsync(true);
});
await waitFor(() => expect(onSuccess).toHaveBeenCalled());
expect(typeof onSuccess.mock.calls[0][0]).toBe("string");
expect(onSuccess.mock.calls[0][0].length).toBeGreaterThan(0);
});
});