Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for fee bump pipeline #113

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
)
})
})
})