Last active
August 29, 2015 14:05
-
-
Save michaelcox/9f01639b1ab0c00ce267 to your computer and use it in GitHub Desktop.
Backbone Unit Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
var Backbone = require('backbone'); | |
var _ = require('underscore'); | |
var models = require('./models'); | |
var collections = {}; | |
collections.Posts = Backbone.Collection.extend({ | |
model: models.Post, | |
initialize: function() { | |
_.bindAll(this, 'getTopPosts'); | |
}, | |
getTopPosts: function(options) { | |
options = opitons || {}; | |
var posts = this.toJSON(); | |
posts = _.sortBy(posts, function(post) { | |
return post.views; | |
}); | |
return _.first(posts, options.max); | |
} | |
}); | |
return collections; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
var collections = require('src/collections'); | |
var _ = require('underscore'); | |
describe('Collections', function() { | |
describe('Posts Collection', function() { | |
it('should have a method to return the most popular posts', function() { | |
var postsData = [ | |
{ | |
name: 'Some Post 1', | |
views: 1000 | |
}, | |
{ | |
name: 'Some Post 2', | |
views: 5 | |
}, | |
{ | |
name: 'Some Post 3', | |
views: 1100 | |
} | |
]; | |
var posts = new collections.Posts(postsData); | |
var topPosts = posts.getTopPosts({ | |
max: 2 | |
}); | |
expect(topPosts).to.have.length(2); | |
expect(_.pluck(topPosts, 'name')).to.contain('Some Post 1', 'Some Post 3'); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
var Backbone = require('backbone'); | |
var models = {}; | |
models.Post = Backbone.Model.extend(); | |
return models; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
var Backbone = require('backbone'); | |
// Lodash AMD template loader | |
// https://github.com/tbranyen/lodash-template-loader | |
var template = require('ldsh!./posts'); | |
return Backbone.View.extend({ | |
initialize: function() { | |
this.listenTo(this.collection, 'sync', this.render); | |
}, | |
render: function() { | |
var html = template(this.collection.toJSON()); | |
this.$('[data-bind="posts"]').html(html); | |
return this; | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
var collections = require('src/collections'); | |
var PostsView = require('src/posts-view'); | |
var _ = require('underscore'); | |
describe('Views', function() { | |
describe('Posts View', function() { | |
it('should render all posts to the page in a list', function() { | |
var postsData = [ | |
{ | |
name: 'Some Post 1', | |
views: 1000 | |
}, | |
{ | |
name: 'Some Post 2', | |
views: 5 | |
}, | |
{ | |
name: 'Some Post 3', | |
views: 1100 | |
} | |
]; | |
var posts = new collections.Posts(postsData); | |
var postsView = new PostsView({ | |
collection: posts | |
}); | |
// Simulate the event triggered when data returns from the server | |
posts.trigger('sync'); | |
var $output = postsView.$el; | |
expect($output.find('li')).to.have.length(3); | |
expect($output.find('li').at(0).text()).to.equal('Some Post 1'); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
<% _.each(posts, function(post) { %> | |
<li><%= post.name %></li> | |
<% }); %> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original blog post for this is here: http://mdcox.net/posts/backbone-unit-testing.html