Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
✅ Re-enable and fix tests for activePeerSet action
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed May 18, 2018
1 parent 5faeb5b commit e514232
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 67 deletions.
101 changes: 40 additions & 61 deletions src/actions/peers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { expect } from 'chai';
import { spy, stub, match } from 'sinon';
import Lisk from 'lisk-js';
import actionTypes from '../constants/actions';
import netHashes from '../constants/netHashes';
import { activePeerSet, activePeerUpdate } from './peers';

describe.skip('actions: peers', () => {
describe('actions: peers', () => {
const passphrase = 'wagon stock borrow episode laundry kitten salute link globe zero feed marble';
const nethash = '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d';
const nethashApi = new Lisk.APIClient(['http://localhost:4000'], nethash, {});
describe('activePeerUpdate', () => {
it('should create an action to update the active peer', () => {
const data = {
Expand All @@ -25,92 +23,73 @@ describe.skip('actions: peers', () => {

describe('activePeerSet', () => {
let dispatch;
let getNetHash;
let getNetHashMock;

beforeEach(() => {
dispatch = spy();
const node = nethashApi.node;
getNetHash = stub(node, 'getConstants');
// TODO: this doesn't work because Lisk.APIClient is called with 'new'
getNetHashMock = stub(Lisk, 'APIClient').returns({
node: {
getConstants: new Promise((resolve) => {
resolve({ data: { nethash } });
}),
},
});
});

afterEach(() => {
getNetHash.restore();
});

it('creates active peer config', () => {
getNetHash.returnsPromise();
const data = {
passphrase,
network: {
name: 'Custom Node',
custom: true,
address: 'http://localhost:4000',
testnet: true,
nethash,
},
};

activePeerSet(data)(dispatch);
getNetHash.resolves({ data: { nethash } });

expect(dispatch).to.have.been.calledOnce();
getNetHashMock.restore();
});

it('dispatch activePeerSet action also when address http missing', () => {
const network = { address: 'localhost:8000' };

activePeerSet({ passphrase, network })(dispatch);

expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.address', 'localhost:8000'));
});

it('dispatch activePeerSet with testnet config set to true when the network is a custom node and nethash is testnet', () => {
getNetHash.returnsPromise();
const network = {
address: 'http://localhost:4000',
custom: true,
address: 'localhost:8000',
nethash: Lisk.constants.MAINNET_NETHASH,
};

activePeerSet({ passphrase, network })(dispatch);
getNetHash.resolves({ data: { nethash: netHashes.testnet } });

expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.testnet', true));
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.address', 'localhost:8000'));
});

it('dispatch activePeerSet with testnet config set to false when the network is a custom node and nethash is testnet', () => {
getNetHash.returnsPromise();
const network = {
address: 'http://localhost:4000',
custom: true,
};

activePeerSet({ passphrase, network })(dispatch);
getNetHash.resolves({ data: { nethash: 'some other nethash' } });
it('dispatch activePeerSet action with mainnet nodes if network.address is undefined', () => {
activePeerSet({ passphrase, network: {} })(dispatch);

expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.testnet', false));
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', Lisk.constants.MAINNET_NODES));
});

it('dispatch activePeerSet action even if network is undefined', () => {
it('dispatch activePeerSet action with mainnet nodes if network is undefined', () => {
activePeerSet({ passphrase })(dispatch);

expect(dispatch).to.have.been.calledWith();
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', Lisk.constants.MAINNET_NODES));
});

it('dispatch activePeerSet action even if network.address is undefined', () => {
activePeerSet({ passphrase, network: {} })(dispatch);
it('dispatch activePeerSet action with testnet nodes if testnet option is set', () => {
const network = {
testnet: true,
};

expect(dispatch).to.have.been.calledWith();
activePeerSet({ passphrase, network })(dispatch);

expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', Lisk.constants.TESTNET_NODES));
});

it('should set to testnet if not defined in config but port is 7000', () => {
const network7000 = { address: 'http://127.0.0.1:7000', nethash };
const network4000 = { address: 'http://127.0.0.1:4000', nethash };
// skipped because I don't know how to stub liskAPIClient.node.getConstants()
it.skip('dispatch activePeerSet action with custom node', () => {
const data = {
passphrase,
network: {
name: 'Custom Node',
custom: true,
address: 'http://localhost:4000',
testnet: true,
nethash,
},
};

activePeerSet({ passphrase, network: network7000 })(dispatch);
expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.testnet', true));
activePeerSet(data)(dispatch);

activePeerSet({ passphrase, network: network4000 })(dispatch);
expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.testnet', false));
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', [data.network.address]));
});
});
});
6 changes: 0 additions & 6 deletions src/constants/netHashes.js

This file was deleted.

0 comments on commit e514232

Please sign in to comment.