From 1ef67f68ed28dde17eb651676fb6d5080467e11b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 5 Jan 2017 12:07:10 +0100 Subject: [PATCH] Starting on homestead shows reload snackbar (#4043) * Fix issue where starting on homestead showed reload * Align snackbar timing with errors (60s) --- js/src/redux/providers/chainMiddleware.js | 7 +- .../redux/providers/chainMiddleware.spec.js | 86 +++++++++++++++++++ js/src/redux/providers/statusReducer.js | 8 +- 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 js/src/redux/providers/chainMiddleware.spec.js diff --git a/js/src/redux/providers/chainMiddleware.js b/js/src/redux/providers/chainMiddleware.js index 82281f3b88a..77c757da689 100644 --- a/js/src/redux/providers/chainMiddleware.js +++ b/js/src/redux/providers/chainMiddleware.js @@ -15,6 +15,7 @@ // along with Parity. If not, see . import { showSnackbar } from './snackbarActions'; +import { DEFAULT_NETCHAIN } from './statusReducer'; export default class ChainMiddleware { toMiddleware () { @@ -23,11 +24,11 @@ export default class ChainMiddleware { const { collection } = action; if (collection && collection.netChain) { - const chain = collection.netChain; + const newChain = collection.netChain; const { nodeStatus } = store.getState(); - if (chain !== nodeStatus.netChain) { - store.dispatch(showSnackbar(`Switched to ${chain}. Please reload the page.`, 5000)); + if (newChain !== nodeStatus.netChain && nodeStatus.netChain !== DEFAULT_NETCHAIN) { + store.dispatch(showSnackbar(`Switched to ${newChain}. Please reload the page.`, 60000)); } } } diff --git a/js/src/redux/providers/chainMiddleware.spec.js b/js/src/redux/providers/chainMiddleware.spec.js new file mode 100644 index 00000000000..ed2d5eca61d --- /dev/null +++ b/js/src/redux/providers/chainMiddleware.spec.js @@ -0,0 +1,86 @@ +// Copyright 2015, 2016 Parity Technologies (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 sinon from 'sinon'; + +import { initialState as defaultNodeStatusState } from './statusReducer'; +import ChainMiddleware from './chainMiddleware'; + +let middleware; +let next; +let store; + +function createMiddleware (collection = {}) { + middleware = new ChainMiddleware().toMiddleware(); + next = sinon.stub(); + store = { + dispatch: sinon.stub(), + getState: () => { + return { + nodeStatus: Object.assign({}, defaultNodeStatusState, collection) + }; + } + }; + + return middleware; +} + +function callMiddleware (action) { + return middleware(store)(next)(action); +} + +describe('reduxs/providers/ChainMiddleware', () => { + describe('next action', () => { + beforeEach(() => { + createMiddleware(); + }); + + it('calls next with matching actiontypes', () => { + callMiddleware({ type: 'statusCollection' }); + + expect(next).to.have.been.calledWithMatch({ type: 'statusCollection' }); + }); + + it('calls next with non-matching actiontypes', () => { + callMiddleware({ type: 'nonMatchingType' }); + + expect(next).to.have.been.calledWithMatch({ type: 'nonMatchingType' }); + }); + }); + + describe('chain switching', () => { + it('does not dispatch when moving from the initial/unknown chain', () => { + createMiddleware(); + callMiddleware({ type: 'statusCollection', collection: { netChain: 'homestead' } }); + + expect(store.dispatch).not.to.have.been.called; + }); + + it('does not dispatch when moving to the same chain', () => { + createMiddleware({ netChain: 'homestead' }); + callMiddleware({ type: 'statusCollection', collection: { netChain: 'homestead' } }); + + expect(store.dispatch).not.to.have.been.called; + }); + + it('does dispatch when moving between chains', () => { + createMiddleware({ netChain: 'homestead' }); + callMiddleware({ type: 'statusCollection', collection: { netChain: 'ropsten' } }); + + expect(store.dispatch).to.have.been.called; + }); + }); +}); diff --git a/js/src/redux/providers/statusReducer.js b/js/src/redux/providers/statusReducer.js index 17186b01256..4bef27b1bab 100644 --- a/js/src/redux/providers/statusReducer.js +++ b/js/src/redux/providers/statusReducer.js @@ -17,6 +17,7 @@ import BigNumber from 'bignumber.js'; import { handleActions } from 'redux-actions'; +const DEFAULT_NETCHAIN = '(unknown)'; const initialState = { blockNumber: new BigNumber(0), blockTimestamp: new Date(), @@ -32,7 +33,7 @@ const initialState = { gasLimit: new BigNumber(0), hashrate: new BigNumber(0), minGasPrice: new BigNumber(0), - netChain: 'ropsten', + netChain: DEFAULT_NETCHAIN, netPeers: { active: new BigNumber(0), connected: new BigNumber(0), @@ -82,3 +83,8 @@ export default handleActions({ return Object.assign({}, state, { refreshStatus }); } }, initialState); + +export { + DEFAULT_NETCHAIN, + initialState +};