web3.js/test/web3.eth.filter.js

103 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-03-24 10:16:21 +00:00
var chai = require('chai');
2015-10-08 06:41:55 +00:00
var Web3 = require('../index');
var web3 = new Web3();
2015-03-24 10:16:21 +00:00
var assert = chai.assert;
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var method = 'filter';
var tests = [{
args: [{
fromBlock: 0,
toBlock: 10,
address: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'
}],
formattedArgs: [{
fromBlock: '0x0',
toBlock: '0xa',
address: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855',
2015-03-26 14:43:35 +00:00
topics: []
2015-04-28 15:01:37 +00:00
}],
result: '0xf',
formattedResult: '0xf',
call: 'eth_newFilter'
},{
args: [{
fromBlock: 'latest',
toBlock: 'latest',
address: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'
}],
formattedArgs: [{
fromBlock: 'latest',
toBlock: 'latest',
address: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855',
topics: []
2015-03-24 10:16:21 +00:00
}],
result: '0xf',
formattedResult: '0xf',
call: 'eth_newFilter'
},{
2015-05-07 10:45:36 +00:00
args: ['latest'],
formattedArgs: [],
2015-03-24 10:16:21 +00:00
result: '0xf',
formattedResult: '0xf',
call: 'eth_newBlockFilter'
2015-05-07 10:45:36 +00:00
},{
args: ['pending'],
formattedArgs: [],
result: '0xf',
formattedResult: '0xf',
call: 'eth_newPendingTransactionFilter'
2015-03-24 10:16:21 +00:00
}];
2015-03-25 22:50:39 +00:00
describe('web3.eth', function () {
2015-03-24 10:16:21 +00:00
describe(method, function () {
tests.forEach(function (test, index) {
it('property test: ' + index, function () {
2015-09-14 22:13:52 +00:00
2015-03-24 10:16:21 +00:00
// given
2015-09-14 22:13:52 +00:00
var provider = new FakeHttpProvider();
web3.reset();
2015-09-14 22:13:52 +00:00
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
// call
2015-10-08 06:41:55 +00:00
var filter = web3.eth[method].apply(web3.eth, test.args);
2015-09-14 22:13:52 +00:00
// test filter.get
if(typeof test.args === 'object') {
var logs = [{data: '0xb'}, {data: '0x11'}];
2015-03-24 10:16:21 +00:00
2015-09-14 22:13:52 +00:00
provider.injectResult(logs);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
assert.deepEqual(payload.params, [test.formattedResult]);
});
// sync should throw an error
try {
assert.throws(filter.get());
} catch(e){
assert.instanceOf(e, Error);
}
// async should get the fake logs
filter.get(function(e, res){
assert.equal(logs, res);
web3.reset();
2015-09-14 22:13:52 +00:00
done();
});
}
2015-03-24 10:16:21 +00:00
});
});
});
});
2015-10-07 03:10:02 +00:00