|
| 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