Skip to content

Commit d75ab83

Browse files
author
Philip Roberts
committed
Add tests for colon support in eventnames, and binding to self
1 parent 7aaf07f commit d75ab83

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

catalyst/test/bind.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('bind', () => {
1616
}
1717
querySelectorAll() {}
1818
foo() {}
19+
matches() {}
1920
}
2021

2122
it('queries for Elements matching data-action*="tagname"', () => {
@@ -42,6 +43,40 @@ describe('bind', () => {
4243
expect(instance.foo).to.have.been.called(1)
4344
})
4445

46+
it('allows for the presence of `:` in an event name', () => {
47+
const instance = new MyController()
48+
spy.on(instance, 'foo')
49+
const el = new FakeElement()
50+
instance.querySelectorAll = () => [el]
51+
el.closest = () => instance
52+
el.getAttribute = () => 'custom:event:my-controller#foo'
53+
spy.on(el, 'addEventListener')
54+
bind(instance)
55+
expect(el.addEventListener).to.have.been.called.once.with('custom:event')
56+
const {calls} = el.addEventListener.__spy
57+
const fn = calls[0][1]
58+
expect(instance.foo).to.have.not.been.called()
59+
fn()
60+
expect(instance.foo).to.have.been.called(1)
61+
})
62+
63+
it('binds events on the controller to itself', () => {
64+
const instance = new MyController()
65+
spy.on(instance, 'foo')
66+
instance.matches = () => true
67+
instance.getAttribute = () => 'click:my-controller#foo'
68+
instance.addEventListener = () => true
69+
instance.querySelectorAll = () => []
70+
spy.on(instance, 'addEventListener')
71+
bind(instance)
72+
expect(instance.addEventListener).to.have.been.called.once.with('click')
73+
const {calls} = instance.addEventListener.__spy
74+
const fn = calls[0][1]
75+
expect(instance.foo).to.have.not.been.called()
76+
fn()
77+
expect(instance.foo).to.have.been.called(1)
78+
})
79+
4580
it('does not bind elements whose closest selector is not this controller', () => {
4681
const instance = new MyController()
4782
spy.on(instance, 'foo')

0 commit comments

Comments
 (0)