Skip to content

Instantly share code, notes, and snippets.

@michaelcox
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelcox/9f01639b1ab0c00ce267 to your computer and use it in GitHub Desktop.
Save michaelcox/9f01639b1ab0c00ce267 to your computer and use it in GitHub Desktop.
Backbone Unit Testing
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;
});
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');
});
});
});
});
define(function(require) {
var Backbone = require('backbone');
var models = {};
models.Post = Backbone.Model.extend();
return models;
});
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;
}
});
});
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');
});
});
});
});
<ul>
<% _.each(posts, function(post) { %>
<li><%= post.name %></li>
<% }); %>
</ul>
@michaelcox
Copy link
Author

The original blog post for this is here: http://mdcox.net/posts/backbone-unit-testing.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment