web3.js/test/polling.js

68 lines
2.1 KiB
JavaScript

var chai = require('chai');
var assert = chai.assert;
var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var tests = [{
protocol: 'eth',
args: ['pending'],
firstResult: 1,
firstPayload: {
method: "eth_newBlockFilter",
params: [
"pending"
]
},
secondResult: [2],
secondPayload: {
method: "eth_getFilterChanges"
}
}];
var testPolling = function (tests) {
describe('polling', function () {
tests.forEach(function (test, index) {
it('should create && successfully poll filter', function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.firstResult);
var step = 0
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.firstPayload.method);
assert.deepEqual(payload.params, test.firstPayload.params);
} else if (step === 1) {
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === test.secondPayload.method && p.params[0] === test.firstResult;
});
assert.equal(r.length > 0, true);
}
});
// when
var filter = web3[test.protocol].filter.apply(null, test.args);
provider.injectBatchResults([test.secondResult]);
filter.watch(function (err, result) {
if (test.err) {
// todo
} else {
assert.equal(result, test.secondResult[0]);
}
done();
});
});
});
});
};
testPolling(tests);