web3.js/test/polling.js

122 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-03-25 14:37:14 +01:00
var chai = require('chai');
var assert = chai.assert;
2015-10-08 07:42:08 +02:00
var Web3 = require('../index');
var web3 = new Web3();
2015-03-25 14:37:14 +01:00
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
2015-03-26 15:43:35 +01:00
var utils = require('../lib/utils/utils');
2015-03-25 14:37:14 +01:00
var tests = [{
protocol: 'eth',
2015-05-07 12:45:36 +02:00
args: ['latest'],
2015-03-25 14:37:14 +01:00
firstResult: 1,
firstPayload: {
method: "eth_newBlockFilter",
2015-05-07 12:45:36 +02:00
params: []
},
secondResult: ['0x1234'],
secondPayload: {
method: "eth_getFilterChanges"
}
},
{
protocol: 'eth',
args: ['pending'],
firstResult: 1,
firstPayload: {
method: "eth_newPendingTransactionFilter",
params: []
2015-03-25 14:37:14 +01:00
},
2015-05-07 12:45:36 +02:00
secondResult: ['0x1234'],
2015-03-25 14:37:14 +01:00
secondPayload: {
method: "eth_getFilterChanges"
}
}];
var testPolling = function (tests) {
2015-03-25 23:50:39 +01:00
describe('web3.eth.filter.polling', function () {
2015-03-25 14:37:14 +01:00
tests.forEach(function (test, index) {
it('should create && successfully poll filter', function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
2015-03-25 14:37:14 +01:00
provider.injectResult(test.firstResult);
var step = 0;
2015-03-25 14:37:14 +01:00
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);
2015-03-26 15:43:35 +01:00
} else if (step === 1 && utils.isArray(payload)) {
step++;
2015-03-25 14:37:14 +01:00
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
2015-10-08 07:42:08 +02:00
var filter = web3[test.protocol].filter.apply(web3[test.protocol], test.args);
2015-03-25 14:37:14 +01:00
provider.injectBatchResults([test.secondResult]);
filter.watch(function (err, result) {
2015-06-24 10:28:12 +02:00
if (test.err) {
// todo
} else {
assert.equal(result, test.secondResult[0]);
}
filter.stopWatching();
2015-06-24 10:28:12 +02:00
done();
});
});
it('should create && successfully poll filter when passed as callback', function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
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 && utils.isArray(payload)) {
step++;
2015-06-24 10:28:12 +02:00
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);
}
});
// add callback
test.args.push(function (err, result) {
2015-03-25 14:37:14 +01:00
if (test.err) {
// todo
} else {
assert.equal(result, test.secondResult[0]);
}
filter.stopWatching();
2015-03-25 14:37:14 +01:00
done();
});
2015-06-24 10:28:12 +02:00
// when
2015-10-08 07:42:08 +02:00
var filter = web3[test.protocol].filter.apply(web3[test.protocol], test.args);
2015-06-24 10:28:12 +02:00
provider.injectBatchResults([test.secondResult]);
2015-03-25 14:37:14 +01:00
});
});
});
};
testPolling(tests);