Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edoparearyee committed Jul 16, 2015
1 parent 564cbb9 commit f853340
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/js/playlist/controllers/PlaylistCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module("FM.playlist.PlaylistCtrl", [
"FM.api.TracksResource",
"FM.api.UsersResource",
"FM.api.PlayerQueueResource",
"FM.api.PaginationInterceptor",
"ngRoute",
])
/**
Expand Down Expand Up @@ -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;
});
};

Expand Down
47 changes: 38 additions & 9 deletions tests/unit/playlist/controllers/PlaylistCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));

Expand All @@ -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", {
Expand All @@ -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");
});

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
});

});

0 comments on commit f853340

Please sign in to comment.