pulsar/spec/command-registry-spec.coffee

127 lines
5.0 KiB
CoffeeScript
Raw Normal View History

2014-09-05 18:22:28 +04:00
CommandRegistry = require '../src/command-registry'
describe "CommandRegistry", ->
[registry, parent, child, grandchild] = []
beforeEach ->
parent = document.createElement("div")
child = document.createElement("div")
grandchild = document.createElement("div")
parent.classList.add('parent')
child.classList.add('child')
grandchild.classList.add('grandchild')
child.appendChild(grandchild)
parent.appendChild(child)
document.querySelector('#jasmine-content').appendChild(parent)
registry = new CommandRegistry(parent)
2014-09-09 21:52:30 +04:00
describe "command dispatch", ->
it "invokes callbacks with selectors matching the target", ->
called = false
registry.add '.grandchild', 'command', (event) ->
expect(this).toBe grandchild
expect(event.type).toBe 'command'
expect(event.eventPhase).toBe Event.BUBBLING_PHASE
expect(event.target).toBe grandchild
expect(event.currentTarget).toBe grandchild
called = true
2014-09-05 18:22:28 +04:00
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(called).toBe true
2014-09-09 21:52:30 +04:00
it "invokes callbacks with selectors matching ancestors of the target", ->
calls = []
2014-09-09 21:52:30 +04:00
registry.add '.child', 'command', (event) ->
expect(this).toBe child
expect(event.target).toBe grandchild
expect(event.currentTarget).toBe child
calls.push('child')
2014-09-09 21:52:30 +04:00
registry.add '.parent', 'command', (event) ->
expect(this).toBe parent
expect(event.target).toBe grandchild
expect(event.currentTarget).toBe parent
calls.push('parent')
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['child', 'parent']
2014-09-09 21:52:30 +04:00
it "orders multiple matching listeners for an element by selector specificity", ->
child.classList.add('foo', 'bar')
calls = []
2014-09-09 21:52:30 +04:00
registry.add '.foo.bar', 'command', -> calls.push('.foo.bar')
registry.add '.foo', 'command', -> calls.push('.foo')
registry.add '.bar', 'command', -> calls.push('.bar') # specificity ties favor commands added later, like CSS
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['.foo.bar', '.bar', '.foo']
2014-09-09 21:52:30 +04:00
it "stops bubbling through ancestors when .stopPropagation() is called on the event", ->
calls = []
2014-09-09 21:52:30 +04:00
registry.add '.parent', 'command', -> calls.push('parent')
registry.add '.child', 'command', -> calls.push('child-2')
registry.add '.child', 'command', (event) -> calls.push('child-1'); event.stopPropagation()
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['child-1', 'child-2']
2014-09-09 21:52:30 +04:00
it "stops invoking callbacks when .stopImmediatePropagation() is called on the event", ->
calls = []
2014-09-09 21:52:30 +04:00
registry.add '.parent', 'command', -> calls.push('parent')
registry.add '.child', 'command', -> calls.push('child-2')
registry.add '.child', 'command', (event) -> calls.push('child-1'); event.stopImmediatePropagation()
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['child-1']
2014-09-09 21:52:30 +04:00
it "allows listeners to be removed via a disposable returned by ::add", ->
calls = []
2014-09-09 21:52:30 +04:00
disposable1 = registry.add '.parent', 'command', -> calls.push('parent')
disposable2 = registry.add '.child', 'command', -> calls.push('child')
2014-09-09 21:52:30 +04:00
disposable1.dispose()
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['child']
2014-09-09 21:52:30 +04:00
calls = []
disposable2.dispose()
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual []
2014-09-09 21:52:30 +04:00
it "allows multiple commands to be registered under one selector when called with an object", ->
calls = []
2014-09-09 21:52:30 +04:00
disposable = registry.add '.child',
'command-1': -> calls.push('command-1')
'command-2': -> calls.push('command-2')
2014-09-09 21:52:30 +04:00
grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true))
grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true))
2014-09-09 21:52:30 +04:00
expect(calls).toEqual ['command-1', 'command-2']
2014-09-09 21:52:30 +04:00
calls = []
disposable.dispose()
grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true))
grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true))
expect(calls).toEqual []
2014-09-09 22:42:55 +04:00
describe "::findCommands({target})", ->
it "returns commands that can be invoked on the target or its ancestors", ->
registry.add '.parent', 'namespace:command-1', ->
registry.add '.child', 'namespace:command-2', ->
registry.add '.grandchild', 'namespace:command-3', ->
registry.add '.grandchild.no-match', 'namespace:command-4', ->
expect(registry.findCommands(target: grandchild)[0..2]).toEqual [
2014-09-09 22:42:55 +04:00
{name: 'namespace:command-3', displayName: 'Namespace: Command 3'}
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'}
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]