From ea73be17468b1bd02171c82e1fb21b1bc16ccd0e Mon Sep 17 00:00:00 2001 From: Jez Ng Date: Sat, 20 Oct 2012 23:17:30 -0400 Subject: Refactor handlerStack. Closes #657. Previously, handlerStack was designed only for removal of the handler right at the top of the stack. However, some handlers sought to remove themselves when they were not at the top of the stack, creating confusion. The new handlerStack ensures that such removal can always be done safely. --- tests/unit_tests/handler_stack_test.coffee | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/unit_tests/handler_stack_test.coffee (limited to 'tests/unit_tests') diff --git a/tests/unit_tests/handler_stack_test.coffee b/tests/unit_tests/handler_stack_test.coffee new file mode 100644 index 00000000..0ed8f4c0 --- /dev/null +++ b/tests/unit_tests/handler_stack_test.coffee @@ -0,0 +1,52 @@ +require "./test_helper.js" +extend(global, require "../../lib/handler_stack.js") + +context "handlerStack", + setup -> + stub global, "DomUtils", {} + stub DomUtils, "suppressEvent", -> + @handlerStack = new HandlerStack + @handler1Called = false + @handler2Called = false + + should "bubble events", -> + @handlerStack.push { keydown: => @handler1Called = true } + @handlerStack.push { keydown: => @handler2Called = true } + @handlerStack.bubbleEvent 'keydown', {} + assert.isTrue @handler2Called + assert.isTrue @handler1Called + + should "terminate bubbling on falsy return value", -> + @handlerStack.push { keydown: => @handler1Called = true } + @handlerStack.push { keydown: => @handler2Called = true; false } + @handlerStack.bubbleEvent 'keydown', {} + assert.isTrue @handler2Called + assert.isFalse @handler1Called + + should "remove handlers correctly", -> + @handlerStack.push { keydown: => @handler1Called = true } + handlerId = @handlerStack.push { keydown: => @handler2Called = true } + @handlerStack.remove handlerId + @handlerStack.bubbleEvent 'keydown', {} + assert.isFalse @handler2Called + assert.isTrue @handler1Called + + should "remove handlers correctly", -> + handlerId = @handlerStack.push { keydown: => @handler1Called = true } + @handlerStack.push { keydown: => @handler2Called = true } + @handlerStack.remove handlerId + @handlerStack.bubbleEvent 'keydown', {} + assert.isTrue @handler2Called + assert.isFalse @handler1Called + + should "handle self-removing handlers correctly", -> + ctx = @ + @handlerStack.push { keydown: => @handler1Called = true } + @handlerStack.push { keydown: -> + ctx.handler2Called = true + @remove() + } + @handlerStack.bubbleEvent 'keydown', {} + assert.isTrue @handler2Called + assert.isTrue @handler1Called + assert.equal @handlerStack.stack.length, 1 -- cgit v1.2.3