-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathuse-user.unit.test.tsx
116 lines (98 loc) · 3.19 KB
/
use-user.unit.test.tsx
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
import * as swrModule from "swr";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { User } from "../../types/index.js";
import { useUser } from "./use-user.js";
// Define mockMutate outside the mock factory so it can be referenced in tests
const mockMutate = vi.fn();
// Mock the SWR module, preserving original exports like SWRConfig
vi.mock("swr", async (importActual) => {
const actual = await importActual<typeof swrModule>();
return {
...actual,
default: vi.fn(() => ({
// Mock the default export (useSWR hook)
data: undefined,
error: undefined,
isLoading: true,
isValidating: false,
mutate: mockMutate
}))
};
});
describe("useUser", () => {
const mockUser: User = {
sub: "user_123",
name: "Test User",
email: "[email protected]"
};
beforeEach(() => {
// Clear mocks before each test
vi.clearAllMocks();
mockMutate.mockClear();
});
afterEach(() => {
// restoreAllMocks handles spies and mocks
vi.restoreAllMocks();
});
it("should return isLoading when no data or error", () => {
// Reset the global mock implementation for this specific test
vi.mocked(swrModule.default).mockImplementation(() => ({
data: undefined,
error: undefined,
isLoading: true,
isValidating: false,
mutate: mockMutate
}));
const result = useUser();
expect(result.isLoading).toBe(true);
expect(result.user).toBe(undefined);
expect(result.error).toBe(undefined);
expect(typeof result.invalidate).toBe("function");
});
it("should return user data when data is available", () => {
// Mock SWR default export (useSWR hook) to return user data for this test
vi.mocked(swrModule.default).mockImplementationOnce(() => ({
data: mockUser,
error: undefined,
isLoading: false,
isValidating: false,
mutate: mockMutate
}));
const result = useUser();
expect(result.isLoading).toBe(false);
expect(result.user).toBe(mockUser);
expect(result.error).toBe(null);
expect(typeof result.invalidate).toBe("function");
});
it("should return error when fetch fails", () => {
const mockError = new Error("Unauthorized");
// Mock SWR default export (useSWR hook) to return error for this test
vi.mocked(swrModule.default).mockImplementationOnce(() => ({
data: undefined,
error: mockError,
isLoading: false,
isValidating: false,
mutate: mockMutate
}));
const result = useUser();
expect(result.isLoading).toBe(false);
expect(result.user).toBe(null);
expect(result.error).toBe(mockError);
expect(typeof result.invalidate).toBe("function");
});
it("should call mutate when invalidate is called", () => {
// Mock SWR default export (useSWR hook) with mockMutate for invalidate testing
vi.mocked(swrModule.default).mockImplementationOnce(() => ({
data: mockUser,
error: undefined,
isLoading: false,
isValidating: false,
mutate: mockMutate
}));
const result = useUser();
// Call invalidate function
result.invalidate();
// Verify mutate was called
expect(mockMutate).toHaveBeenCalledTimes(1);
});
});