Skip to content

Commit 23b9a6f

Browse files
committed
feat: add tests for main class and factory
1 parent 27e49c7 commit 23b9a6f

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

test/core/uniDevKitV4.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { UniDevKitV4 } from "@/core/uniDevKitV4";
2+
import type { UniDevKitV4Config } from "@/types/core";
3+
import { beforeEach, describe, expect, it } from "vitest";
4+
5+
describe("UniDevKitV4", () => {
6+
let config: UniDevKitV4Config;
7+
let sdk: UniDevKitV4;
8+
9+
beforeEach(() => {
10+
config = {
11+
chainId: 1,
12+
rpcUrl: "https://eth.llamarpc.com",
13+
contracts: {
14+
poolManager: "0x1234567890123456789012345678901234567890",
15+
positionDescriptor: "0x1234567890123456789012345678901234567890",
16+
positionManager: "0x1234567890123456789012345678901234567890",
17+
quoter: "0x1234567890123456789012345678901234567890",
18+
stateView: "0x1234567890123456789012345678901234567890",
19+
universalRouter: "0x1234567890123456789012345678901234567890",
20+
permit2: "0x1234567890123456789012345678901234567890",
21+
},
22+
};
23+
sdk = new UniDevKitV4(config);
24+
});
25+
26+
it("should initialize with correct config", () => {
27+
expect(sdk.getChainId()).toBe(config.chainId);
28+
expect(sdk.getContracts()).toEqual(config.contracts);
29+
});
30+
31+
it("should get contract address", () => {
32+
expect(sdk.getContractAddress("quoter")).toBe(config.contracts.quoter);
33+
});
34+
35+
it("should throw error for non-existent contract", () => {
36+
// @ts-expect-error Testing invalid contract name
37+
expect(() => sdk.getContractAddress("invalid")).toThrow();
38+
});
39+
40+
it("should update config", () => {
41+
const newConfig: UniDevKitV4Config = {
42+
...config,
43+
chainId: 5,
44+
rpcUrl: "https://goerli.infura.io/v3/your-api-key",
45+
};
46+
sdk.updateConfig(newConfig);
47+
expect(sdk.getChainId()).toBe(newConfig.chainId);
48+
});
49+
50+
it("should create client with custom native currency", () => {
51+
const customConfig: UniDevKitV4Config = {
52+
...config,
53+
nativeCurrency: {
54+
name: "Custom Token",
55+
symbol: "CTK",
56+
decimals: 18,
57+
},
58+
};
59+
const customSdk = new UniDevKitV4(customConfig);
60+
expect(customSdk.getClient()).toBeDefined();
61+
});
62+
});
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import {
2+
createInstance,
3+
getInstance,
4+
listChainIds,
5+
removeInstance,
6+
resetInstances,
7+
} from "@/core/uniDevKitV4Factory";
8+
import type { UniDevKitV4Config } from "@/types/core";
9+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
10+
11+
describe("UniDevKitV4Factory", () => {
12+
let config: UniDevKitV4Config;
13+
14+
beforeEach(() => {
15+
config = {
16+
chainId: 1,
17+
rpcUrl: "https://eth.llamarpc.com",
18+
contracts: {
19+
poolManager: "0x1234567890123456789012345678901234567890",
20+
positionDescriptor: "0x1234567890123456789012345678901234567890",
21+
positionManager: "0x1234567890123456789012345678901234567890",
22+
quoter: "0x1234567890123456789012345678901234567890",
23+
stateView: "0x1234567890123456789012345678901234567890",
24+
universalRouter: "0x1234567890123456789012345678901234567890",
25+
permit2: "0x1234567890123456789012345678901234567890",
26+
},
27+
};
28+
});
29+
30+
afterEach(() => {
31+
resetInstances();
32+
});
33+
34+
it("should create and get instance", () => {
35+
const instance = createInstance(config);
36+
const retrievedInstance = getInstance(config.chainId);
37+
expect(retrievedInstance).toBe(instance);
38+
});
39+
40+
it("should reuse existing instance", () => {
41+
const instance1 = createInstance(config);
42+
const instance2 = createInstance(config);
43+
expect(instance1).toBe(instance2);
44+
});
45+
46+
it("should list chain IDs", () => {
47+
createInstance(config);
48+
const chainIds = listChainIds();
49+
expect(chainIds).toContain(config.chainId);
50+
});
51+
52+
it("should remove instance", () => {
53+
createInstance(config);
54+
removeInstance(config.chainId);
55+
expect(() => getInstance(config.chainId)).toThrow();
56+
});
57+
58+
it("should reset all instances", () => {
59+
createInstance(config);
60+
resetInstances();
61+
expect(() => getInstance(config.chainId)).toThrow();
62+
});
63+
64+
it("should throw when getting non-existent instance", () => {
65+
expect(() => getInstance(999)).toThrow();
66+
});
67+
68+
it("should get default instance when only one chain is configured", () => {
69+
const instance = createInstance(config);
70+
const defaultInstance = getInstance();
71+
expect(defaultInstance).toBe(instance);
72+
});
73+
74+
it("should throw when getting default instance with multiple chains", () => {
75+
createInstance(config);
76+
createInstance({ ...config, chainId: 5 });
77+
expect(() => getInstance()).toThrow(
78+
"Multiple instances found. Please specify a chain ID. Available chains: 1, 5",
79+
);
80+
});
81+
82+
describe("Multiple chain configurations", () => {
83+
it("should handle multiple chains with different RPCs", () => {
84+
const mainnet = createInstance(config);
85+
const goerli = createInstance({
86+
...config,
87+
chainId: 5,
88+
rpcUrl: "https://goerli.infura.io/v3/your-api-key",
89+
});
90+
const sepolia = createInstance({
91+
...config,
92+
chainId: 11155111,
93+
rpcUrl: "https://sepolia.infura.io/v3/your-api-key",
94+
});
95+
96+
expect(getInstance(1)).toBe(mainnet);
97+
expect(getInstance(5)).toBe(goerli);
98+
expect(getInstance(11155111)).toBe(sepolia);
99+
});
100+
101+
it("should handle multiple chains with different native currencies", () => {
102+
const mainnet = createInstance(config);
103+
const polygon = createInstance({
104+
...config,
105+
chainId: 137,
106+
rpcUrl: "https://polygon-rpc.com",
107+
nativeCurrency: {
108+
name: "MATIC",
109+
symbol: "MATIC",
110+
decimals: 18,
111+
},
112+
});
113+
114+
expect(getInstance(1)).toBe(mainnet);
115+
expect(getInstance(137)).toBe(polygon);
116+
});
117+
118+
it("should list all configured chain IDs", () => {
119+
createInstance(config); // mainnet
120+
createInstance({ ...config, chainId: 5 }); // goerli
121+
createInstance({ ...config, chainId: 137 }); // polygon
122+
123+
const chainIds = listChainIds();
124+
expect(chainIds).toHaveLength(3);
125+
expect(chainIds).toContain(1);
126+
expect(chainIds).toContain(5);
127+
expect(chainIds).toContain(137);
128+
});
129+
130+
it("should remove specific chain instance", () => {
131+
createInstance(config); // mainnet
132+
createInstance({ ...config, chainId: 5 }); // goerli
133+
createInstance({ ...config, chainId: 137 }); // polygon
134+
135+
removeInstance(5);
136+
const chainIds = listChainIds();
137+
expect(chainIds).toHaveLength(2);
138+
expect(chainIds).not.toContain(5);
139+
expect(() => getInstance(5)).toThrow();
140+
});
141+
});
142+
});

0 commit comments

Comments
 (0)