From 6521c869c45d8af4612d996871ba7df803daca89 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Mon, 22 Jun 2015 07:56:21 +0200 Subject: [PATCH 1/4] Failing test to demonstrate problem --- spec/command-registry-spec.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index b92d3e6b0..799bceb8e 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -158,6 +158,15 @@ describe "CommandRegistry", -> addError = error expect(addError.message).toContain(badSelector) + it "throws an error when called with an non-function callback", -> + badCallback = null + addError = null + try + registry.add '.selector', 'foo:bar', badCallback + catch error + addError = error + expect(addError.message).toContain("Can't register a command with non-function callback.") + describe "::findCommands({target})", -> it "returns commands that can be invoked on the target or its ancestors", -> registry.add '.parent', 'namespace:command-1', -> From d47737718be131781c7af48b1414049e7784c705 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Mon, 22 Jun 2015 07:59:08 +0200 Subject: [PATCH 2/4] Throw error for commands with non-function callbacks --- src/command-registry.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/command-registry.coffee b/src/command-registry.coffee index 72098eda0..a9b953536 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -93,6 +93,9 @@ class CommandRegistry return disposable if typeof target is 'string' + if typeof callback isnt 'function' + throw new Error("Can't register a command with non-function callback.") + validateSelector(target) @addSelectorBasedListener(target, commandName, callback) else From 9aa78606b326cfbe0e82acff388230f76b2c5987 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Mon, 22 Jun 2015 18:47:41 +0200 Subject: [PATCH 3/4] Catch both string and object targets --- src/command-registry.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/command-registry.coffee b/src/command-registry.coffee index a9b953536..870093e2f 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -92,10 +92,10 @@ class CommandRegistry disposable.add @add(target, commandName, callback) return disposable - if typeof target is 'string' - if typeof callback isnt 'function' - throw new Error("Can't register a command with non-function callback.") + if typeof callback isnt 'function' + throw new Error("Can't register a command with non-function callback.") + if typeof target is 'string' validateSelector(target) @addSelectorBasedListener(target, commandName, callback) else From 326457d21b6a5f3bf74b6d11761db97e10edda51 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Mon, 22 Jun 2015 18:55:08 +0200 Subject: [PATCH 4/4] Add test for object target case --- spec/command-registry-spec.coffee | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 799bceb8e..0a8cafa6b 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -158,15 +158,26 @@ describe "CommandRegistry", -> addError = error expect(addError.message).toContain(badSelector) - it "throws an error when called with an non-function callback", -> + it "throws an error when called with a non-function callback and selector target", -> badCallback = null addError = null + try registry.add '.selector', 'foo:bar', badCallback catch error addError = error expect(addError.message).toContain("Can't register a command with non-function callback.") + it "throws an error when called with an non-function callback and object target", -> + badCallback = null + addError = null + + try + registry.add document.body, 'foo:bar', badCallback + catch error + addError = error + expect(addError.message).toContain("Can't register a command with non-function callback.") + describe "::findCommands({target})", -> it "returns commands that can be invoked on the target or its ancestors", -> registry.add '.parent', 'namespace:command-1', ->