Skip to content

Commit 6730c9c

Browse files
committed
test: Add test suite for fromProvider in EIP-1193 adapter
1 parent 8233b77 commit 6730c9c

File tree

3 files changed

+143
-5
lines changed

3 files changed

+143
-5
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, expect, test, vi } from "vitest";
2+
import { TEST_ACCOUNT_A } from "~test/test-wallets.js";
3+
import { ANVIL_CHAIN } from "../../../test/src/chains.js";
4+
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
5+
import { trackConnect } from "../../analytics/track/connect.js";
6+
import { fromProvider } from "./from-eip1193.js";
7+
import type { EIP1193Provider } from "./types.js";
8+
9+
vi.mock("../../analytics/track/connect.js");
10+
11+
describe("fromProvider", () => {
12+
const mockProvider: EIP1193Provider = {
13+
on: vi.fn(),
14+
removeListener: vi.fn(),
15+
request: vi.fn(),
16+
};
17+
18+
const mockAccount = TEST_ACCOUNT_A;
19+
20+
test("should create a wallet with the correct properties", () => {
21+
const wallet = fromProvider({
22+
provider: mockProvider,
23+
walletId: "io.metamask",
24+
});
25+
26+
expect(wallet.id).toBe("io.metamask");
27+
expect(wallet.subscribe).toBeDefined();
28+
expect(wallet.connect).toBeDefined();
29+
expect(wallet.disconnect).toBeDefined();
30+
expect(wallet.getAccount).toBeDefined();
31+
expect(wallet.getChain).toBeDefined();
32+
expect(wallet.getConfig).toBeDefined();
33+
expect(wallet.switchChain).toBeDefined();
34+
});
35+
36+
test("should use 'adapter' as default walletId", () => {
37+
const wallet = fromProvider({
38+
provider: mockProvider,
39+
});
40+
41+
expect(wallet.id).toBe("adapter");
42+
});
43+
44+
test("should handle async provider function", async () => {
45+
const wallet = fromProvider({
46+
provider: async () =>
47+
Promise.resolve({
48+
...mockProvider,
49+
request: () => Promise.resolve([mockAccount.address]),
50+
}),
51+
});
52+
53+
// Connect to trigger provider initialization
54+
await wallet.connect({
55+
client: TEST_CLIENT,
56+
});
57+
58+
expect(wallet.getAccount()?.address).toBe(mockAccount.address);
59+
});
60+
61+
test("should emit events on connect", async () => {
62+
const wallet = fromProvider({
63+
provider: {
64+
...mockProvider,
65+
request: () => Promise.resolve([mockAccount.address]),
66+
},
67+
});
68+
69+
const onConnectSpy = vi.fn();
70+
wallet.subscribe("onConnect", onConnectSpy);
71+
72+
await wallet.connect({
73+
client: TEST_CLIENT,
74+
chain: ANVIL_CHAIN,
75+
});
76+
77+
expect(onConnectSpy).toHaveBeenCalled();
78+
expect(trackConnect).toHaveBeenCalledWith({
79+
client: TEST_CLIENT,
80+
walletType: "adapter",
81+
walletAddress: mockAccount.address,
82+
});
83+
});
84+
85+
test("should emit events on disconnect", async () => {
86+
const wallet = fromProvider({
87+
provider: mockProvider,
88+
});
89+
90+
const onDisconnectSpy = vi.fn();
91+
wallet.subscribe("disconnect", onDisconnectSpy);
92+
93+
await wallet.disconnect();
94+
95+
expect(onDisconnectSpy).toHaveBeenCalled();
96+
});
97+
98+
test("should handle chain changes", async () => {
99+
const wallet = fromProvider({
100+
provider: {
101+
...mockProvider,
102+
request: () => Promise.resolve([mockAccount.address]),
103+
},
104+
});
105+
106+
const onChainChangedSpy = vi.fn();
107+
wallet.subscribe("chainChanged", onChainChangedSpy);
108+
109+
await wallet.connect({
110+
client: TEST_CLIENT,
111+
chain: ANVIL_CHAIN,
112+
});
113+
114+
const chain = wallet.getChain();
115+
expect(chain).toBe(ANVIL_CHAIN);
116+
});
117+
118+
test("should reset state on disconnect", async () => {
119+
const wallet = fromProvider({
120+
provider: {
121+
...mockProvider,
122+
request: () => Promise.resolve([mockAccount.address]),
123+
},
124+
});
125+
126+
mockProvider.request = vi.fn().mockResolvedValueOnce([mockAccount.address]);
127+
128+
await wallet.connect({
129+
client: TEST_CLIENT,
130+
chain: ANVIL_CHAIN,
131+
});
132+
133+
await wallet.disconnect();
134+
135+
expect(wallet.getAccount()).toBeUndefined();
136+
expect(wallet.getChain()).toBeUndefined();
137+
});
138+
});

packages/thirdweb/src/adapters/eip1193/from-eip1193.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ox__Hex from "ox/Hex";
12
import { trackConnect } from "../../analytics/track/connect.js";
23
import type { Chain } from "../../chains/types.js";
34
import { getCachedChainIfExists } from "../../chains/utils.js";
@@ -9,7 +10,6 @@ import type { Account, Wallet } from "../../wallets/interfaces/wallet.js";
910
import { createWalletEmitter } from "../../wallets/wallet-emitter.js";
1011
import type { WalletId } from "../../wallets/wallet-types.js";
1112
import type { EIP1193Provider } from "./types.js";
12-
import * as ox__Hex from "ox/Hex";
1313

1414
export type FromEip1193AdapterOptions = {
1515
provider: EIP1193Provider | (() => Promise<EIP1193Provider>);
@@ -55,7 +55,7 @@ export function fromProvider(options: FromEip1193AdapterOptions): Wallet {
5555
chain = undefined;
5656
}
5757

58-
let handleDisconnect = async () => { };
58+
let handleDisconnect = async () => {};
5959

6060
const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
6161
reset();
@@ -75,7 +75,7 @@ export function fromProvider(options: FromEip1193AdapterOptions): Wallet {
7575
};
7676

7777
return {
78-
id: options.walletId as WalletId,
78+
id,
7979
subscribe: emitter.subscribe,
8080
getConfig: () => undefined,
8181
getChain() {

packages/thirdweb/src/wallets/injected/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function connectEip1193Wallet({
7070

7171
const addr = addresses?.[0];
7272
if (!addr) {
73-
throw new Error("no accounts available");
73+
throw new Error("Failed to connect to wallet, no accounts available");
7474
}
7575

7676
// use the first account
@@ -123,7 +123,7 @@ export async function autoConnectEip1193Wallet({
123123

124124
const addr = addresses[0];
125125
if (!addr) {
126-
throw new Error("no accounts available");
126+
throw new Error("Failed to connect to wallet, no accounts available");
127127
}
128128

129129
// use the first account

0 commit comments

Comments
 (0)