-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathuse-user.test.ts
More file actions
118 lines (98 loc) · 2.93 KB
/
use-user.test.ts
File metadata and controls
118 lines (98 loc) · 2.93 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
// Import the mocked SWR module
import swr from "swr";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { User } from "../../types/index.js";
import { useUser } from "./use-user.js";
// Create a mock for SWR that we can control in our tests
vi.mock("swr", () => {
const mockMutate = vi.fn();
return {
__esModule: true,
default: vi.fn(() => ({
data: undefined,
error: undefined,
isLoading: true,
isValidating: false,
mutate: mockMutate
}))
};
});
describe("useUser", () => {
const mockUser: User = {
sub: "user_123",
name: "Test User",
email: "test@example.com"
};
// Get a reference to the mocked SWR default function
const mockSWR = vi.mocked(swr);
// Get a reference to the mockMutate function inside the mock
const mockMutate = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
// Reset the default mock implementation
mockSWR.mockImplementation(() => ({
data: undefined,
error: undefined,
isLoading: true,
isValidating: false,
mutate: mockMutate
}));
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should return isLoading when no data or error", () => {
// The default mock implementation has isLoading=true
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 to return user data
mockSWR.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 to return error
mockSWR.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 with mockMutate for invalidate testing
mockSWR.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);
});
});