From 560c035358101732db6c9cc3bc7ea8d0c92fad18 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 10 Oct 2024 18:55:48 +0200 Subject: [PATCH] common: fix mocked tests Signed-off-by: Gustavo Inacio --- .../__tests__/tap-pagination.test.ts | 166 ++++++++++-------- .../__tests__/validate-queries.test.ts | 3 + packages/indexer-common/src/tap-subgraph.ts | 2 + 3 files changed, 93 insertions(+), 78 deletions(-) diff --git a/packages/indexer-common/src/allocations/__tests__/tap-pagination.test.ts b/packages/indexer-common/src/allocations/__tests__/tap-pagination.test.ts index 657564954..5345f4a87 100644 --- a/packages/indexer-common/src/allocations/__tests__/tap-pagination.test.ts +++ b/packages/indexer-common/src/allocations/__tests__/tap-pagination.test.ts @@ -72,56 +72,8 @@ function paginateArray( return array.slice(startIndex, startIndex + pageSize) } -const mockQueryTapSubgraph = jest - .fn() - .mockImplementation(async (_, variables): Promise> => { - const pageSize: number = variables.pageSize - const lastId: string | undefined = variables.lastId - - const paginatedTransactions = paginateArray( - transactions, - (tx) => tx.id, - pageSize, - lastId, - ) - - return { - data: { - transactions: paginatedTransactions, - _meta: { - block: { - hash: 'blockhash', - timestamp: 100000, - }, - }, - }, - } - }) - -const mockQueryNetworkSubgraph = jest - .fn() - .mockImplementation(async (_, variables): Promise> => { - const pageSize: number = variables.pageSize - const lastId: string | undefined = variables.lastId - - const paginatedAllocations = paginateArray( - allocations, - (allocation) => allocation.id, - pageSize, - lastId, - ) - - return { - data: { - allocations: paginatedAllocations, - meta: { - block: { - hash: 'blockhash', - }, - }, - }, - } - }) +let mockQueryNetworkSubgraph: jest.Mock +let mockQueryTapSubgraph: jest.Mock jest.spyOn(TapCollector.prototype, 'startRAVProcessing').mockImplementation() const setup = () => { @@ -133,34 +85,92 @@ const setup = () => { const metrics = createMetrics() // Clearing the registry prevents duplicate metric registration in the default registry. metrics.registry.clear() - const transactionManager = null as unknown as TransactionManager - const models = null as unknown as QueryFeeModels - const tapContracts = null as unknown as TapContracts - const allocations = null as unknown as Eventual - const networkSpecification = { - indexerOptions: { voucherRedemptionThreshold: 0, finalityTime: 0 }, - networkIdentifier: 'test', - } as unknown as NetworkSpecification - - const tapSubgraph = { - query: mockQueryTapSubgraph, - } as unknown as TAPSubgraph - const networkSubgraph = { - query: mockQueryNetworkSubgraph, - } as unknown as NetworkSubgraph - - tapCollector = TapCollector.create({ - logger, - metrics, - transactionManager, - models, - tapContracts, - allocations, - networkSpecification, - - networkSubgraph, - tapSubgraph, - }) + + mockQueryTapSubgraph = jest + .fn() + .mockImplementation( + async (_, variables): Promise> => { + console.log('MOCKING IMPLEMENTATION FOR TAP SUBGRAPH') + const pageSize: number = variables.pageSize + const lastId: string | undefined = variables.lastId + + const paginatedTransactions = paginateArray( + transactions, + (tx) => tx.id, + pageSize, + lastId, + ) + + return { + data: { + transactions: paginatedTransactions, + _meta: { + block: { + hash: 'blockhash', + timestamp: 100000, + }, + }, + }, + } + }, + ) + + mockQueryNetworkSubgraph = jest + .fn() + .mockImplementation( + async (_, variables): Promise> => { + const pageSize: number = variables.pageSize + const lastId: string | undefined = variables.lastId + + const paginatedAllocations = paginateArray( + allocations, + (allocation) => allocation.id, + pageSize, + lastId, + ) + + return { + data: { + allocations: paginatedAllocations, + meta: { + block: { + hash: 'blockhash', + }, + }, + }, + } + }, + ) + { + const transactionManager = null as unknown as TransactionManager + const models = null as unknown as QueryFeeModels + const tapContracts = null as unknown as TapContracts + const allocations = null as unknown as Eventual + const networkSpecification = { + indexerOptions: { voucherRedemptionThreshold: 0, finalityTime: 0 }, + networkIdentifier: 'test', + } as unknown as NetworkSpecification + + const tapSubgraph = { + query: mockQueryTapSubgraph, + } as unknown as TAPSubgraph + const networkSubgraph = { + query: mockQueryNetworkSubgraph, + } as unknown as NetworkSubgraph + + tapCollector = TapCollector.create({ + logger, + metrics, + transactionManager, + models, + tapContracts, + allocations, + networkSpecification, + + networkSubgraph, + tapSubgraph, + }) + } } describe('TAP Pagination', () => { diff --git a/packages/indexer-common/src/allocations/__tests__/validate-queries.test.ts b/packages/indexer-common/src/allocations/__tests__/validate-queries.test.ts index 798858167..b6836867f 100644 --- a/packages/indexer-common/src/allocations/__tests__/validate-queries.test.ts +++ b/packages/indexer-common/src/allocations/__tests__/validate-queries.test.ts @@ -69,6 +69,7 @@ describe('Validate TAP queries', () => { // this subgraph is in an eventual // we check if it was called more than 0 times expect(mockedFunc).toBeCalled() + mockedFunc.mockReset() }, timeout, ) @@ -77,10 +78,12 @@ describe('Validate TAP queries', () => { 'test `findTransactionsForRavs` query is valid', async () => { const mockedFunc = jest.spyOn(tapCollector.tapSubgraph, 'query') + const result = await tapCollector['findTransactionsForRavs']([]) expect(result.transactions).toEqual([]) expect(result._meta.block.hash.length).toEqual(66) expect(mockedFunc).toBeCalledTimes(1) + mockedFunc.mockReset() }, timeout, ) diff --git a/packages/indexer-common/src/tap-subgraph.ts b/packages/indexer-common/src/tap-subgraph.ts index 8e0f2da9f..0264c18f8 100644 --- a/packages/indexer-common/src/tap-subgraph.ts +++ b/packages/indexer-common/src/tap-subgraph.ts @@ -43,6 +43,8 @@ export class TAPSubgraph { // eslint-disable-next-line @typescript-eslint/no-explicit-any variables?: Record, ): Promise> { + // magic solution for jest tests https://stackoverflow.com/questions/69976411/jest-tlswrap-open-handle-error-using-simple-node-postgres-pool-query-fixed-wit/70012434#70012434 + await Promise.resolve(process.nextTick(Boolean)) const response = await this.endpointClient.post('', { query: print(query), variables,