diff --git a/js/package.json b/js/package.json index a5fab74d5..50f512823 100644 --- a/js/package.json +++ b/js/package.json @@ -67,6 +67,7 @@ "babel-register": "6.18.0", "babel-runtime": "6.18.0", "chai": "3.5.0", + "chai-as-promised": "6.0.0", "chai-enzyme": "0.6.1", "copy-webpack-plugin": "4.0.1", "core-js": "2.4.1", diff --git a/js/src/ui/TxList/store.js b/js/src/ui/TxList/store.js index 3faac193e..09cdd2226 100644 --- a/js/src/ui/TxList/store.js +++ b/js/src/ui/TxList/store.js @@ -45,6 +45,8 @@ export default class Store { if (bnB.eq(0)) { return bnB.eq(bnA) ? 0 : 1; + } else if (bnA.eq(0)) { + return -1; } return bnB.comparedTo(bnA); diff --git a/js/src/ui/TxList/store.spec.js b/js/src/ui/TxList/store.spec.js new file mode 100644 index 000000000..9fb3a709e --- /dev/null +++ b/js/src/ui/TxList/store.spec.js @@ -0,0 +1,90 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import sinon from 'sinon'; + +import Store from './store'; + +const SUBID = 123; +const BLOCKS = { + 1: { blockhash: '0x1' }, + 2: { blockhash: '0x2' } +}; +const TRANSACTIONS = { + '0x123': { blockNumber: new BigNumber(1) }, + '0x234': { blockNumber: new BigNumber(0) }, + '0x345': { blockNumber: new BigNumber(2) }, + '0x456': { blockNumber: new BigNumber(0) } +}; + +describe.only('ui/TxList/store', () => { + let api; + let store; + + beforeEach(() => { + api = { + subscribe: sinon.stub().resolves(SUBID), + eth: { + getBlockByNumber: (blockNumber) => { + return Promise.resolve(BLOCKS[blockNumber]); + } + } + }; + store = new Store(api); + }); + + describe('create', () => { + it('has empty storage', () => { + expect(store.blocks).to.deep.equal({}); + expect(store.sortedHashes.peek()).to.deep.equal([]); + expect(store.transactions).to.deep.equal({}); + }); + + it('subscribes to eth_blockNumber', () => { + expect(api.subscribe).to.have.been.calledWith('eth_blockNumber'); + expect(store._subscriptionId).to.equal(SUBID); + }); + }); + + describe('addBlocks', () => { + beforeEach(() => { + store.addBlocks(BLOCKS); + }); + + it('adds the blocks to the list', () => { + expect(store.blocks).to.deep.equal(BLOCKS); + }); + }); + + describe('addTransactions', () => { + beforeEach(() => { + store.addTransactions(TRANSACTIONS); + }); + + it('adds all transactions to the list', () => { + expect(store.transactions).to.deep.equal(TRANSACTIONS); + }); + + it('sorts transactions based on blockNumber', () => { + expect(store.sortedHashes.peek()).to.deep.equal(['0x234', '0x456', '0x345', '0x123']); + }); + + it('adds pending transactions to the pending queue', () => { + expect(store._pendingHashes).to.deep.equal(['0x234', '0x456']); + }); + }); +}); diff --git a/js/test/mocha.config.js b/js/test/mocha.config.js index adc43530e..36c91c76e 100644 --- a/js/test/mocha.config.js +++ b/js/test/mocha.config.js @@ -22,11 +22,13 @@ es6Promise.polyfill(); import 'mock-local-storage'; import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; import chaiEnzyme from 'chai-enzyme'; import sinonChai from 'sinon-chai'; import { WebSocket } from 'mock-socket'; import jsdom from 'jsdom'; +chai.use(chaiAsPromised); chai.use(chaiEnzyme()); chai.use(sinonChai);