From 94fa2db986880275b5b7197875bf789780bf888d Mon Sep 17 00:00:00 2001 From: Jannis Redmann Date: Wed, 22 Feb 2017 17:44:11 +0100 Subject: [PATCH] attempt to fix subscribeToEvents test (#4638) --- js/src/util/subscribe-to-events.js | 3 ++- js/src/util/subscribe-to-events.spec.js | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/js/src/util/subscribe-to-events.js b/js/src/util/subscribe-to-events.js index 07c9943b9..20c04260e 100644 --- a/js/src/util/subscribe-to-events.js +++ b/js/src/util/subscribe-to-events.js @@ -76,7 +76,8 @@ const subscribeToEvents = (contract, events, opt = {}) => { filter = api.eth .newFilter({ - fromBlock: opt.from, toBlock: opt.to, + fromBlock: opt.from, + toBlock: opt.to, address: contract.address, topics: [signatures] }) diff --git a/js/src/util/subscribe-to-events.spec.js b/js/src/util/subscribe-to-events.spec.js index 2bafe22fd..db0d40f07 100644 --- a/js/src/util/subscribe-to-events.spec.js +++ b/js/src/util/subscribe-to-events.spec.js @@ -14,15 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -import { spy, stub } from 'sinon'; +import { spy, stub, useFakeTimers } from 'sinon'; import subscribeToEvents from './subscribe-to-events'; import { pastLogs, liveLogs, createApi, createContract } from './subscribe-to-events.test.js'; +// Note: We want to have a `setTimeout` that is independent from +// `sinon.useFakeTimers`. Therefore we dereference `setTimeout` here. +const _setTimeout = setTimeout; const delay = (t) => new Promise((resolve) => { - setTimeout(resolve, t); + _setTimeout(resolve, t); }); describe('util/subscribe-to-events', () => { @@ -97,7 +100,8 @@ describe('util/subscribe-to-events', () => { expect(api.eth.uninstallFilter.firstCall.args).to.eql([ 123 ]); }); - it.skip('checks for new events regularly', async function () { + it('checks for new events regularly', async function () { + const clock = useFakeTimers(); const { api, contract } = this; api.eth.getFilterLogs = stub().resolves([]); @@ -108,12 +112,16 @@ describe('util/subscribe-to-events', () => { subscribeToEvents(contract, [ 'Bar' ], { interval: 5 }) .on('log', onLog) .on('Bar', onBar); - await delay(10); + await delay(1); // let stubs resolve + clock.tick(5); + await delay(1); // let stubs resolve expect(onLog.callCount).to.be.at.least(1); expect(onLog.firstCall.args).to.eql([ liveLogs[0] ]); expect(onBar.callCount).to.be.at.least(1); expect(onBar.firstCall.args).to.eql([ liveLogs[0] ]); + + clock.restore(); }); it('accepts a custom block range', async function () {