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

Commit

Permalink
Seperate getNethash and requestToActivePeer to avoid mock problems in…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
Gina Contrino committed Oct 13, 2017
1 parent 7e1d9d3 commit c867b29
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/actions/peers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Lisk from 'lisk-js';
import actionTypes from '../constants/actions';
import { getNethash } from './../utils/api/peers';
import { getNethash } from './../utils/api/nethash';

const peerSet = (data, config) => ({
data: Object.assign({
Expand Down
4 changes: 2 additions & 2 deletions src/actions/peers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import { spy, stub, match } from 'sinon';
import actionTypes from '../constants/actions';
import { activePeerSet, activePeerUpdate } from './peers';
import * as peersApi from './../utils/api/peers';
import * as nethashApi from './../utils/api/nethash';


describe('actions: peers', () => {
Expand All @@ -29,7 +29,7 @@ describe('actions: peers', () => {

beforeEach(() => {
dispatch = spy();
getNetHash = stub(peersApi, 'getNethash');
getNetHash = stub(nethashApi, 'getNethash');
});

afterEach(() => {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/api/nethash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { requestToActivePeer } from './peers';

/* eslint-disable */
export const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash'));
/* eslint-enable */

29 changes: 29 additions & 0 deletions src/utils/api/nethash.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'chai';
import { mock } from 'sinon';
import * as peers from './peers';
import { getNethash } from './nethash';


describe('Utils: Nethash', () => {
let peersMock;
const activePeer = {};

beforeEach(() => {
peersMock = mock(peers);
});

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

it('should return the result from requestToActivePeer call', () => {
const mockedReturns = 'requestToActivePeer returns something';

peersMock.expects('requestToActivePeer')
.withArgs(activePeer, 'blocks/getNethash')
.returns(mockedReturns);

const returnedPromise = getNethash(activePeer);
expect(returnedPromise).to.equal(mockedReturns);
});
});
9 changes: 3 additions & 6 deletions src/utils/api/peers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadingStarted, loadingFinished } from '../../utils/loading';

const requestToActivePeer = (activePeer, path, urlParams) =>
/* eslint-disable */
export const requestToActivePeer = (activePeer, path, urlParams) =>
new Promise((resolve, reject) => {
loadingStarted(path);
activePeer.sendRequest(path, urlParams, (data) => {
Expand All @@ -12,8 +13,4 @@ const requestToActivePeer = (activePeer, path, urlParams) =>
loadingFinished(path);
});
});


const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash'));

export { requestToActivePeer, getNethash };
/* eslint-enable */

0 comments on commit c867b29

Please sign in to comment.