tmpvar/jsdom — Issue #394: Event delegation fails when specified with non-trivial selectors
Author: nickstenning
Created: 2012-02-01 17:01:37+00:00
State: open

So, my brain hurts. I've been trying to debug some failing tests on a project of mine (okfn/annotator) and have, I think, nailed our issues down to `jsdom`.

In short, it appears that the appropriate event callbacks don't get called when using jQuery's `$.delegate` (or newer `$.on`) functions, but only if the `selector` for the delegation is something more complex than a tag name. To give a concrete minimal testcase, see the following:

```coffee
class TestCase
constructor: ->
@el = $('

')

@el.on('click', '.foo', => this.methodByClass())
@el.on('click', 'a', => this.methodByTagName())
@el.on('click', '#clickMe', => this.methodById())

methodById: ->
methodByClass: ->
methodByTagName: ->

describe 'Delegation failing example', ->
tc = null

beforeEach ->
tc = new TestCase()

it "should delegate events correctly", ->
spyOn(tc, 'methodById')
spyOn(tc, 'methodByClass')
spyOn(tc, 'methodByTagName')

tc.el.find('#clickMe').click()

expect(tc.methodById).toHaveBeenCalled()
expect(tc.methodByClass).toHaveBeenCalled()
expect(tc.methodByTagName).toHaveBeenCalled()
```

(reproduced in full, for easy commandline use, at https://gist.github.com/1718004 -- you'll also need [this file](https://github.com/okfn/annotator/blob/master/test/lib/jasmine.coffee))

I'd expect all three `expect` clauses to pass, and indeed they do if you run this test in a browser. But if I run them in a `jsdom` environment, only `methodByTagName` gets successfully called.

Can you help me? Is this really a bug in `jsdom`, or is it jQuery's fault. More likely, am I failing to set some options when booting jsdom? For reference, our tests have been working until I recently upgraded node, jsdom, etc.