js: Don't include params if method is called without arguments

This commit is contained in:
Stein Magnus Jodal 2014-06-18 22:22:42 +02:00
parent 858a6977df
commit 149287c06a
2 changed files with 23 additions and 14 deletions

View File

@ -255,11 +255,12 @@ Mopidy.prototype._getApiSpec = function () {
Mopidy.prototype._createApi = function (methods) {
var caller = function (method) {
return function () {
var params = Array.prototype.slice.call(arguments);
return this._send({
method: method,
params: params
});
var message = {method: method};
if (arguments.length === 0) {
return this._send(message);
}
message.params = Array.prototype.slice.call(arguments);
return this._send(message);
}.bind(this);
}.bind(this);

View File

@ -767,28 +767,36 @@ buster.testCase("Mopidy", {
},
"by-position calling convention": {
setUp: function () {
this.mopidy._createApi({
foo: {
params: ["bar", "baz"]
}
});
this.sendStub = this.stub(this.mopidy, "_send");
},
"is the default": function () {
assert.equals(
this.mopidy._settings.callingConvention,
"by-position-only");
},
"sends messages with function arguments unchanged": function () {
var sendStub = this.stub(this.mopidy, "_send");
this.mopidy._createApi({
foo: {
params: ["bar", "baz"]
}
});
"sends no params if no arguments passed to function": function () {
this.mopidy.foo();
assert.calledOnceWith(this.sendStub, {method: "foo"});
},
"sends messages with function arguments unchanged": function () {
this.mopidy.foo(31, 97);
assert.calledOnceWith(sendStub, {
assert.calledOnceWith(this.sendStub, {
method: "foo",
params: [31, 97]
});
},
}
}
});