Skip to content

Commit a7f6802

Browse files
committed
fix tests
1 parent 1ad187b commit a7f6802

File tree

2 files changed

+31
-140
lines changed

2 files changed

+31
-140
lines changed

packages/thirdweb/src/react/core/hooks/wallets/useAutoConnectCore.test.tsx

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe("useAutoConnectCore", () => {
3232
adaptedAccount: TEST_ACCOUNT_A,
3333
client: TEST_CLIENT,
3434
chain: ethereum,
35-
onDisconnect: () => {},
36-
switchChain: () => {},
35+
onDisconnect: () => { },
36+
switchChain: () => { },
3737
});
3838
const { result } = renderHook(
3939
() =>
@@ -51,7 +51,7 @@ describe("useAutoConnectCore", () => {
5151
onDisconnect: () => {
5252
console.warn(id);
5353
},
54-
switchChain: () => {},
54+
switchChain: () => { },
5555
}),
5656
),
5757
{ wrapper },
@@ -67,8 +67,8 @@ describe("useAutoConnectCore", () => {
6767
adaptedAccount: TEST_ACCOUNT_A,
6868
client: TEST_CLIENT,
6969
chain: ethereum,
70-
onDisconnect: () => {},
71-
switchChain: () => {},
70+
onDisconnect: () => { },
71+
switchChain: () => { },
7272
});
7373
const { result } = renderHook(
7474
() =>
@@ -86,7 +86,7 @@ describe("useAutoConnectCore", () => {
8686
onDisconnect: () => {
8787
console.warn(id);
8888
},
89-
switchChain: () => {},
89+
switchChain: () => { },
9090
}),
9191
),
9292
{ wrapper },
@@ -98,60 +98,4 @@ describe("useAutoConnectCore", () => {
9898
{ timeout: 1000 },
9999
);
100100
});
101-
102-
it("should call onTimeout on ... timeout", async () => {
103-
const wallet = createWalletAdapter({
104-
adaptedAccount: TEST_ACCOUNT_A,
105-
client: TEST_CLIENT,
106-
chain: ethereum,
107-
onDisconnect: () => {},
108-
switchChain: () => {},
109-
});
110-
mockStorage.setItem("thirdweb:active-wallet-id", wallet.id);
111-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
112-
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
113-
// Purposefully mock the wallet.autoConnect method to test the timeout logic
114-
wallet.autoConnect = () =>
115-
new Promise((resolve) => {
116-
setTimeout(() => {
117-
// @ts-ignore Mock purpose
118-
resolve("Connection successful");
119-
}, 2100);
120-
});
121-
renderHook(
122-
() =>
123-
useAutoConnectCore(
124-
mockStorage,
125-
{
126-
wallets: [wallet],
127-
client: TEST_CLIENT,
128-
onTimeout: () => {},
129-
timeout: 0,
130-
},
131-
(id: WalletId) =>
132-
createWalletAdapter({
133-
adaptedAccount: TEST_ACCOUNT_A,
134-
client: TEST_CLIENT,
135-
chain: ethereum,
136-
onDisconnect: () => {
137-
console.warn(id);
138-
},
139-
switchChain: () => {},
140-
}),
141-
),
142-
{ wrapper },
143-
);
144-
await waitFor(
145-
() => {
146-
expect(warnSpy).toHaveBeenCalled();
147-
expect(warnSpy).toHaveBeenCalledWith(
148-
"AutoConnect timeout: 0ms limit exceeded.",
149-
);
150-
expect(infoSpy).toHaveBeenCalled();
151-
expect(infoSpy).toHaveBeenCalledWith("TIMEOUTTED");
152-
warnSpy.mockRestore();
153-
},
154-
{ timeout: 2000 },
155-
);
156-
});
157101
});

packages/thirdweb/src/wallets/connection/autoConnectCore.test.ts

Lines changed: 25 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe("useAutoConnectCore", () => {
2929
adaptedAccount: TEST_ACCOUNT_A,
3030
client: TEST_CLIENT,
3131
chain: ethereum,
32-
onDisconnect: () => {},
33-
switchChain: () => {},
32+
onDisconnect: () => { },
33+
switchChain: () => { },
3434
});
3535

3636
expect(
@@ -49,7 +49,7 @@ describe("useAutoConnectCore", () => {
4949
onDisconnect: () => {
5050
console.warn(id);
5151
},
52-
switchChain: () => {},
52+
switchChain: () => { },
5353
}),
5454
manager,
5555
}),
@@ -63,8 +63,8 @@ describe("useAutoConnectCore", () => {
6363
adaptedAccount: TEST_ACCOUNT_A,
6464
client: TEST_CLIENT,
6565
chain: ethereum,
66-
onDisconnect: () => {},
67-
switchChain: () => {},
66+
onDisconnect: () => { },
67+
switchChain: () => { },
6868
});
6969

7070
expect(
@@ -83,74 +83,21 @@ describe("useAutoConnectCore", () => {
8383
onDisconnect: () => {
8484
console.warn(id);
8585
},
86-
switchChain: () => {},
86+
switchChain: () => { },
8787
}),
8888
manager,
8989
}),
9090
).toBe(false);
9191
});
9292

