2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2017-01-05 12:07:10 +01:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
2017-05-09 12:01:44 +02:00
|
|
|
import Contracts from '@parity/shared/contracts';
|
|
|
|
|
2017-01-05 12:07:10 +01:00
|
|
|
import { initialState as defaultNodeStatusState } from './statusReducer';
|
|
|
|
import ChainMiddleware from './chainMiddleware';
|
2017-07-21 15:46:53 +02:00
|
|
|
import { createWsApi } from '../../../../test/e2e/ethapi';
|
2017-01-05 12:07:10 +01:00
|
|
|
|
|
|
|
let middleware;
|
|
|
|
let next;
|
|
|
|
let store;
|
2017-04-20 17:31:15 +02:00
|
|
|
let clock;
|
2017-01-05 12:07:10 +01:00
|
|
|
|
2017-01-12 14:25:32 +01:00
|
|
|
const api = createWsApi();
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-04-21 13:14:07 +02:00
|
|
|
Contracts.get(api);
|
2017-01-12 14:25:32 +01:00
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
function stubGlobals () {
|
|
|
|
clock = sinon.useFakeTimers();
|
|
|
|
sinon.spy(window.location, 'reload');
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreGlobals () {
|
|
|
|
window.location.reload.restore();
|
|
|
|
clock.restore();
|
|
|
|
}
|
|
|
|
|
2017-01-05 12:07:10 +01:00
|
|
|
function createMiddleware (collection = {}) {
|
|
|
|
middleware = new ChainMiddleware().toMiddleware();
|
|
|
|
next = sinon.stub();
|
|
|
|
store = {
|
|
|
|
dispatch: sinon.stub(),
|
|
|
|
getState: () => {
|
|
|
|
return {
|
2017-01-12 14:25:32 +01:00
|
|
|
api: api,
|
2017-01-05 12:07:10 +01:00
|
|
|
nodeStatus: Object.assign({}, defaultNodeStatusState, collection)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return middleware;
|
|
|
|
}
|
|
|
|
|
|
|
|
function callMiddleware (action) {
|
2017-04-20 17:31:15 +02:00
|
|
|
const result = middleware(store)(next)(action);
|
|
|
|
|
|
|
|
clock.tick(100);
|
|
|
|
|
|
|
|
return result;
|
2017-01-05 12:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('reduxs/providers/ChainMiddleware', () => {
|
2017-04-20 17:31:15 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
stubGlobals();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
restoreGlobals();
|
|
|
|
});
|
|
|
|
|
2017-01-05 12:07:10 +01:00
|
|
|
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', () => {
|
2017-04-20 17:31:15 +02:00
|
|
|
it('does not reload when moving from the initial/unknown chain', () => {
|
2017-01-05 12:07:10 +01:00
|
|
|
createMiddleware();
|
|
|
|
callMiddleware({ type: 'statusCollection', collection: { netChain: 'homestead' } });
|
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
expect(window.location.reload).not.to.have.been.called;
|
2017-01-05 12:07:10 +01:00
|
|
|
});
|
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
it('does not reload when moving to the same chain', () => {
|
2017-01-05 12:07:10 +01:00
|
|
|
createMiddleware({ netChain: 'homestead' });
|
|
|
|
callMiddleware({ type: 'statusCollection', collection: { netChain: 'homestead' } });
|
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
expect(window.location.reload).not.to.have.been.called;
|
2017-01-05 12:07:10 +01:00
|
|
|
});
|
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
it('does reload when moving between chains', () => {
|
2017-01-05 12:07:10 +01:00
|
|
|
createMiddleware({ netChain: 'homestead' });
|
|
|
|
callMiddleware({ type: 'statusCollection', collection: { netChain: 'ropsten' } });
|
|
|
|
|
2017-04-20 17:31:15 +02:00
|
|
|
expect(window.location.reload).to.have.been.called;
|
2017-01-05 12:07:10 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|