diff --git a/app/js/playlist/controllers/PlaylistCtrl.js b/app/js/playlist/controllers/PlaylistCtrl.js index e0bb18c9..9f4716cb 100644 --- a/app/js/playlist/controllers/PlaylistCtrl.js +++ b/app/js/playlist/controllers/PlaylistCtrl.js @@ -11,6 +11,7 @@ angular.module("FM.playlist.PlaylistCtrl", [ "FM.api.TracksResource", "FM.api.UsersResource", "FM.api.PlayerQueueResource", + "FM.api.PaginationInterceptor", "ngRoute", ]) /** @@ -112,7 +113,7 @@ angular.module("FM.playlist.PlaylistCtrl", [ $scope.playlist = $scope.playlist.concat(response.items); $scope.page.loading = false; - $scope.page.total = response.meta.totalPages ? response.meta.totalPages : 0; + $scope.page.total = response.meta.totalPages ? response.meta.totalPages : 1; }); }; diff --git a/tests/unit/playlist/controllers/PlaylistCtrl.js b/tests/unit/playlist/controllers/PlaylistCtrl.js index c9d5bb99..0d139d3c 100644 --- a/tests/unit/playlist/controllers/PlaylistCtrl.js +++ b/tests/unit/playlist/controllers/PlaylistCtrl.js @@ -3,19 +3,26 @@ describe("FM.playlist.PlaylistCtrl", function() { var $rootScope, $location, $route, $scope, $q, $httpBackend, - TracksResource, UsersResource, PlayerQueueResource, playlistData, playlistMeta; + TracksResource, UsersResource, PlayerQueueResource, playlistData, playlistMeta, + queue, queueMeta, queueHeader, users, tracks; beforeEach(function (){ module("FM.playlist.PlaylistCtrl"); }); - beforeEach(inject(function (_$httpBackend_) { - $httpBackend = _$httpBackend_ + beforeEach(inject(function (_$httpBackend_) { + $httpBackend = _$httpBackend_; - $httpBackend.whenGET(/.*player\/queue/).respond(200, [{ track: { uri: "foo", duration: 100 } },{ track: { uri: "bar", duration: 100 } }]); - $httpBackend.whenGET(/.*player\/queue\/meta/).respond(200, {"play_time": 20100, "genres": {"dirty south rap": 2, "pop": 12, "quiet storm": 1}, "total": 2, "users": {"fef86892-0a28-4b26-b0b3-90a1050cfffd": 2}}); - $httpBackend.whenGET(/.*users.*/).respond(200, {"family_name": "Light", "display_name": "Alex Light", "avatar_url": "http://placehold.it/400", "spotify_playlists": null, "given_name": "Alex", "id": "16369f65-6aa5-4d04-8927-a77016d0d721"}); - $httpBackend.whenGET(/.*tracks.*/).respond(200, {"album": {"id": "d7b737a9-d70b-49a9-9f42-8c204b342000", "images": [{"url": "http://placehold.it/640x629?text=Album+Art", "width": 640, "height": 629}, {"url": "http://placehold.it/300x295?text=Album+Art", "width": 300, "height": 295}, {"url": "http://placehold.it/64x63?text=Album+Art", "width": 64, "height": 63}], "name": "Boston", "uri": "spotify:album:2QLp07RO6anZHmtcKTEvSC"}, "name": "More Than a Feeling", "uri": "spotify:track:1QEEqeFIZktqIpPI4jSVSF", "play_count": 0, "artists": [{"id": "8c22640a-02ef-4ee0-90eb-87c9c9a2534f", "uri": "spotify:artist:29kkCKKGXheHuoO829FxWK", "name": "Boston"}], "duration": 285133, "id": "0739b113-ad3a-47a4-bea9-edb00ba192f5"}); + queue = [{ track: { uri: "foo", duration: 100 } },{ track: { uri: "bar", duration: 100 } }]; + queueHeader = {"Total-Pages": "2", "Total-Count": "4"}; + queueMeta = {"play_time": 20100, "genres": {"dirty south rap": 2, "pop": 12, "quiet storm": 1}, "total": 2, "users": {"fef86892-0a28-4b26-b0b3-90a1050cfffd": 2}}; + users = {"family_name": "Light", "display_name": "Alex Light", "avatar_url": "http://placehold.it/400", "spotify_playlists": null, "given_name": "Alex", "id": "16369f65-6aa5-4d04-8927-a77016d0d721"}; + tracks = {"album": {"id": "d7b737a9-d70b-49a9-9f42-8c204b342000", "images": [{"url": "http://placehold.it/640x629?text=Album+Art", "width": 640, "height": 629}, {"url": "http://placehold.it/300x295?text=Album+Art", "width": 300, "height": 295}, {"url": "http://placehold.it/64x63?text=Album+Art", "width": 64, "height": 63}], "name": "Boston", "uri": "spotify:album:2QLp07RO6anZHmtcKTEvSC"}, "name": "More Than a Feeling", "uri": "spotify:track:1QEEqeFIZktqIpPI4jSVSF", "play_count": 0, "artists": [{"id": "8c22640a-02ef-4ee0-90eb-87c9c9a2534f", "uri": "spotify:artist:29kkCKKGXheHuoO829FxWK", "name": "Boston"}], "duration": 285133, "id": "0739b113-ad3a-47a4-bea9-edb00ba192f5"}; + + $httpBackend.whenGET(/.*player\/queue/).respond(200, queue, queueHeader); + $httpBackend.whenGET(/.*player\/queue\/meta/).respond(200, queueMeta); + $httpBackend.whenGET(/.*users.*/).respond(200, users); + $httpBackend.whenGET(/.*tracks.*/).respond(200, tracks); $httpBackend.whenGET(/partials\/.*/).respond(200); })); @@ -39,7 +46,7 @@ describe("FM.playlist.PlaylistCtrl", function() { UsersResource = $injector.get("UsersResource"); spyOn(UsersResource, "get").and.callThrough(); - playlistData = { items: [{ track: { uri: "foo", duration: 100 } },{ track: { uri: "bar", duration: 100 } }]}; + playlistData = { items: [{ track: { uri: "foo", duration: 100 } },{ track: { uri: "bar", duration: 100 } }], meta: { totalCount: 4, totalPages: 2 }}; playlistMeta = {"play_time": 20100, "genres": {"dirty south rap": 2, "pop": 12, "quiet storm": 1}, "total": 2, "users": {"fef86892-0a28-4b26-b0b3-90a1050cfffd": 9}}; $controller("PlaylistCtrl", { @@ -60,8 +67,8 @@ describe("FM.playlist.PlaylistCtrl", function() { it("should load views", function() { $location.path("/"); - $rootScope.$digest(); $httpBackend.flush(); + $rootScope.$digest(); expect($route.current.controller).toBe("PlaylistCtrl"); }); @@ -86,6 +93,8 @@ describe("FM.playlist.PlaylistCtrl", function() { }); it("should remove item from playlist on end event", function(){ + $scope.page.total = 1; + $scope.onEnd({},{ uri: "baz" }); expect($scope.playlist.length).toBe(2); @@ -99,6 +108,8 @@ describe("FM.playlist.PlaylistCtrl", function() { }); it("should update playlist meta on end event", function(){ + $scope.page.total = 1; + $scope.onEnd({},{ uri: "foo" }); $httpBackend.flush(); expect($scope.meta.play_time).toEqual(20000); @@ -119,4 +130,22 @@ describe("FM.playlist.PlaylistCtrl", function() { expect($scope.meta.play_time).toEqual(playTime); }); + it("should load more tracks", function(){ + expect($scope.playlist.length).toBe(2); + + $scope.loadMore(); + $httpBackend.flush(); + $rootScope.$digest(); + + expect($scope.playlist.length).toBe(4); + + queueHeader["Total-Pages"] = undefined; + + $scope.loadMore(); + $httpBackend.flush(); + $rootScope.$digest(); + + expect($scope.playlist.length).toBe(6); + }); + });