Skip to content

Commit

Permalink
#20 Add get trading wallet asset balance test
Browse files Browse the repository at this point in the history
  • Loading branch information
andreafspeziale authored and ceres3idoo committed Feb 25, 2019
1 parent fd7e9ce commit 030a6f9
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/unit/commands/getTradingWalletBalanceCommand.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-env node, jest */
const sandbox = require('sinon').createSandbox()

const { PrivateKeyService } = require('../../../src/services/PrivateKeyService')

const logger = require('../../../src/logger')
const TradingWalletServiceBuilder = require('../../../src/factories/TradingWalletServiceBuilder')
const PrivateKeyValidator = require('../../../src/validators/PrivateKeyValidator')

const GetTradingWalletBalanceCommandValidator = require('../../../src/validators/GetTradingWalletBalanceCommandValidator')
const GetTradingWalletBalanceCommand = require('../../../src/commands/GetBalanceCommand')

const getTradingWalletBalanceCommandValidator = new GetTradingWalletBalanceCommandValidator(logger)

const tradingWalletService = TradingWalletServiceBuilder.build()

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)

const getTradingWalletBalanceCommand = new GetTradingWalletBalanceCommand(logger, tradingWalletService,
getTradingWalletBalanceCommandValidator, privateKeyService, privateKeyValidator)

afterEach(() => {
sandbox.restore()
})

describe('tws get-balance', () => {
const from = '0x9c858489661158d1721a66319f8683925d5a8b70'
const to = '0x230cd1dc412c44bb95aa39018e2a2aed28ebadfc'
const token = '0x9727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a'

describe('should raise Error if from wallet address was not defined as expected', () => {
const invalidFromAddresses = ['NotValidEthereumAddress', [], {}, 21]
test.each(invalidFromAddresses)(`from
is not a valid ethereum address: %o`, async (invalidFrom) => {
const expectedResult = [
{
code: 'ValidationError',
field: 'from',
message: 'from needs to be an ethereum address',
},
]

const result = await getTradingWalletBalanceCommand
.executeAsync({ from: invalidFrom, to, token })

expect(result).toMatchObject(expectedResult)
})

test('from is not defined', async () => {
const from = undefined
const expectedResult = [{
code: 'ValidationError',
field: 'from',
message: 'from is required',
}]

const result = await getTradingWalletBalanceCommand.executeAsync({ from, to, token })
expect(result).toMatchObject(expectedResult)
})
})

describe('should execute GetTradingWalletBalanceCommand as expected', () => {
const draftValues = [false, undefined]
test.each(draftValues)('should return the tradingWallet asset balance with draft value = %o', async (draft) => {
const expectedTradinWalletAssetBalance = '1'
sandbox.stub(tradingWalletService.transactionLib.ethApiClient, 'transactionCallAsync')
.returns(expectedTradinWalletAssetBalance)

const result = await getTradingWalletBalanceCommand.executeAsync({ from, to, token, draft })
expect(result).toBe(expectedTradinWalletAssetBalance)
})

test('should return the transactionObjectDraft for tradingWallet asset balance', async () => {
const expectedTransactionObjectDraft = {
data: '0xf6b1b18b0000000000000000000000009727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a',
from,
to,
value: '0x0',
}

const result = await getTradingWalletBalanceCommand
.executeAsync({ from, to, token, draft: true })

expect(result).toMatchObject(expectedTransactionObjectDraft)
})
})
})

0 comments on commit 030a6f9

Please sign in to comment.