Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting event handlers in objects with jasmine
    primarykey
    data
    text
    <p>I am using a simple MVC pattern in my CoffeeScript ala <a href="http://spinejs.com" rel="nofollow">Spinejs</a>. It allows me to add event listeners by specifying an events object before instantiation:</p> <pre><code>class Controller extends Module @include Events eventSplitter: /^(\S+)\s*(.*)$/ events: {} constructor: (view = '&lt;div/&gt;') -&gt; @view = $(view) @delegate key, func for key, func of @events delegate: (key, func) -&gt; match = key.match @eventSplitter if match[2] is '' @view.on( match[1], $.proxy(@[func], @) ) else @view.on( match[1], match[2], $.proxy(@[func], @) ) </code></pre> <p>Quick explanation - You pass in a selector, element or html string which gets assigned to the view property. Then the delegate method takes an object of events and loops through and assigns event listeners to either the view node itself, or it delegates event handlers from child elements to the view node:</p> <pre><code>class Con extends Controller events: 'click' : 'parent' 'mouseover span' : 'child' parent: (e) -&gt; console.log('div was clicked') child: (e) -&gt; console.log('span was moused over') con = new Con('&lt;div&gt;&lt;span&gt;test&lt;/span&gt;&lt;/div&gt;') con.view.trigger('click') // --&gt; 'div was clicked' con.view.find('span') // --&gt; 'span was moused over' </code></pre> <p>I'm struggling to test this with Jasmine. The following test will pass:</p> <pre><code>expect( con.view ).toHandle('mouseover') expect( con.view ).toHandle('click') </code></pre> <p>Which at least allows me to test that some sort of handler is set, but I would like to test the actual handler being set. I'm trying to spy on the event handlers, but they never seem to be called.</p> <pre><code>spyOn(con, 'parent') con.view.trigger('click') expect( con.parent ).toHaveBeenCalled() // fails! </code></pre> <p>I also tried to spy on the actual method in the prototype.</p> <pre><code>spyOn( Con.prototype, 'parent' ) con.view.trigger('click') expect( Con.prototype.parent ).toHaveBeenCalled() // fails! </code></pre> <p>I can get the test to pass if I call the method explicitly:</p> <pre><code>con.parent(); expect( con.parent ).toHaveBeenCalled() // passes! </code></pre> <p>I think the spy fails when executed by an event because of how the event handler is referenced through the jQuery 'on' method. as in it's a function, but it's not the actual method that's on the con object.</p> <p>Any help would be appreciated. Any general testing advice is welcome as well.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload