Permalink
Browse files

Improve test coverage

  • Loading branch information...
iHiD committed Apr 17, 2013
1 parent fa3fdad commit 709ecc4e93aaefa9baed6ade5200487881c2695e
@@ -13,14 +13,12 @@
setupRoute: function(model, controller, preload) {
var content;
content = [];
- contorller.set("currentPage", 1);
+ controller.set("currentPage", 1);
+ controller.set("searchModel", model);
+ controller.set("content", content);
if (preload) {
- model.find(controller.paginationParams()).addObserver("isLoaded", function() {
- return content.addObjects(this);
- });
+ return controller.updateData();
}
- controller.set("search_model", model);
- return controller.set("content", content);
}
};
template = '{{#if isLoading}}\n Fetching some more stuff <img width="10" src="img/ajax-loader.gif" />\n{{else}}\n {{#if canLoadMore}}\n <a {{action "loadMore" }}> click to load more</a>\n {{else}}\n <strong><em>no more items</em></strong>\n {{/if}}\n{{/if}}';
@@ -53,8 +51,8 @@
return this.updateData();
},
hasItems: function() {
- return this.get("length") > 0;
- }.property("length"),
+ return this.get("content.length") > 0;
+ }.property("content.length"),
search: function() {
this.set("currentPage", 1);
this.get("model").clear();
@@ -64,7 +62,7 @@
var controller;
this.set("isLoading", true);
controller = this;
- return this.get("search_model").find(this.paginationParams()).addObserver("isLoaded", function() {
+ return this.get("searchModel").find(this.paginationParams()).addObserver("isLoaded", function() {
controller.set("isLoading", false);
return controller.get("content").addObjects(this);
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -10,8 +10,8 @@ IHID.InfinitePagination.ControllerMixin = Ember.Mixin.create Ember.Evented,
@updateData()
hasItems: (->
- @get('length') > 0
- ).property('length')
+ @get('content.length') > 0
+ ).property('content.length')
search: ->
@set('currentPage', 1)
@@ -21,7 +21,7 @@ IHID.InfinitePagination.ControllerMixin = Ember.Mixin.create Ember.Evented,
updateData: ->
@set('isLoading', true)
controller = @
- @get('search_model').find(@paginationParams()).addObserver 'isLoaded', ->
+ @get('searchModel').find(@paginationParams()).addObserver 'isLoaded', ->
controller.set('isLoading', false)
controller.get('content').addObjects(@)
@@ -3,15 +3,12 @@ window.IHID.InfinitePagination =
setupRoute: (model, controller, preload) ->
content = []
- contorller.set('currentPage', 1)
- if preload
-
- model.find(controller.paginationParams()).addObserver "isLoaded", ->
- content.addObjects(this)
-
- controller.set 'search_model', model
+ controller.set('currentPage', 1)
+ controller.set 'searchModel', model
controller.set "content", content
+ controller.updateData() if preload
+
template = """
{{#if isLoading}}
Fetching some more stuff <img width="10" src="img/ajax-loader.gif" />
@@ -4,13 +4,23 @@ module "IHID.InfinitePagination.ControllerMixin",
tearDown: ->
window.controller.destroy()
+
+test "defaults are set correctly", ->
+ equal controller.currentPage, 1, "Current page is 1"
+ equal controller.canLoadMore, true, "Can load more is true"
+ equal controller.isLoading, false, "Is loading is false"
test "resetLoadMore sets currentPage back to 1", ->
controller.currentPage = 10
equal controller.currentPage, 10, "precond - currentPage is set to 10"
controller.resetLoadMore()
equal controller.currentPage, 1, "currentPage of controller has been reset to 1"
+test "hasItems correctly determines if there are items", ->
+ equal controller.get('hasItems'), false
+ controller.set('content', [1,2,3,4])
+ equal controller.get('hasItems'), true
+
test "loadMore increments currentPage and invokes updateData", ->
expect 2
@@ -23,7 +33,6 @@ test "loadMore increments currentPage and invokes updateData", ->
equal controller.currentPage, 2, "currentPage has been incremented"
test "search resets page and clears model", ->
- expect 3
controller.currentPage = 4
controller.model = [1,2,3,4]
@@ -34,6 +43,27 @@ test "search resets page and clears model", ->
equal controller.currentPage, 1, "currentPage has been incremented"
equal controller.model.length, 0, "model has been cleared"
+test "search calls updateData()", ->
+ controller.model = [1,2,3,4]
+ controller.reopen updateData: ->
+ ok true, "updateData has been called"
+ controller.search()
+
+test "update Data toggles isLoading", ->
+ controller.set('content', [])
+ controller.set('searchModel', {find: -> {addObserver: (s,f) -> f() }})
+ controller.set('isLoading', true)
+ controller.updateData()
+ equal controller.get('isLoading'), false
+
+test "update Data toggles addsObjects to content", ->
+ controller.set('content', [])
+ o = [1,2,3,4]
+ o.addObserver = ((eventName,callback) -> callback.call(@))
+ controller.set('searchModel', {find: (params) -> o})
+ controller.updateData()
+ equal JSON.stringify(controller.get('content')), JSON.stringify([1,2,3,4])
+
test "paginationParams adds currentPage", ->
controller.currentPage = 4
equal JSON.stringify(controller.paginationParams()),
@@ -1,4 +1,29 @@
module "IHID"
test "it is defined and an Ember.Namespace", ->
- ok IHID, "IHID is defined"
+ ok IHID, "IHID is defined"
+
+module "IHID.InfinitePagination",
+ setup: ->
+ window.controller = Ember.Object.create()#WithMixins IHID.InfinitePagination.ControllerMixin
+ window.model = Ember.Object.create()#WithMixins IHID.InfinitePagination.ControllerMixin
+
+ tearDown: ->
+ window.controller.destroy()
+
+test "setupRoute sets controller's variables correctly", ->
+ IHID.InfinitePagination.setupRoute(model, controller)
+ equal controller.get('currentPage'), 1
+ equal controller.get('searchModel'), model
+ equal JSON.stringify(controller.get('content')), "[]"
+
+test "controller.updateData should not be called if preload is true", ->
+ expect(0)
+ controller.reopen updateData: ->
+ ok false, "updateData should not be called without preloading specified"
+ IHID.InfinitePagination.setupRoute(model, controller)
+
+test "controller.updateData should be called if preload is true", ->
+ controller.reopen updateData: ->
+ ok true, "updateData should be called without preloading specified"
+ IHID.InfinitePagination.setupRoute(model, controller, true)

0 comments on commit 709ecc4

Please sign in to comment.