Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
test(Eth): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed Aug 31, 2017
1 parent 5ea43ae commit b0cca2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
21 changes: 15 additions & 6 deletions tests/eth/eth-socket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ const EthSocket = require('../../src/eth/eth-socket');

describe('EthSocket', () => {
const url = 'wss://ws.blockchain.info/eth/inv'
const balanceResponse = JSON.stringify({ op: 'account_sub', account: '0xasdf', balance: '1000', nonce: 1, txHash: 'xyz' })
const balanceResponse = JSON.stringify({ op: 'account_sub', account: '0xasdf', balance: '1000', nonce: 1, txHash: 'xyz', tx: { hash: 'xyz' } })
const blockResponse = JSON.stringify({ op: 'block_sub', height: 123 })

let account
let ethWallet

let mockAccount = (address) => ({
address,
setData () {},
fetchTransaction () {},
appendTransaction () {},
isCorrectAddress (a) { return a === address }
})

beforeEach(() => {
account = { address: '0xasdf', setData () {}, fetchTransaction () {} }
account = mockAccount('0xasdf')
ethWallet = { setLatestBlock () {} }
})

Expand Down Expand Up @@ -64,11 +72,11 @@ describe('EthSocket', () => {
expect(account.setData).toHaveBeenCalledWith(jasmine.objectContaining({ balance: '1000', nonce: 1 }))
})

it('should call .fetchTransaction on message', () => {
it('should call .appendTransaction with the tx object', () => {
let handler = EthSocket.accountMessageHandler(account)
spyOn(account, 'fetchTransaction')
spyOn(account, 'appendTransaction')
handler(balanceResponse)
expect(account.fetchTransaction).toHaveBeenCalledWith('xyz')
expect(account.appendTransaction).toHaveBeenCalledWith({ hash: 'xyz' })
})

it('should do nothing for non-balance message', () => {
Expand All @@ -86,8 +94,9 @@ describe('EthSocket', () => {
})

it('should reset the balance of a legacy address', () => {
let legacyAccount = { address: '0xabcd', setData: jasmine.createSpy('setData') }
let legacyAccount = mockAccount('0xabcd')
let handler = EthSocket.accountMessageHandler(account, legacyAccount)
spyOn(legacyAccount, 'setData')
handler(JSON.stringify({ op: 'account_sub', account: '0xasdf', tx: { from: '0xabcd' } }))
expect(legacyAccount.setData).toHaveBeenCalledWith({ balance: '0' })
})
Expand Down
20 changes: 13 additions & 7 deletions tests/eth/eth-wallet-tx.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('EthWalletTx', () => {
'gasUsed': 21000
}

const mockAccount = (addr) => ({
isCorrectAddress (a) {
return a === addr
}
})

describe('instance', () => {
let tx
beforeEach(() => {
Expand Down Expand Up @@ -56,41 +62,41 @@ describe('EthWalletTx', () => {

describe('.getTxType()', () => {
it('should identify a received tx', () => {
let account = { address: '0xasdf2' }
let account = mockAccount('0xasdf2')
expect(tx.getTxType(account)).toEqual('received')
})

it('should identify a sent tx', () => {
let account = { address: '0xasdf1' }
let account = mockAccount('0xasdf1')
expect(tx.getTxType(account)).toEqual('sent')
})

it('should return null if neither sent or received', () => {
let account = { address: '0xasdf3' }
let account = mockAccount('0xasdf3')
expect(tx.getTxType(account)).toEqual(null)
})
})

describe('.isToAccount()', () => {
it('should be true if tx is to account', () => {
let account = { address: '0xasdf2' }
let account = mockAccount('0xasdf2')
expect(tx.isToAccount(account)).toEqual(true)
})

it('should be false if tx is not to account', () => {
let account = { address: '0xasdf1' }
let account = mockAccount('0xasdf1')
expect(tx.isToAccount(account)).toEqual(false)
})
})

describe('.isFromAccount()', () => {
it('should be true if tx is from account', () => {
let account = { address: '0xasdf1' }
let account = mockAccount('0xasdf1')
expect(tx.isFromAccount(account)).toEqual(true)
})

it('should be false if tx is not from account', () => {
let account = { address: '0xasdf2' }
let account = mockAccount('0xasdf2')
expect(tx.isFromAccount(account)).toEqual(false)
})
})
Expand Down

0 comments on commit b0cca2c

Please sign in to comment.