Please note that you will find all 3 parts of this article as well as the source code of the tutorial in the project’s GitHub repository.
Creating Backbone.js View
In the previous part of the series, we have created our first Backbone.js model using TDD principles.
The third and last part of the article is going to show you a process of developing a Backbone.js view, that will render the markup, handle the user input and reflect model’s state in the UI. Of course, this time we will use the test-driven development principles as well.
Just as it was with the ControlColor
model, we will first write some tests in tests/views/control-color.js
to make sure our view is defined and possesses Backbone-specific functions in its prototype:
QUnit.module( 'views/control-color.js', function() {
QUnit.test( 'Color control view exists and is Backbone view', function( assert ) {
var View = window.ControlColorView;
assert.ok( ! _.isUndefined( View ), 'View is defined' );
assert.ok( _.isFunction( View.prototype.setElement ) && _.isFunction( View.prototype.delegateEvents ), 'View is Backbone view' );
} );
} );
The tests are failing, so we’ll define the ControlColorView
in the src/views/control-color.js
file like:
window.ControlColorView = Backbone.View.extend( {} );
From the red phase we rapidly moved to the green phase. There’s nothing to refactor so far, so we’ll now take care of rendering the markup based on model’s state.
Continue reading Test-driven Development of a Backbone.js App. Part 3.