Skip to content

Commit d2a7ef9

Browse files
committed
test: add unit tests
1 parent 6022891 commit d2a7ef9

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createThirdwebClient } from "../../../../client/client.js";
3+
import { getClientFetch } from "../../../../utils/fetch.js";
4+
import { getUser } from "./getUser.js";
5+
6+
vi.mock("../../../../utils/fetch.js", () => ({
7+
getClientFetch: vi.fn(),
8+
}));
9+
10+
describe("getUser", () => {
11+
const mockClient = createThirdwebClient({
12+
secretKey: "secret",
13+
});
14+
15+
const mockFetch = vi.fn();
16+
beforeEach(() => {
17+
vi.clearAllMocks();
18+
vi.mocked(getClientFetch).mockReturnValue(mockFetch);
19+
});
20+
21+
it("should throw an error if no secret key is provided", async () => {
22+
await expect(
23+
getUser({
24+
client: { ...mockClient, secretKey: undefined },
25+
walletAddress: "0x123",
26+
}),
27+
).rejects.toThrow(
28+
"A secret key is required to query for users. If you're making this request from the server, please add a secret key to your client.",
29+
);
30+
});
31+
32+
it("should throw an error if no query parameter is provided", async () => {
33+
await expect(
34+
getUser({
35+
client: mockClient,
36+
}),
37+
).rejects.toThrow(
38+
"Please provide a walletAddress, email, phone, id, or externalWalletAddress to query for users.",
39+
);
40+
});
41+
42+
it("should call the correct URL with email", async () => {
43+
mockFetch.mockResolvedValueOnce({
44+
ok: true,
45+
json: async () => [{
46+
userId: "user1",
47+
walletAddress: "0x123",
48+
49+
createdAt: "2023-01-01T00:00:00Z",
50+
linkedAccounts: []
51+
}]
52+
});
53+
54+
const result = await getUser({
55+
client: mockClient,
56+
57+
});
58+
59+
expect(mockFetch).toHaveBeenCalledWith(
60+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=email&email=test%40test.com"
61+
);
62+
expect(result).toEqual({
63+
userId: "user1",
64+
walletAddress: "0x123",
65+
66+
createdAt: "2023-01-01T00:00:00Z",
67+
profiles: []
68+
});
69+
});
70+
71+
it("should call the correct URL with phone", async () => {
72+
mockFetch.mockResolvedValueOnce({
73+
ok: true,
74+
json: async () => [{
75+
userId: "user1",
76+
walletAddress: "0x123",
77+
phone: "+1234567890",
78+
createdAt: "2023-01-01T00:00:00Z",
79+
linkedAccounts: []
80+
}]
81+
});
82+
83+
const result = await getUser({
84+
client: mockClient,
85+
phone: "+1234567890"
86+
});
87+
88+
expect(mockFetch).toHaveBeenCalledWith(
89+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=phone&phone=%2B1234567890"
90+
);
91+
expect(result).toEqual({
92+
userId: "user1",
93+
walletAddress: "0x123",
94+
phone: "+1234567890",
95+
createdAt: "2023-01-01T00:00:00Z",
96+
profiles: []
97+
});
98+
});
99+
100+
it("should call the correct URL with id", async () => {
101+
mockFetch.mockResolvedValueOnce({
102+
ok: true,
103+
json: async () => [{
104+
userId: "user1",
105+
walletAddress: "0x123",
106+
createdAt: "2023-01-01T00:00:00Z",
107+
linkedAccounts: [{
108+
type: "id",
109+
details: {
110+
id: "0x456"
111+
}
112+
}]
113+
}]
114+
});
115+
116+
const result = await getUser({
117+
client: mockClient,
118+
id: "user1"
119+
});
120+
121+
expect(mockFetch).toHaveBeenCalledWith(
122+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=id&id=user1"
123+
);
124+
expect(result).toEqual({
125+
userId: "user1",
126+
walletAddress: "0x123",
127+
createdAt: "2023-01-01T00:00:00Z",
128+
profiles: [
129+
{
130+
type: "id",
131+
details: {
132+
id: "0x456"
133+
}
134+
}
135+
]
136+
});
137+
});
138+
139+
it("should call the correct URL with externalWalletAddress", async () => {
140+
mockFetch.mockResolvedValueOnce({
141+
ok: true,
142+
json: async () => [{
143+
userId: "user1",
144+
walletAddress: "0x123",
145+
createdAt: "2023-01-01T00:00:00Z",
146+
linkedAccounts: [{
147+
type: "siwe",
148+
details: {
149+
address: "0x456"
150+
}
151+
}]
152+
}]
153+
});
154+
155+
const result = await getUser({
156+
client: mockClient,
157+
externalWalletAddress: "0x456"
158+
});
159+
160+
expect(mockFetch).toHaveBeenCalledWith(
161+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=externalWalletAddress&externalWalletAddress=0x456"
162+
);
163+
expect(result).toEqual({
164+
userId: "user1",
165+
walletAddress: "0x123",
166+
createdAt: "2023-01-01T00:00:00Z",
167+
profiles: [
168+
{
169+
type: "wallet",
170+
details: {
171+
address: "0x456"
172+
}
173+
}
174+
]
175+
});
176+
});
177+
178+
179+
180+
it("should handle fetch errors", async () => {
181+
mockFetch.mockResolvedValueOnce({
182+
ok: false,
183+
});
184+
185+
await expect(
186+
getUser({
187+
client: mockClient,
188+
walletAddress: "0x123",
189+
}),
190+
).rejects.toThrow("Failed to get profiles");
191+
});
192+
193+
it("should return null if no user is found", async () => {
194+
mockFetch.mockResolvedValueOnce({
195+
ok: true,
196+
json: async () => [],
197+
});
198+
199+
const result = await getUser({
200+
client: mockClient,
201+
walletAddress: "0x123",
202+
});
203+
204+
expect(result).toBeNull();
205+
});
206+
});

0 commit comments

Comments
 (0)