web3.js/test/helpers/test.method.js

80 lines
2.8 KiB
JavaScript

var chai = require('chai');
var assert = chai.assert;
var web3 = require('../../index');
var FakeHttpProvider = require('./FakeHttpProvider');
var clone = function (object) { return JSON.parse(JSON.stringify(object)); };
var runTests = function (obj, method, tests) {
var testName = obj ? 'web3.' + obj : 'web';
describe(testName, function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('sync test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
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);
});
var args = clone(test.args)
// when
if (obj) {
var result = web3[obj][method].apply(null, args);
} else {
var result = web3[method].apply(null, args);
}
// when
//var result = (obj)
//? web3[obj][method].apply(null, test.args.slice(0))
//: web3[method].apply(null, test.args.slice(0));
// then
assert.deepEqual(test.formattedResult, result);
});
it('async test: ' + index, function (done) {
// given
var provider = new FakeHttpProvider();
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);
});
var args = clone(test.args);
// add callback
args.push(function (err, result) {
assert.deepEqual(test.formattedResult, result);
done();
});
// when
if (obj) {
web3[obj][method].apply(null, args);
} else {
web3[method].apply(null, args);
}
});
});
});
});
};
module.exports = {
runTests: runTests
}