Skip to content

Commit

Permalink
test: add unit test for fee bump pipeline (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunonascdev authored Apr 1, 2024
1 parent ec59df0 commit d8924db
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ module.exports = {
testEnvironment: 'node',
collectCoverage: true,
coverageReporters: ['cobertura', 'json', 'html', 'text'],
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/*/.*\\.types.ts', '.*\\mocks.ts', './coverage/'],
collectCoverageFrom: ['./**/*.ts'],
collectCoverageFrom: [
'./**/*.ts',
'!<rootDir>/*/.*\\.types.ts',
'!<rootDir>/*/.*\\.mocks.ts',
'!<rootDir>/dist/',
'!/node_modules/',
'!/coverage/',
],
setupFilesAfterEnv: ['./setup-tests.ts'],
modulePathIgnorePatterns: ['<rootDir>/dist/'],
moduleDirectories: ['node_modules', 'src'],
Expand Down
16 changes: 9 additions & 7 deletions src/stellar-plus/core/contract-engine/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ContractEngine } from '../contract-engine'
import { Asset, Contract, SorobanRpc, xdr } from '@stellar/stellar-sdk'

import { Constants } from 'stellar-plus'
import { spec as tokenSpec, methods as tokenMethods } from 'stellar-plus/asset/soroban-token/constants'
import { CEError } from './errors'
import { methods as tokenMethods, spec as tokenSpec } from 'stellar-plus/asset/soroban-token/constants'
import { SorobanTransactionPipeline } from 'stellar-plus/core/pipelines/soroban-transaction'
import { StellarPlusError } from 'stellar-plus/error'
import { DefaultRpcHandler } from 'stellar-plus/rpc'
import { TransactionInvocation } from 'stellar-plus/types'

import { CEError } from './errors'
import { SorobanInvokeArgs } from './types'
import { SorobanTransactionPipeline } from 'stellar-plus/core/pipelines/soroban-transaction'
import { ContractEngine } from '../contract-engine'
import { ContractIdOutput, ContractWasmHashOutput } from '../pipelines/soroban-get-transaction/types'
import { Asset, Contract, SorobanRpc, xdr } from '@stellar/stellar-sdk'
import { DefaultRpcHandler } from 'stellar-plus/rpc'
import { StellarPlusError } from 'stellar-plus/error'

jest.mock('stellar-plus/core/pipelines/soroban-transaction', () => ({
SorobanTransactionPipeline: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/order */
import { Asset, Operation } from '@stellar/stellar-base'
import { Horizon, TransactionBuilder } from '@stellar/stellar-sdk'
import { AccountResponse } from '@stellar/stellar-sdk/lib/horizon'
import { Asset, Operation } from '@stellar/stellar-base'

import { testnet } from 'stellar-plus/constants'
import {
BuildTransactionPipelineInput as BTInput,
Expand Down
103 changes: 103 additions & 0 deletions src/stellar-plus/core/pipelines/fee-bump/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { FeeBumpTransaction, Transaction, TransactionBuilder } from '@stellar/stellar-sdk'

import {
FeeBumpPipelineInput as FBInput,
FeeBumpPipelineOutput as FBOutput,
} from 'stellar-plus/core/pipelines/fee-bump/types'

import { FeeBumpPipeline } from '.'

jest.mock('@stellar/stellar-sdk', () => ({
Horizon: {
Server: jest.fn(),
},
Transaction: jest.fn(() => {
return {
networkPassphrase: 'networkPassphrase',
toXDR: jest.fn(() => {
return 'toXDR'
}),
}
}),
TransactionBuilder: {
fromXDR: jest.fn(() => {
return {}
}),
buildFeeBumpTransaction: jest.fn(() => {
return {}
}),
},
}))

const MOCKED_BUMP_FEE = '101'

const MOCKED_TRANSACTION = {
networkPassphrase: 'networkPassphrase',
toXDR: jest.fn(() => 'toXDR'),
} as unknown as Transaction
const MOCKED_FEE_BUMPED_TRANSACTION: FeeBumpTransaction = {} as FeeBumpTransaction
const MOCKED_BT_INPUT: FBInput = {
innerTransaction: MOCKED_TRANSACTION,
feeBumpHeader: {
header: {
source: 'source',
fee: MOCKED_BUMP_FEE,
timeout: 2,
},
signers: [],
},
}
const MOCKED_BT_OUTPUT: FBOutput = MOCKED_FEE_BUMPED_TRANSACTION

describe('FeeBumpPipeline', () => {
let feeBumpPipeline: FeeBumpPipeline

beforeEach(() => {
jest.clearAllMocks()
})

describe('Wrap Transaction', () => {
beforeEach(() => {
feeBumpPipeline = new FeeBumpPipeline()
jest.clearAllMocks()
})

it('should wrap transaction successfully', async () => {
await expect(feeBumpPipeline.execute(MOCKED_BT_INPUT)).resolves.toEqual(MOCKED_BT_OUTPUT)
expect(TransactionBuilder.fromXDR).toHaveBeenCalledTimes(1)
expect(TransactionBuilder.buildFeeBumpTransaction).toHaveBeenCalledTimes(1)
expect(TransactionBuilder.buildFeeBumpTransaction).toHaveBeenCalledWith(
MOCKED_BT_INPUT.feeBumpHeader.header.source,
MOCKED_BT_INPUT.feeBumpHeader.header.fee,
{},
'networkPassphrase'
)
})

it('should throw error', async () => {
const mockedInput = {
...MOCKED_BT_INPUT,
feeBumpHeader: {
...MOCKED_BT_INPUT.feeBumpHeader,
header: {
...MOCKED_BT_INPUT.feeBumpHeader.header,
fee: '0',
},
},
}
;(TransactionBuilder.buildFeeBumpTransaction as unknown as jest.Mock).mockImplementationOnce(() => {
throw new Error('Error building fee bump transaction!')
})

await expect(feeBumpPipeline.execute(mockedInput)).rejects.toThrow('Unexpected error!')
expect(TransactionBuilder.fromXDR).toHaveBeenCalledTimes(1)
expect(TransactionBuilder.buildFeeBumpTransaction).toHaveBeenCalledTimes(1)
expect(TransactionBuilder.buildFeeBumpTransaction).toHaveBeenCalledWith(
MOCKED_BT_INPUT.feeBumpHeader.header.source,
'0',
{},
'networkPassphrase'
)
})
})
})

0 comments on commit d8924db

Please sign in to comment.