Testing Event Handlers

In notebook:
FrontEndMasters BackboneJS
Created at:
2016-06-11
Updated:
2016-06-11
Tags:
JavaScript libraries testing
Testing event handlers for the views is more problematic, since they rely on he DOM.

Writes a test for ​createComment​. 
Not a hard rule what to test. He will test that the interface are called. But he does not test, that the jQuery selector used actually does exist in the DOM. This would be achieved in something like a Selenium test. Selenium would use real browsers.
In the same file as before. 
  describe('.createComment', function(){
  beforeEach(function(){
    this.app = new App();
    // create a stub for every test we will run in this block
    // we do AFTER the app (view) has been initialised
    // so have access to the collection instance
    this.stub = sinon.stub(this.app.colleciton, 'create');
  });
  
  afterEach(function(){
    this.stub.restore();
  })
  
  it('calls event.preventDefault', function(){
    var event = { preventDefault: sinon.stub(); };
    this.app.createComment( event );
    assert.ok( event.preventDefault.called );
  });
  
  it('calls collection.create', function(){
    var event = { preventDefault: sinon.stub()};
    this.app.createComment( event );
    
    assert.ok( this.stub.called()));
  });
})
CI server: github + CircleCI or TravisCI - can run selenium, or browser based