Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a low-levelish way of doing it with jasmine, testing that pushState works as expected and that your router sets up things properly... I assume a <code>router</code> that has been initialized and has a <em>home</em> route mapped to ''. You can adapt this for your other routes. I also assume you've done in your app initialization a <code>Backbone.history.start({ pushState: true });</code></p> <pre><code> describe('app.Router', function () { var router = app.router, pushStateSpy; it('has a "home" route', function () { expect(router.routes['']).toEqual('home'); }); it('triggers the "home" route', function () { var home = spyOn(router, 'home').andCallThrough(); pushStateSpy = spyOn(window.history, 'pushState').andCallFake(function (data, title, url) { expect(url).toEqual('/'); router.home(); }); router.navigate(''); expect(pushStateSpy).toHaveBeenCalled(); expect(home).toHaveBeenCalled(); ... }); }); </code></pre> <p>You can effectively achieve similar things by doing <code>Backbone.history.stop();</code> it's meant for this reason.</p> <p><strong>UPDATE:</strong> Browsers with no <code>pushState</code>:</p> <p>This of course will work fine if your browser you test on has support for <code>pushState</code>. If you test against browsers that don't, you can conditionally test as follows:</p> <pre><code>it('triggers the "home" route', function () { var home = spyOn(router, 'home').andCallThrough(); if (Backbone.history._hasPushState) { pushStateSpy = spyOn(window.history, 'pushState').andCallFake(function (data, title, url) { expect(url).toEqual('/'); router.home(); }); router.navigate('', {trigger: true}); expect(pushStateSpy).toHaveBeenCalled(); expect(home).toHaveBeenCalled(); } else if (Backbone.history._wantsHashChange) { var updateHashSpy = spyOn(Backbone.history, '_updateHash').andCallFake(function (loc, frag) { expect(frag).toEqual(''); router.home(); }); router.navigate('', {trigger: true}); expect(updateHashSpy).toHaveBeenCalled(); expect(home).toHaveBeenCalled(); } }); </code></pre> <p>If you are on IE6, good luck.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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