From 7b01b824631142e59be7096aef27eb9ef2a38eab Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Fri, 13 Oct 2017 10:26:51 +0200 Subject: [PATCH] Seperate getNethash and requestToActivePeer to avoid mock problems in tests --- src/actions/peers.js | 2 +- src/actions/peers.test.js | 4 ++-- src/utils/api/nethash.js | 6 ++++++ src/utils/api/nethash.test.js | 29 +++++++++++++++++++++++++++++ src/utils/api/peers.js | 9 +++------ 5 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/utils/api/nethash.js create mode 100644 src/utils/api/nethash.test.js diff --git a/src/actions/peers.js b/src/actions/peers.js index c6dd2dd62..60131e2b5 100644 --- a/src/actions/peers.js +++ b/src/actions/peers.js @@ -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({ diff --git a/src/actions/peers.test.js b/src/actions/peers.test.js index 6ade42353..d29afa900 100644 --- a/src/actions/peers.test.js +++ b/src/actions/peers.test.js @@ -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', () => { @@ -29,7 +29,7 @@ describe('actions: peers', () => { beforeEach(() => { dispatch = spy(); - getNetHash = stub(peersApi, 'getNethash'); + getNetHash = stub(nethashApi, 'getNethash'); }); afterEach(() => { diff --git a/src/utils/api/nethash.js b/src/utils/api/nethash.js new file mode 100644 index 000000000..aeffb5d4c --- /dev/null +++ b/src/utils/api/nethash.js @@ -0,0 +1,6 @@ +import { requestToActivePeer } from './peers'; + +/* eslint-disable */ +export const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash')); +/* eslint-enable */ + diff --git a/src/utils/api/nethash.test.js b/src/utils/api/nethash.test.js new file mode 100644 index 000000000..eebbf9e06 --- /dev/null +++ b/src/utils/api/nethash.test.js @@ -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); + }); +}); diff --git a/src/utils/api/peers.js b/src/utils/api/peers.js index c4da103ce..c0ef67343 100644 --- a/src/utils/api/peers.js +++ b/src/utils/api/peers.js @@ -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) => { @@ -12,8 +13,4 @@ const requestToActivePeer = (activePeer, path, urlParams) => loadingFinished(path); }); }); - - -const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash')); - -export { requestToActivePeer, getNethash }; +/* eslint-enable */