From 96501eb26a6df44df28227087cc3c510295dd76c Mon Sep 17 00:00:00 2001 From: Cameron Gilbert Date: Thu, 19 Oct 2023 17:18:07 -0400 Subject: [PATCH] fix(test): dedupe code --- .../batchHandlers/queryBatchHandler.test.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/indexer-nibi/src/batchHandlers/queryBatchHandler.test.ts b/packages/indexer-nibi/src/batchHandlers/queryBatchHandler.test.ts index 2d2086fc..f440f8d4 100644 --- a/packages/indexer-nibi/src/batchHandlers/queryBatchHandler.test.ts +++ b/packages/indexer-nibi/src/batchHandlers/queryBatchHandler.test.ts @@ -2,6 +2,14 @@ import { queryBatchHandler } from "./queryBatchHandler" import { communityPoolQueryString } from "../query/communityPool" import { delegationsQueryString } from "../query" +const checkFields = (objects: any[], fields: any[]) => { + objects.forEach((obj: any) => { + fields.forEach((field: string | any[]) => { + expect(obj).toHaveProperty(field) + }) + }) +} + describe("queryBatchHandler tests", () => { test("queryBatchHandler", async () => { const resp = await queryBatchHandler( @@ -20,20 +28,16 @@ describe("queryBatchHandler tests", () => { expect(resp).toHaveProperty("communityPool") expect(resp).toHaveProperty("delegations") - if ((resp.communityPool?.length ?? 0) > 0) { - const [communityPool] = resp.communityPool ?? [] - const fields = ["amount", "denom"] - fields.forEach((field: string) => { - expect(communityPool).toHaveProperty(field) - }) + if (resp.communityPool?.length) { + const [communityPool] = resp.communityPool + const communityPoolFields = ["amount", "denom"] + checkFields([communityPool], communityPoolFields) } - if ((resp.delegations?.length ?? 0) > 0) { - const [delegation] = resp.delegations ?? [] - const fields = ["amount", "delegator", "validator"] - fields.forEach((field: string) => { - expect(delegation).toHaveProperty(field) - }) + if (resp.delegations?.length) { + const [delegation] = resp.delegations + const delegationFields = ["amount", "delegator", "validator"] + checkFields([delegation], delegationFields) } }) })