From 155fb675ec1476c290fe43ca47c84ca276506107 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 9 Sep 2014 11:52:30 -0600 Subject: [PATCH] :lipstick: group dispatch specs --- spec/command-registry-spec.coffee | 143 +++++++++++++++--------------- 1 file changed, 72 insertions(+), 71 deletions(-) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index b39678c91..35b8a02b6 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -16,97 +16,98 @@ describe "CommandRegistry", -> registry = new CommandRegistry(parent) - 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 + 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 - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(called).toBe true + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(called).toBe true - it "invokes callbacks with selectors matching ancestors of the target", -> - calls = [] + it "invokes callbacks with selectors matching ancestors of the target", -> + calls = [] - registry.add '.child', 'command', (event) -> - expect(this).toBe child - expect(event.target).toBe grandchild - expect(event.currentTarget).toBe child - calls.push('child') + registry.add '.child', 'command', (event) -> + expect(this).toBe child + expect(event.target).toBe grandchild + expect(event.currentTarget).toBe child + calls.push('child') - registry.add '.parent', 'command', (event) -> - expect(this).toBe parent - expect(event.target).toBe grandchild - expect(event.currentTarget).toBe parent - calls.push('parent') + registry.add '.parent', 'command', (event) -> + expect(this).toBe parent + expect(event.target).toBe grandchild + expect(event.currentTarget).toBe parent + calls.push('parent') - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual ['child', 'parent'] + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child', 'parent'] - it "orders multiple matching listeners for an element by selector specificity", -> - child.classList.add('foo', 'bar') - calls = [] + it "orders multiple matching listeners for an element by selector specificity", -> + child.classList.add('foo', 'bar') + calls = [] - 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 + 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 - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual ['.foo.bar', '.bar', '.foo'] + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['.foo.bar', '.bar', '.foo'] - it "stops bubbling through ancestors when .stopPropagation() is called on the event", -> - calls = [] + it "stops bubbling through ancestors when .stopPropagation() is called on the event", -> + calls = [] - 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() + 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() - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual ['child-1', 'child-2'] + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child-1', 'child-2'] - it "stops invoking callbacks when .stopImmediatePropagation() is called on the event", -> - calls = [] + it "stops invoking callbacks when .stopImmediatePropagation() is called on the event", -> + calls = [] - 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() + 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() - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual ['child-1'] + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child-1'] - it "allows listeners to be removed via a disposable returned by ::add", -> - calls = [] + it "allows listeners to be removed via a disposable returned by ::add", -> + calls = [] - disposable1 = registry.add '.parent', 'command', -> calls.push('parent') - disposable2 = registry.add '.child', 'command', -> calls.push('child') + disposable1 = registry.add '.parent', 'command', -> calls.push('parent') + disposable2 = registry.add '.child', 'command', -> calls.push('child') - disposable1.dispose() - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual ['child'] + disposable1.dispose() + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child'] - calls = [] - disposable2.dispose() - grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) - expect(calls).toEqual [] + calls = [] + disposable2.dispose() + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual [] - it "allows multiple commands to be registered under one selector when called with an object", -> - calls = [] + it "allows multiple commands to be registered under one selector when called with an object", -> + calls = [] - disposable = registry.add '.child', - 'command-1': -> calls.push('command-1') - 'command-2': -> calls.push('command-2') + disposable = registry.add '.child', + 'command-1': -> calls.push('command-1') + 'command-2': -> calls.push('command-2') - grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) - grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) + grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) + grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) - expect(calls).toEqual ['command-1', 'command-2'] + expect(calls).toEqual ['command-1', 'command-2'] - calls = [] - disposable.dispose() - grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) - grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) - expect(calls).toEqual [] + calls = [] + disposable.dispose() + grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) + grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) + expect(calls).toEqual []