From db86abdf70e7eebdb05fa8ed64a1d3574e1df5c6 Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 4 Sep 2014 00:16:06 -0700 Subject: [PATCH] Add list view tests Also, * moved fetch out of the list view * removed unused #last() function * put test setup lines in their own tiny file. * added data-cover to view script tags for code coveage reports. --- js/views/conversation_view.js | 1 + js/views/list_view.js | 6 +----- test/test.js | 2 ++ test/test_views.html | 28 +++++++++++++++++----------- test/views/list_view_test.js | 22 ++++++++++++++++++++++ test/views/message_view_test.js | 2 -- 6 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 test/test.js create mode 100644 test/views/list_view_test.js diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 1c4c62a5..95790af0 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -12,6 +12,7 @@ var Whisper = Whisper || {}; this.$el.html(Mustache.render(this.template)); this.view = new Whisper.MessageListView({collection: this.model.messages()}); + this.model.messages().fetch({reset: true}); this.$el.find('.discussion-container').append(this.view.el); }, events: { diff --git a/js/views/list_view.js b/js/views/list_view.js index 3a39de7b..6d9444cb 100644 --- a/js/views/list_view.js +++ b/js/views/list_view.js @@ -9,12 +9,12 @@ var Whisper = Whisper || {}; */ Whisper.ListView = Backbone.View.extend({ tagName: 'ul', + itemView: Backbone.View, initialize: function() { this.listenTo(this.collection, 'change', this.render); // auto update this.listenTo(this.collection, 'add', this.addOne); this.listenTo(this.collection, 'reset', this.addAll); this.listenTo(this.collection, 'all', this.render); - this.collection.fetch({reset: true}); }, addOne: function(model) { @@ -27,10 +27,6 @@ var Whisper = Whisper || {}; addAll: function() { this.$el.html(''); this.collection.each(this.addOne, this); - }, - - last: function() { - this.collection.at(this.collection.length - 1); } }); })(); diff --git a/test/test.js b/test/test.js new file mode 100644 index 00000000..593c764b --- /dev/null +++ b/test/test.js @@ -0,0 +1,2 @@ +mocha.setup("bdd"); +window.assert = chai.assert; diff --git a/test/test_views.html b/test/test_views.html index a156511d..b86579e2 100644 --- a/test/test_views.html +++ b/test/test_views.html @@ -126,26 +126,32 @@ - + - + - + + - - - - - - - - + + + + + + + + + + + + + diff --git a/test/views/list_view_test.js b/test/views/list_view_test.js new file mode 100644 index 00000000..7167e7aa --- /dev/null +++ b/test/views/list_view_test.js @@ -0,0 +1,22 @@ +describe('ListView', function() { + var collection; + + beforeEach(function(){ + collection = new Backbone.Collection(); + }); + + it('should add children to the list element as they are added to the collection', function() { + var view = new Whisper.ListView({collection: collection}); + collection.add('hello'); + assert.equal(view.$el.children().length, 1); + collection.add('world'); + assert.equal(view.$el.children().length, 2); + }); + + it('should add all the children to the list element on reset', function() { + var view = new Whisper.ListView({collection: collection}); + collection.reset(['goodbye', 'world']); + assert.equal(view.$el.children().length, 2); + }); + +}); diff --git a/test/views/message_view_test.js b/test/views/message_view_test.js index 8f0e0eee..af4ac61a 100644 --- a/test/views/message_view_test.js +++ b/test/views/message_view_test.js @@ -1,5 +1,3 @@ -mocha.setup("bdd"); -window.assert = chai.assert; describe('MessageView', function() { var message = Whisper.Messages.add({ threadId: 'test-thread',