This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-10 17:43:44 -08:00
parent 4e7b1b8979
commit 0346671d18

View File

@@ -2,10 +2,10 @@ GlobalKeymap = require 'global-keymap'
$ = require 'jquery'
describe "GlobalKeymap", ->
handler = null
keymap = null
beforeEach ->
handler = new GlobalKeymap
keymap = new GlobalKeymap
describe "handleKeyEvent", ->
fragment = null
@@ -13,8 +13,8 @@ describe "GlobalKeymap", ->
insertCharHandler = null
beforeEach ->
handler.bindKeys '.command-mode', 'x': 'deleteChar'
handler.bindKeys '.insert-mode', 'x': 'insertChar'
keymap.bindKeys '.command-mode', 'x': 'deleteChar'
keymap.bindKeys '.insert-mode', 'x': 'insertChar'
fragment = $ """
<div class="command-mode">
@@ -31,48 +31,52 @@ describe "GlobalKeymap", ->
describe "when the event's target node matches a selector with a matching binding", ->
it "triggers the command event associated with that binding on the target node", ->
handler.handleKeyEvent(keypressEvent('x', target: fragment[0]))
keymap.handleKeyEvent(keypressEvent('x', target: fragment[0]))
expect(deleteCharHandler).toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
deleteCharHandler.reset()
fragment.removeClass('command-mode').addClass('insert-mode')
handler.handleKeyEvent(keypressEvent('x', target: fragment[0]))
keymap.handleKeyEvent(keypressEvent('x', target: fragment[0]))
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).toHaveBeenCalled()
describe "when no binding matches the event", ->
it "returns false", ->
expect(keymap.handleKeyEvent(keypressEvent('0', target: fragment[0]))).toBe(false)
describe "when the event's target node *descends* from a selector with a matching binding", ->
it "triggers the command event associated with that binding on the target node", ->
target = fragment.find('.child-node')[0]
handler.handleKeyEvent(keypressEvent('x', target: target))
keymap.handleKeyEvent(keypressEvent('x', target: target))
expect(deleteCharHandler).toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
deleteCharHandler.reset()
fragment.removeClass('command-mode').addClass('insert-mode')
handler.handleKeyEvent(keypressEvent('x', target: target))
keymap.handleKeyEvent(keypressEvent('x', target: target))
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).toHaveBeenCalled()
describe "when the event's target node descends from *multiple* selectors with a matching binding", ->
it "only triggers bindings on selectors associated with the closest ancestor node", ->
handler.bindKeys '.child-node', 'x': 'foo'
keymap.bindKeys '.child-node', 'x': 'foo'
fooHandler = jasmine.createSpy 'fooHandler'
fragment.on 'foo', fooHandler
target = fragment.find('.grandchild-node')[0]
handler.handleKeyEvent(keypressEvent('x', target: target))
keymap.handleKeyEvent(keypressEvent('x', target: target))
expect(fooHandler).toHaveBeenCalled()
expect(deleteCharHandler).not.toHaveBeenCalled()
expect(insertCharHandler).not.toHaveBeenCalled()
describe "when the event bubbles to a node that matches multiple selectors", ->
it "triggers the binding for the most specific selector", ->
handler.bindKeys 'div .child-node', 'x': 'foo'
handler.bindKeys '.command-mode .child-node', 'x': 'baz'
handler.bindKeys '.child-node', 'x': 'bar'
keymap.bindKeys 'div .child-node', 'x': 'foo'
keymap.bindKeys '.command-mode .child-node', 'x': 'baz'
keymap.bindKeys '.child-node', 'x': 'bar'
fooHandler = jasmine.createSpy 'fooHandler'
barHandler = jasmine.createSpy 'barHandler'
@@ -82,8 +86,9 @@ describe "GlobalKeymap", ->
fragment.on 'baz', bazHandler
target = fragment.find('.grandchild-node')[0]
handler.handleKeyEvent(keypressEvent('x', target: target))
keymap.handleKeyEvent(keypressEvent('x', target: target))
expect(fooHandler).not.toHaveBeenCalled()
expect(barHandler).not.toHaveBeenCalled()
expect(bazHandler).toHaveBeenCalled()