diff --git a/lib/messages/utils.js b/lib/messages/utils.js index d1bd29e..eb3a975 100644 --- a/lib/messages/utils.js +++ b/lib/messages/utils.js @@ -7,12 +7,12 @@ var _ = bitcore.deps._; var utils; module.exports = utils = { - checkInventory: function(inventory) { + checkInventory: function(arg) { $.checkArgument( - _.isUndefined(inventory) || - inventory.length === 0 || - (!_.isUndefined(inventory[0].type) && !_.isUndefined(inventory[0].hash)), - 'Inventory must be an array of inventory objects' + _.isUndefined(arg) || + (Array.isArray(arg) && arg.length === 0) || + (Array.isArray(arg) && !_.isUndefined(arg[0].type) && !_.isUndefined(arg[0].hash)), + 'Argument is expected to be an array of inventory objects' ); }, checkFinished: function checkFinished(parser) { diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index ed28719..f4df5e6 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -70,6 +70,22 @@ describe('Command Messages', function() { }); + describe('Inventory', function() { + it('should error if arg is not an array', function() { + (function() { + var message = messages.Inventory({}); + }).should.throw('Argument is expected to be an array of inventory objects'); + }); + it('should not error if arg is an empty array', function() { + var message = messages.Inventory([]); + }); + it('should error if arg is not an array of inventory objects', function() { + (function() { + var message = messages.Inventory([Number(0)]); + }).should.throw('Argument is expected to be an array of inventory objects'); + }); + }); + describe('Transaction', function() { it('should be able to pass a custom Transaction', function(done) {