diff --git a/app/common/main-controller_test.js b/app/common/main-controller_test.js index 8618bf9..8780b1f 100644 --- a/app/common/main-controller_test.js +++ b/app/common/main-controller_test.js @@ -208,7 +208,7 @@ describe("Main controller", function () { expect(notifications.updateMessage).toHaveBeenCalledWith('Favorite Updated!', true); }); - it("Given a song that was starred, when I toggle its star, then subsonic service will be called, the song will be starred and a notification will be displayed", function () { + it("Given a song that was starred, when I toggle its star, then subsonic service will be called, the song will no longer be starred and a notification will be displayed", function () { var song = { id: 784, starred: true }; scope.toggleStar(song); deferred.resolve(false); diff --git a/app/queue/queue.js b/app/queue/queue.js index 6cb1475..0b1032f 100644 --- a/app/queue/queue.js +++ b/app/queue/queue.js @@ -7,7 +7,9 @@ angular.module('jamstash.queue.controller', [ 'ngLodash', 'jamstash.player.service', + 'jamstash.subsonic.service', 'jamstash.selectedsongs', + 'jamstash.notifications', 'ui.sortable' ]) @@ -17,28 +19,32 @@ QueueController.$inject = [ '$scope', 'lodash', 'player', - 'SelectedSongs' + 'SelectedSongs', + 'subsonic', + 'notifications' ]; function QueueController( $scope, _, player, - SelectedSongs + SelectedSongs, + subsonic, + notifications ) { 'use strict'; var self = this; _.extend(self, { player: player, - addSongToQueue : player.addSong, emptyQueue : emptyQueue, isPlayingSong : isPlayingSong, playSong : player.play, removeSelectedSongsFromQueue: removeSelectedSongsFromQueue, removeSongFromQueue : player.removeSong, shuffleQueue : shuffleQueue, - toggleSelection : SelectedSongs.toggle + toggleSelection : SelectedSongs.toggle, + toggleStar : toggleStar }); function emptyQueue() { @@ -61,6 +67,17 @@ function QueueController( $('#SideBar').stop().scrollTo('.header', 400); } + // TODO: Hyz: Duplicate of main-controller's toggleStar. + // Refactor in a SubsonicSong service that'll hold all the common operations done on songs. + function toggleStar(song) { + var promise = subsonic.toggleStar(song).then(function (newState) { + song.starred = newState; + notifications.updateMessage('Favorite Updated!', true); + }); + + return promise; + } + $scope.$watch(function () { return player.getPlayingSong(); }, function (newSong) { diff --git a/app/queue/queue_test.js b/app/queue/queue_test.js index 6e48e29..0ab9e52 100644 --- a/app/queue/queue_test.js +++ b/app/queue/queue_test.js @@ -2,13 +2,14 @@ describe("Queue controller", function () { 'use strict'; - var QueueContoller, $scope, player, SelectedSongs; - var song; + var QueueContoller, $scope, player, subsonic, SelectedSongs, notifications, $q, deferred, song; beforeEach(function () { module('jamstash.queue.controller'); - SelectedSongs = jasmine.createSpyObj("SelectedSongs", ["get"]); + SelectedSongs = jasmine.createSpyObj("SelectedSongs", [ + "get" + ]); player = jasmine.createSpyObj("player", [ "emptyQueue", @@ -18,13 +19,27 @@ describe("Queue controller", function () { "shuffleQueue" ]); - inject(function ($controller, $rootScope) { + subsonic = jasmine.createSpyObj("subsonic", [ + "toggleStar" + ]); + + notifications = jasmine.createSpyObj("notifications", [ + "updateMessage" + ]); + + inject(function ($controller, $rootScope, _$q_, lodash) { + $q = _$q_; + deferred = $q.defer(); + $scope = $rootScope.$new(); QueueContoller = $controller('QueueController', { $scope : $scope, + _ : lodash, player : player, - SelectedSongs: SelectedSongs + SelectedSongs: SelectedSongs, + subsonic : subsonic, + notifications: notifications }); }); player.queue = []; @@ -80,4 +95,34 @@ describe("Queue controller", function () { expect($.fn.scrollTo).toHaveBeenCalled(); }); + + describe("toggleStar() -", function () { + beforeEach(function () { + subsonic.toggleStar.and.returnValue(deferred.promise); + }); + + it("Given a song that was not starred, when I toggle its star, then subsonic service will be called, the song will be starred and a notification will be displayed", function () { + song = { id: 4218, starred: false }; + + QueueContoller.toggleStar(song); + deferred.resolve(true); + $scope.$apply(); + + expect(subsonic.toggleStar).toHaveBeenCalledWith(song); + expect(song.starred).toBeTruthy(); + expect(notifications.updateMessage).toHaveBeenCalledWith('Favorite Updated!', true); + }); + + it("Given a song that was starred, when I toggle its star, then subsonic service will be called, the song will be starred and a notification will be displayed", function () { + song = { id: 784, starred: true }; + + QueueContoller.toggleStar(song); + deferred.resolve(false); + $scope.$apply(); + + expect(subsonic.toggleStar).toHaveBeenCalledWith(song); + expect(song.starred).toBeFalsy(); + expect(notifications.updateMessage).toHaveBeenCalledWith('Favorite Updated!', true); + }); + }); });