diff options
| author | Jez Ng | 2012-10-20 23:17:30 -0400 | 
|---|---|---|
| committer | Jez Ng | 2012-10-20 23:17:45 -0400 | 
| commit | ea73be17468b1bd02171c82e1fb21b1bc16ccd0e (patch) | |
| tree | 223e4a3b56a410f599e19f81b38752ded86d44c6 /tests/unit_tests/handler_stack_test.coffee | |
| parent | 2ed217b43663a553c78fa08a8a10152a43e93cf5 (diff) | |
| download | vimium-ea73be17468b1bd02171c82e1fb21b1bc16ccd0e.tar.bz2 | |
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.
Diffstat (limited to 'tests/unit_tests/handler_stack_test.coffee')
| -rw-r--r-- | tests/unit_tests/handler_stack_test.coffee | 52 | 
1 files changed, 52 insertions, 0 deletions
| 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 | 
