diff options
author | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2017-09-28 01:57:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-28 01:57:02 +0800 |
commit | e72083f6e81f888f714125f7c050e777ea5c9bdd (patch) | |
tree | 8177fb150ae254b838cabec028b3c5e08fbc951e /test | |
parent | ff5e8746bee0d7461df02e7f2186ff7c90bfcb50 (diff) | |
parent | ca0dff06f5d9283e57b452dcc7e85c31b248b8aa (diff) | |
download | tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.gz tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.bz2 tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.lz tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.xz tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.tar.zst tangerine-wallet-browser-e72083f6e81f888f714125f7c050e777ea5c9bdd.zip |
Merge branch 'master' into filter-fixes-moar
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/mock-encryptor.js | 4 | ||||
-rw-r--r-- | test/unit/components/pending-tx-test.js | 5 | ||||
-rw-r--r-- | test/unit/keyring-controller-test.js | 167 | ||||
-rw-r--r-- | test/unit/pending-balance-test.js | 93 | ||||
-rw-r--r-- | test/unit/tx-controller-test.js | 4 |
5 files changed, 104 insertions, 169 deletions
diff --git a/test/lib/mock-encryptor.js b/test/lib/mock-encryptor.js index cdf13c507..ef229a82f 100644 --- a/test/lib/mock-encryptor.js +++ b/test/lib/mock-encryptor.js @@ -29,4 +29,8 @@ module.exports = { return 'WHADDASALT!' }, + getRandomValues () { + return 'SOO RANDO!!!1' + } + } diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 22a98bc93..fdade1042 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -34,10 +34,15 @@ describe('PendingTx', function () { const renderer = ReactTestUtils.createRenderer() const newGasPrice = '0x77359400' + const computedBalances = {} + computedBalances[Object.keys(identities)[0]] = { + ethBalance: '0x00000000000000056bc75e2d63100000', + } const props = { identities, accounts: identities, txData, + computedBalances, sendTransaction: (txMeta, event) => { // Assert changes: const result = ethUtil.addHexPrefix(txMeta.txParams.gasPrice) diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js deleted file mode 100644 index 2d9a53723..000000000 --- a/test/unit/keyring-controller-test.js +++ /dev/null @@ -1,167 +0,0 @@ -const assert = require('assert') -const KeyringController = require('../../app/scripts/keyring-controller') -const configManagerGen = require('../lib/mock-config-manager') -const ethUtil = require('ethereumjs-util') -const BN = ethUtil.BN -const mockEncryptor = require('../lib/mock-encryptor') -const sinon = require('sinon') - -describe('KeyringController', function () { - let keyringController - const password = 'password123' - const seedWords = 'puzzle seed penalty soldier say clay field arctic metal hen cage runway' - const addresses = ['eF35cA8EbB9669A35c31b5F6f249A9941a812AC1'.toLowerCase()] - const accounts = [] - // let originalKeystore - - beforeEach(function (done) { - this.sinon = sinon.sandbox.create() - window.localStorage = {} // Hacking localStorage support into JSDom - - keyringController = new KeyringController({ - configManager: configManagerGen(), - txManager: { - getTxList: () => [], - getUnapprovedTxList: () => [], - }, - ethStore: { - addAccount (acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, - }, - }) - - // Stub out the browser crypto for a mock encryptor. - // Browser crypto is tested in the integration test suite. - keyringController.encryptor = mockEncryptor - - keyringController.createNewVaultAndKeychain(password) - .then(function (newState) { - newState - done() - }) - .catch((err) => { - done(err) - }) - }) - - afterEach(function () { - // Cleanup mocks - this.sinon.restore() - }) - - describe('#createNewVaultAndKeychain', function () { - this.timeout(10000) - - it('should set a vault on the configManager', function (done) { - keyringController.store.updateState({ vault: null }) - assert(!keyringController.store.getState().vault, 'no previous vault') - keyringController.createNewVaultAndKeychain(password) - .then(() => { - const vault = keyringController.store.getState().vault - assert(vault, 'vault created') - done() - }) - .catch((reason) => { - done(reason) - }) - }) - }) - - describe('#restoreKeyring', function () { - it(`should pass a keyring's serialized data back to the correct type.`, function (done) { - const mockSerialized = { - type: 'HD Key Tree', - data: { - mnemonic: seedWords, - numberOfAccounts: 1, - }, - } - const mock = this.sinon.mock(keyringController) - - mock.expects('getBalanceAndNickname') - .exactly(1) - - keyringController.restoreKeyring(mockSerialized) - .then((keyring) => { - assert.equal(keyring.wallets.length, 1, 'one wallet restored') - return keyring.getAccounts() - }) - .then((accounts) => { - assert.equal(accounts[0], addresses[0]) - mock.verify() - done() - }) - .catch((reason) => { - done(reason) - }) - }) - }) - - describe('#createNickname', function () { - it('should add the address to the identities hash', function () { - const fakeAddress = '0x12345678' - keyringController.createNickname(fakeAddress) - const identities = keyringController.memStore.getState().identities - const identity = identities[fakeAddress] - assert.equal(identity.address, fakeAddress) - }) - }) - - describe('#saveAccountLabel', function () { - it('sets the nickname', function (done) { - const account = addresses[0] - var nick = 'Test nickname' - const identities = keyringController.memStore.getState().identities - identities[ethUtil.addHexPrefix(account)] = {} - keyringController.memStore.updateState({ identities }) - keyringController.saveAccountLabel(account, nick) - .then((label) => { - try { - assert.equal(label, nick) - const persisted = keyringController.store.getState().walletNicknames[account] - assert.equal(persisted, nick) - done() - } catch (err) { - done() - } - }) - .catch((reason) => { - done(reason) - }) - }) - }) - - describe('#getAccounts', function () { - it('returns the result of getAccounts for each keyring', function (done) { - keyringController.keyrings = [ - { getAccounts () { return Promise.resolve([1, 2, 3]) } }, - { getAccounts () { return Promise.resolve([4, 5, 6]) } }, - ] - - keyringController.getAccounts() - .then((result) => { - assert.deepEqual(result, [1, 2, 3, 4, 5, 6]) - done() - }) - }) - }) - - describe('#addGasBuffer', function () { - it('adds 100k gas buffer to estimates', function () { - const gas = '0x04ee59' // Actual estimated gas example - const tooBigOutput = '0x80674f9' // Actual bad output - const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16) - const correctBuffer = new BN('100000', 10) - const correct = bnGas.add(correctBuffer) - - // const tooBig = new BN(tooBigOutput, 16) - const result = keyringController.addGasBuffer(gas) - const bnResult = new BN(ethUtil.stripHexPrefix(result), 16) - - assert.equal(result.indexOf('0x'), 0, 'included hex prefix') - assert(bnResult.gt(bnGas), 'Estimate increased in value.') - assert.equal(bnResult.sub(bnGas).toString(10), '100000', 'added 100k gas') - assert.equal(result, '0x' + correct.toString(16), 'Added the right amount') - assert.notEqual(result, tooBigOutput, 'not that bad estimate') - }) - }) -}) diff --git a/test/unit/pending-balance-test.js b/test/unit/pending-balance-test.js new file mode 100644 index 000000000..5048d487b --- /dev/null +++ b/test/unit/pending-balance-test.js @@ -0,0 +1,93 @@ +const assert = require('assert') +const PendingBalanceCalculator = require('../../app/scripts/lib/pending-balance-calculator') +const MockTxGen = require('../lib/mock-tx-gen') +const BN = require('ethereumjs-util').BN +let providerResultStub = {} + +const zeroBn = new BN(0) +const etherBn = new BN(String(1e18)) +const ether = '0x' + etherBn.toString(16) + +describe('PendingBalanceCalculator', function () { + let balanceCalculator + + describe('#calculateMaxCost(tx)', function () { + it('returns a BN for a given tx value', function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: ether, + gasPrice: '0x0', + gas: '0x0', + } + }, { count: 1 }) + + const balanceCalculator = generateBalanceCalcWith([], zeroBn) + const result = balanceCalculator.calculateMaxCost(pendingTxs[0]) + assert.equal(result.toString(), etherBn.toString(), 'computes one ether') + }) + + it('calculates gas costs as well', function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: '0x0', + gasPrice: '0x2', + gas: '0x3', + } + }, { count: 1 }) + + const balanceCalculator = generateBalanceCalcWith([], zeroBn) + const result = balanceCalculator.calculateMaxCost(pendingTxs[0]) + assert.equal(result.toString(), '6', 'computes 6 wei of gas') + }) + }) + + describe('if you have no pending txs and one ether', function () { + + beforeEach(function () { + balanceCalculator = generateBalanceCalcWith([], etherBn) + }) + + it('returns the network balance', async function () { + const result = await balanceCalculator.getBalance() + assert.equal(result, ether, `gave ${result} needed ${ether}`) + }) + }) + + describe('if you have a one ether pending tx and one ether', function () { + beforeEach(function () { + const txGen = new MockTxGen() + pendingTxs = txGen.generate({ + status: 'submitted', + txParams: { + value: ether, + gasPrice: '0x0', + gas: '0x0', + } + }, { count: 1 }) + + balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn) + }) + + it('returns the subtracted result', async function () { + const result = await balanceCalculator.getBalance() + assert.equal(result, '0x0', `gave ${result} needed '0x0'`) + return true + }) + + }) +}) + +function generateBalanceCalcWith (transactions, providerStub = zeroBn) { + const getPendingTransactions = async () => transactions + const getBalance = async () => providerStub + + return new PendingBalanceCalculator({ + getBalance, + getPendingTransactions, + }) +} + diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 7bb193242..7b875db66 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -27,7 +27,7 @@ describe('Transaction Controller', function () { networkStore: new ObservableStore(currentNetworkId), txHistoryLimit: 10, blockTracker: { getCurrentBlock: noop, on: noop, once: noop }, - ethStore: { getState: noop }, + accountTracker: { getState: noop }, signTransaction: (ethTx) => new Promise((resolve) => { ethTx.sign(privKey) resolve() @@ -431,4 +431,4 @@ describe('Transaction Controller', function () { }).catch(done) }) }) -})
\ No newline at end of file +}) |