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

Re-enable unit tests after Core 1.0.0 migration - Closes #1072 #1073

Merged
merged 7 commits into from
May 25, 2018
Merged
21 changes: 10 additions & 11 deletions src/actions/peers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import i18next from 'i18next';
import Lisk from 'lisk-elements';
import actionTypes from '../constants/actions';
// import { getNethash } from './../utils/api/nethash';
import { errorToastDisplayed } from './toaster';
import netHashes from '../constants/netHashes';
import { loadingStarted, loadingFinished } from '../utils/loading';

const peerSet = (data, config) => ({
Expand Down Expand Up @@ -39,19 +37,20 @@ export const activePeerSet = data =>
config.ssl = protocol === 'https:';
config.port = port || (config.ssl ? 443 : 80);
config.nodes = [`${protocol}//${hostname}:${port}`];
} else if (config.testnet) {
config.nodes = Lisk.APIClient.constants.TESTNET_NODES;
config.nethash = Lisk.APIClient.constants.TESTNET_NETHASH;
} else {
config.nodes = Lisk.APIClient.constants.MAINNET_NODES;
config.nethash = Lisk.APIClient.constants.MAINNET_NETHASH;
}
if (config.testnet === undefined && config.port !== undefined) {
config.testnet = config.port === '7000';
}

if (config.custom) {
const getNethash = new Lisk.APIClient(config.nodes, { nethash: config.nethash });
const liskAPIClient = new Lisk.APIClient(config.nodes, { nethash: config.nethash });
loadingStarted('getConstants');
getNethash.node.getConstants().then((response) => {
liskAPIClient.node.getConstants().then((response) => {
loadingFinished('getConstants');
config.testnet = response.data.nethash === netHashes.testnet;
if (!config.testnet && response.data.nethash !== netHashes.mainnet) {
config.nethash = response.data.nethash;
}
config.nethash = response.data.nethash;
dispatch(peerSet(data, config));
}).catch(() => {
loadingFinished('getConstants');
Expand Down
117 changes: 60 additions & 57 deletions src/actions/peers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { expect } from 'chai';
import { spy, stub, match } from 'sinon';
import Lisk from 'lisk-elements';
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 +24,96 @@ describe.skip('actions: peers', () => {

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

beforeEach(() => {
dispatch = spy();
const node = nethashApi.node;
getNetHash = stub(node, 'getConstants');
getConstantsMock = stub().returnsPromise();
APIClientBackup = Lisk.APIClient;

// TODO: find a better way of mocking Lisk.APIClient
Lisk.APIClient = class MockAPIClient {
constructor() {
this.node = {
getConstants: getConstantsMock,
};
}
};
Lisk.APIClient.constants = APIClientBackup.constants;
});

afterEach(() => {
getNetHash.restore();
Lisk.APIClient = APIClientBackup;
});

it('creates active peer config', () => {
getNetHash.returnsPromise();
const data = {
passphrase,
network: {
name: 'Custom Node',
custom: true,
address: 'http://localhost:4000',
testnet: true,
nethash,
},
it('dispatch activePeerSet action also when address http missing', () => {
const network = {
address: 'localhost:8000',
nethash: Lisk.APIClient.constants.MAINNET_NETHASH,
};

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

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

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

activePeerSet({ passphrase, network })(dispatch);
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.options.address', 'localhost:8000'));
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', Lisk.APIClient.constants.MAINNET_NODES));
});

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,
};

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

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

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

activePeerSet({ passphrase, network })(dispatch);
getNetHash.resolves({ data: { nethash: 'some other nethash' } });

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

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

expect(dispatch).to.have.been.calledWith();
});
it('dispatch activePeerSet action with custom node', () => {
const data = {
passphrase,
network: {
name: 'Custom Node',
custom: true,
address: 'http://localhost:4000',
testnet: true,
nethash,
},
};
getConstantsMock.resolves({ data: { nethash } });

it('dispatch activePeerSet action even if network.address is undefined', () => {
activePeerSet({ passphrase, network: {} })(dispatch);
activePeerSet(data)(dispatch);

expect(dispatch).to.have.been.calledWith();
expect(dispatch).to.have.been.calledWith(match.hasNested('data.options.nodes', [data.network.address]));
});

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 };
it('dispatch error toast action if getConstants() API call fails', () => {
const data = {
passphrase,
network: {
name: 'Custom Node',
custom: true,
address: 'http://localhost:4000',
testnet: true,
nethash,
},
};
getConstantsMock.rejects({});

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.has('type', actionTypes.toastDisplayed));
});
});
});
6 changes: 0 additions & 6 deletions src/constants/netHashes.js

This file was deleted.

14 changes: 4 additions & 10 deletions src/store/middlewares/login.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Lisk from 'lisk-elements';
import { expect } from 'chai';
import { spy, stub, mock } from 'sinon';
import { spy, stub, match } from 'sinon';
import middleware from './login';
import actionTypes from '../../constants/actions';
import * as accountApi from '../../utils/api/account';
Expand All @@ -10,7 +10,6 @@ describe('Login middleware', () => {
let store;
let next;
const passphrase = 'wagon stock borrow episode laundry kitten salute link globe zero feed marble';
const nethash = '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d';
const activePeer = new Lisk.APIClient(['http://localhost:4000'], {});

const activePeerSetAction = {
Expand Down Expand Up @@ -42,16 +41,11 @@ describe('Login middleware', () => {
expect(next).to.have.been.calledWith(sampleAction);
});

it.skip(`should action data to only have activePeer on ${actionTypes.activePeerSet} action`, () => {
it(`should action data to only have activePeer on ${actionTypes.activePeerSet} action`, () => {
middleware(store)(next)(activePeerSetAction);
const peerMock = mock(activePeer.node);
peerMock.expects('getConstants').withArgs()
.returnsPromise().resolves({ nethash });
peerMock.restore();
peerMock.verify();
expect(next).to.have.been.calledWith({
type: actionTypes.activePeerSet,
data: activePeer,
data: match.hasNested('activePeer', activePeer),
});
});

Expand All @@ -66,7 +60,7 @@ describe('Login middleware', () => {
delegateApiMock.restore();
});

it.skip(`should fetch account and delegate info on ${actionTypes.activePeerSet} action (delegate)`, () => {
it(`should fetch account and delegate info on ${actionTypes.activePeerSet} action (delegate)`, () => {
const accountApiMock = stub(accountApi, 'getAccount').returnsPromise().resolves({ success: true, balance: 0 });
const delegateApiMock = stub(delegateApi, 'getDelegate').returnsPromise().resolves({
success: true,
Expand Down
27 changes: 15 additions & 12 deletions src/utils/api/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,49 @@ import { getAccount, setSecondPassphrase, send,
describe('Utils: Account', () => {
const address = '1449310910991872227L';

describe.skip('getAccount', () => {
describe('getAccount', () => {
let activePeer;
let activePeerMock;
const activePeer = {
getAccount: () => { },
};

beforeEach(() => {
activePeerMock = mock(activePeer);
activePeer = {
accounts: {
get: () => { },
},
};
activePeerMock = mock(activePeer.accounts).expects('get').returnsPromise();
});

afterEach(() => {
activePeerMock.verify();
activePeerMock.restore();
});

it('should return a promise that is resolved when activePeer.getAccount() calls its callback with data.success == true', () => {
it('should return a promise that is resolved when activePeer.accounts.get() resolves one account', () => {
const account = { address, balance: 0, publicKey: null };
const response = { success: true, account };
const response = { data: [account] };

activePeerMock.expects('getAccount').withArgs(address).callsArgWith(1, response);
const requestPromise = getAccount(activePeer, address);
activePeerMock.resolves(response);
return expect(requestPromise).to.eventually.eql({
...account,
serverPublicKey: null,
});
});

it('should return a promise that is resolved even when activePeer.getAccount() calls its callback with data.success == false and "Account not found"', () => {
const response = { success: false, error: 'Account not found' };
it('should return a promise that is resolved even when activePeer.accounts.get() resolves no accounts', () => {
const response = { data: [] };
const account = { address, balance: 0 };

activePeerMock.expects('getAccount').withArgs(address).callsArgWith(1, response);
activePeerMock.resolves(response);
const requestPromise = getAccount(activePeer, address);
return expect(requestPromise).to.eventually.eql(account);
});

it('should otherwise return a promise that is rejected', () => {
const response = { success: false };

activePeerMock.expects('getAccount').withArgs(address).callsArgWith(1, response);
activePeerMock.rejects(response);
const requestPromise = getAccount(activePeer, address);
return expect(requestPromise).to.eventually.be.rejectedWith(response);
});
Expand Down