From c7183bfa31ca52c1fa4a22613a1d3b46ec2003a7 Mon Sep 17 00:00:00 2001 From: Mitsichury Date: Wed, 18 Oct 2023 08:08:54 +0200 Subject: [PATCH] improve tu --- src/client/__tests__/getRegistry.test.ts | 22 +++++++++++++--------- src/client/getRegistry.ts | 8 ++++---- src/index.ts | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/client/__tests__/getRegistry.test.ts b/src/client/__tests__/getRegistry.test.ts index ec24d0c..fc409d1 100644 --- a/src/client/__tests__/getRegistry.test.ts +++ b/src/client/__tests__/getRegistry.test.ts @@ -1,21 +1,25 @@ +import { Web3RegistryClient } from "../Web3RegistryClient"; import { getRegistry } from "../getRegistry"; -import { Peers } from './../../peer/Peers'; +import { Peers } from '../../peer/Peers'; -jest.mock('./../Web3RegistryClient'); +jest.mock('../Web3RegistryClient'); describe("getRegistry", () => { + let fakeWebRegistryClient: Partial; + beforeEach(() => { jest.resetAllMocks(); }); - test("Conf is not complete", () => { - expect(getRegistry({}, {} as Peers, [])).toBe(null); - expect(getRegistry({ NETWORK: "rinkeby" }, {} as Peers, [])).toBe(null); - expect(getRegistry({ API_KEY: "infura"}, {} as Peers, [])).toBe(null); + test("Conf is not complete", async () => { + expect(await getRegistry({}, {} as Peers, [])).toBe(null); + expect(await getRegistry({ NETWORK: "rinkeby" }, {} as Peers, [])).toBe(null); + expect(await getRegistry({ API_KEY: "infura"}, {} as Peers, [5])).toBe(null); }); - test("ok smart contract", () => { - const newLocal = getRegistry({ API_KEY: "infura", NETWORK: "rinkeby" }, {} as Peers, []); - expect(newLocal!.constructor.name).toBe("Web3RegistryClient"); + test("ok smart contract", async () => { + const registry = await getRegistry({ API_KEY: "infura", NETWORK: "rinkeby" }, {} as Peers, [5]); + expect(registry!.constructor.name).toBe("Web3RegistryClient"); + expect(registry!.connect).toHaveBeenCalledWith(5) }); }); \ No newline at end of file diff --git a/src/client/getRegistry.ts b/src/client/getRegistry.ts index 5d9b9cf..b05c417 100644 --- a/src/client/getRegistry.ts +++ b/src/client/getRegistry.ts @@ -1,7 +1,7 @@ import { Peers } from './../peer/Peers.js'; import { Web3RegistryClient } from './Web3RegistryClient.js'; -export function getRegistry(conf: any, peers: Peers, previsousChainObserved: number[]): Web3RegistryClient | null { +export async function getRegistry(conf: any, peers: Peers, previsousChainObserved: number[]): Promise { const apiKey: string = conf.API_KEY; const network: string = conf.NETWORK; @@ -11,9 +11,9 @@ export function getRegistry(conf: any, peers: Peers, previsousChainObserved: num } const registry = new Web3RegistryClient(apiKey, peers); - previsousChainObserved.forEach(chainId => { - registry.connect(chainId) - }) + for (const chain of previsousChainObserved){ + await registry.connect(chain) + } return registry; } diff --git a/src/index.ts b/src/index.ts index a83a566..1bed475 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ if (!isNumeric(process.env.MAX_RESULTS_FOR_QUERY)) { const peers = new Peers(database, host, broadcastClient); -const registryClient = getRegistry(process.env, peers, previsousChainObserved); +const registryClient = await getRegistry(process.env, peers, previsousChainObserved); if (registryClient === null) { process.exit(3); }