web3.js/test/eth.getBalance.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-03-23 13:08:59 +00:00
var chai = require('chai');
var assert = chai.assert;
var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
2015-03-23 13:08:59 +00:00
var method = 'getBalance';
var tests = [{
2015-03-23 16:02:34 +00:00
args: [301, 2],
formattedArgs: ['0x12d', '0x2'],
result: '0x31981',
formattedResult: '0x31981',
call: 'eth_getBalance'
},{
2015-03-23 13:08:59 +00:00
args: ['0x12d', '0x1'],
formattedArgs: ['0x12d', '0x1'],
result: '0x31981',
formattedResult: '0x31981',
call: 'eth_getBalance'
2015-03-23 13:23:45 +00:00
}, {
args: [0x12d, 0x1],
formattedArgs: ['0x12d', '0x1'],
result: '0x31981',
formattedResult: '0x31981',
call: 'eth_getBalance'
}, {
args: [0x12d],
2015-03-23 15:42:09 +00:00
formattedArgs: ['0x12d', 'latest'], // here we might want to get current defaultBlock
2015-03-23 13:23:45 +00:00
result: '0x31981',
formattedResult: '0x31981',
call: 'eth_getBalance'
2015-03-23 13:08:59 +00:00
}];
describe('eth', function () {
describe(method, function () {
tests.forEach(function (test, index) {
2015-03-23 13:23:45 +00:00
it('sync test: ' + index, function () {
2015-03-23 13:08:59 +00:00
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
2015-03-23 16:02:34 +00:00
provider.injectValidation(function (payload) {
2015-03-23 13:08:59 +00:00
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
2015-03-23 16:02:34 +00:00
});
2015-03-23 13:08:59 +00:00
// when
var result = web3.eth[method].apply(null, test.args.slice(0));
// then
2015-03-23 15:42:09 +00:00
assert.equal(+test.formattedResult, result.toNumber());
2015-03-23 13:08:59 +00:00
});
it('async test: ' + index, function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
2015-03-23 16:02:34 +00:00
provider.injectValidation(function (payload) {
2015-03-23 13:08:59 +00:00
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
2015-03-23 16:02:34 +00:00
});
2015-03-23 13:08:59 +00:00
var callback = function (err, result) {
2015-03-23 15:42:09 +00:00
assert.equal(+test.formattedResult, result.toNumber());
2015-03-23 13:08:59 +00:00
done();
};
var args = test.args.slice(0);
args.push(callback);
// when
web3.eth[method].apply(null, args);
});
});
});
});