Skip to content

Commit

Permalink
common: fix mocked tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Oct 10, 2024
1 parent 0c6589b commit 560c035
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,56 +72,8 @@ function paginateArray<T>(
return array.slice(startIndex, startIndex + pageSize)
}

const mockQueryTapSubgraph = jest
.fn()
.mockImplementation(async (_, variables): Promise<QueryResult<TapSubgraphResponse>> => {
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<QueryResult<AllocationsResponse>> => {
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<any, any, any>
let mockQueryTapSubgraph: jest.Mock<any, any, any>

jest.spyOn(TapCollector.prototype, 'startRAVProcessing').mockImplementation()
const setup = () => {
Expand All @@ -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<Allocation[]>
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<QueryResult<TapSubgraphResponse>> => {
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<QueryResult<AllocationsResponse>> => {
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<Allocation[]>
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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-common/src/tap-subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class TAPSubgraph {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
variables?: Record<string, any>,
): Promise<QueryResult<Data>> {
// 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,
Expand Down

0 comments on commit 560c035

Please sign in to comment.