93-
it("should call onTimeout on ... timeout", async () => {
94-
vi.mocked(getUrlToken).mockReturnValue({});
95-
96-
const wallet = createWalletAdapter({
97-
adaptedAccount: TEST_ACCOUNT_A,
98-
client: TEST_CLIENT,
99-
chain: ethereum,
100-
onDisconnect: () => {},
101-
switchChain: () => {},
102-
});
103-
mockStorage.setItem("thirdweb:active-wallet-id", wallet.id);
104-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
105-
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
106-
// Purposefully mock the wallet.autoConnect method to test the timeout logic
107-
wallet.autoConnect = () =>
108-
new Promise((resolve) => {
109-
setTimeout(() => {
110-
// @ts-ignore Mock purpose
111-
resolve("Connection successful");
112-
}, 2100);
113-
});
114-
115-
await autoConnectCore({
116-
force: true,
117-
storage: mockStorage,
118-
props: {
119-
wallets: [wallet],
120-
client: TEST_CLIENT,
121-
onTimeout: () => {},
122-
timeout: 0,
123-
},
124-
createWalletFn: (id: WalletId) =>
125-
createWalletAdapter({
126-
adaptedAccount: TEST_ACCOUNT_A,
127-
client: TEST_CLIENT,
128-
chain: ethereum,
129-
onDisconnect: () => {
130-
console.warn(id);
131-
},
132-
switchChain: () => {},
133-
}),
134-
manager,
135-
});
136-
137-
expect(warnSpy).toHaveBeenCalled();
138-
expect(warnSpy).toHaveBeenCalledWith(
139-
"AutoConnect timeout: 0ms limit exceeded.",
140-
);
141-
expect(infoSpy).toHaveBeenCalled();
142-
expect(infoSpy).toHaveBeenCalledWith("TIMEOUTTED");
143-
warnSpy.mockRestore();
144-
});
145-
14693
it("should handle auth cookie storage correctly", async () => {
14794
const mockAuthCookie = "mock-auth-cookie";
14895
const wallet = createWalletAdapter({
14996
adaptedAccount: TEST_ACCOUNT_A,
15097
client: TEST_CLIENT,
15198
chain: ethereum,
152-
onDisconnect: () => {},
153-
switchChain: () => {},
99+
onDisconnect: () => { },
100+
switchChain: () => { },
154101
});
155102
vi.mocked(getUrlToken).mockReturnValue({
156103
authCookie: mockAuthCookie,
@@ -179,8 +126,8 @@ describe("useAutoConnectCore", () => {
179126
adaptedAccount: TEST_ACCOUNT_A,
180127
client: TEST_CLIENT,
181128
chain: ethereum,
182-
onDisconnect: () => {},
183-
switchChain: () => {},
129+
onDisconnect: () => { },
130+
switchChain: () => { },
184131
});
185132

186133
mockStorage.setItem("thirdweb:active-wallet-id", wallet1.id);
@@ -192,7 +139,7 @@ describe("useAutoConnectCore", () => {
192139
const addConnectedWalletSpy = vi
193140
.spyOn(manager, "connect")
194141
.mockRejectedValueOnce(new Error("Connection failed"));
195-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
142+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => { });
196143

197144
await autoConnectCore({
198145
force: true,
@@ -218,16 +165,16 @@ describe("useAutoConnectCore", () => {
218165
adaptedAccount: TEST_ACCOUNT_A,
219166
client: TEST_CLIENT,
220167
chain: ethereum,
221-
onDisconnect: () => {},
222-
switchChain: () => {},
168+
onDisconnect: () => { },
169+
switchChain: () => { },
223170
});
224171

225172
const wallet2 = createWalletAdapter({
226173
adaptedAccount: { ...TEST_ACCOUNT_A, address: "0x123" },
227174
client: TEST_CLIENT,
228175
chain: ethereum,
229-
onDisconnect: () => {},
230-
switchChain: () => {},
176+
onDisconnect: () => { },
177+
switchChain: () => { },
231178
});
232179
wallet2.id = "io.metamask" as unknown as "adapter";
233180

@@ -259,8 +206,8 @@ describe("useAutoConnectCore", () => {
259206
adaptedAccount: TEST_ACCOUNT_A,
260207
client: TEST_CLIENT,
261208
chain: ethereum,
262-
onDisconnect: () => {},
263-
switchChain: () => {},
209+
onDisconnect: () => { },
210+
switchChain: () => { },
264211
});
265212

266213
vi.mocked(getUrlToken).mockReturnValue({});
@@ -293,8 +240,8 @@ describe("useAutoConnectCore", () => {
293240
adaptedAccount: TEST_ACCOUNT_A,
294241
client: TEST_CLIENT,
295242
chain: ethereum,
296-
onDisconnect: () => {},
297-
switchChain: () => {},
243+
onDisconnect: () => { },
244+
switchChain: () => { },
298245
});
299246

300247
vi.mocked(getUrlToken).mockReturnValue({});
@@ -323,8 +270,8 @@ describe("useAutoConnectCore", () => {
323270
adaptedAccount: TEST_ACCOUNT_A,
324271
client: TEST_CLIENT,
325272
chain: ethereum,
326-
onDisconnect: () => {},
327-
switchChain: () => {},
273+
onDisconnect: () => { },
274+
switchChain: () => { },
328275
});
329276
vi.mocked(getUrlToken).mockReturnValue({
330277
authProvider: "email",
@@ -357,8 +304,8 @@ describe("useAutoConnectCore", () => {
357304
adaptedAccount: TEST_ACCOUNT_A,
358305
client: TEST_CLIENT,
359306
chain: ethereum,
360-
onDisconnect: () => {},
361-
switchChain: () => {},
307+
onDisconnect: () => { },
308+
switchChain: () => { },
362309
});
363310

364311
mockStorage.setItem("thirdweb:active-wallet-id", wallet.id);
@@ -394,8 +341,8 @@ describe("handleWalletConnection", () => {
394341
adaptedAccount: TEST_ACCOUNT_A,
395342
client: TEST_CLIENT,
396343
chain: ethereum,
397-
onDisconnect: () => {},
398-
switchChain: () => {},
344+
onDisconnect: () => { },
345+
switchChain: () => { },
399346
});
400347
it("should return the correct result", async () => {
401348
const result = await handleWalletConnection({

0 commit comments

Comments
 (0)