Refactors subsonic-service's savePlaylist() to use subsonicRequest()

So we can move closer to consistent error-reporting and protocol usage.
- refactors subonic-controller's savePlaylist().
- Adds unit-tests.
This commit is contained in:
Hyzual 2015-03-04 00:48:25 +01:00
parent 715bf413d5
commit d5c6b45dba
4 changed files with 74 additions and 28 deletions

View file

@ -454,30 +454,19 @@ angular.module('jamstash.subsonic.service', ['jamstash.settings', 'jamstash.util
return promise;
},
savePlaylist: function () {
var deferred = $q.defer();
if (content.selectedPlaylist !== null) {
var id = content.selectedPlaylist;
var songs = [];
angular.forEach(content.song, function (item, key) {
songs.push(item.id);
});
if (songs.length > 0) {
$.ajax({
type: 'GET',
url: globals.BaseURL() + '/createPlaylist.view?' + globals.BaseParams(),
dataType: globals.settings.Protocol,
timeout: globals.settings.Timeout,
data: { playlistId: id, songId: songs },
success: function () {
deferred.resolve();
},
traditional: true // Fixes POST with an array in JQuery 1.4
});
savePlaylist: function (playlistId, songs) {
var params = {
params: {
playlistId: playlistId,
songId: []
}
};
for (var i = 0; i < songs.length; i++) {
params.params.songId.push(songs[i].id);
}
return deferred.promise;
return this.subsonicRequest('createPlaylist.view', params);
},
songsRemoveSelected: function (songs) {
var deferred = $q.defer();
angular.forEach(songs, function (item, key) {

View file

@ -508,4 +508,22 @@ describe("Subsonic service -", function() {
expect(promise).toBeResolved();
});
it("Given an array of songs and a playlist id, when I save that playlist, an empty resolved promise will be returned", function() {
var url = 'http://demo.subsonic.com/rest/createPlaylist.view?'+
'c=Jamstash&callback=JSON_CALLBACK&f=jsonp&p=enc:cGFzc3dvcmQ%3D'+'&playlistId=7071'+
'&songId=2801&songId=1002&songId=6612'+
'&u=Hyzual&v=1.10.2';
mockBackend.expectJSONP(url).respond(JSON.stringify(response));
var songs = [
{ id: 2801 },
{ id: 1002 },
{ id: 6612 }
];
var promise = subsonic.savePlaylist(7071, songs);
mockBackend.flush();
expect(promise).toBeResolved();
});
});

View file

@ -441,13 +441,19 @@ angular.module('jamstash.subsonic.controller', ['jamstash.subsonic.service', 'ja
notifications.updateMessage('Please select a playlist to delete.');
}
};
$scope.savePlaylist = function () {
var id = $scope.selectedPlaylist;
subsonic.savePlaylist().then(function (data) {
$scope.getPlaylist(id, '');
notifications.updateMessage('Playlist Updated!', true);
});
if ($scope.selectedPlaylist !== null) {
var promise = subsonic.savePlaylist($scope.selectedPlaylist, $scope.song);
$scope.handleErrors(promise).then(function () {
$scope.getPlaylist('display', $scope.selectedPlaylist);
notifications.updateMessage('Playlist Updated!', true);
});
} else {
notifications.updateMessage('Please select a playlist to save.');
}
};
$scope.loadPlaylistsForMenu = function (data, event) {
$.ajax({
url: globals.BaseURL() + '/getPlaylists.view?' + globals.BaseParams(),

View file

@ -40,7 +40,8 @@ describe("Subsonic controller", function() {
"getRandomSongs",
"getPlaylist",
"newPlaylist",
"deletePlaylist"
"deletePlaylist",
"savePlaylist"
]);
// We make them return different promises and use our deferred variable only when testing
// a particular function, so that they stay isolated
@ -424,7 +425,7 @@ describe("Subsonic controller", function() {
expect(scope.getPlaylists).toHaveBeenCalled();
});
it("Given no selected playlist, when I try to delete that playlist, an error message will be notified", function() {
it("Given no selected playlist, when I try to delete a playlist, an error message will be notified", function() {
scope.selectedPlaylist = null;
scope.deletePlaylist();
@ -432,6 +433,38 @@ describe("Subsonic controller", function() {
expect(notifications.updateMessage).toHaveBeenCalledWith('Please select a playlist to delete.');
expect(subsonic.deletePlaylist).not.toHaveBeenCalled();
});
it("Given a selected playlist, when I save that playlist, the displayed songs will be sent to subsonic-service, the playlist will be displayed again and a notification message will be displayed", function() {
subsonic.savePlaylist.and.returnValue(deferred.promise);
spyOn(scope, 'getPlaylist');
scope.selectedPlaylist = 8469;
scope.song = [
{ id: 3352 },
{ id: 1518 },
{ id: 5179 }
];
scope.savePlaylist();
deferred.resolve();
scope.$apply();
expect(subsonic.savePlaylist).toHaveBeenCalledWith(8469, [
{ id: 3352 },
{ id: 1518 },
{ id: 5179 }
]);
expect(scope.getPlaylist).toHaveBeenCalledWith('display', 8469);
expect(notifications.updateMessage).toHaveBeenCalledWith('Playlist Updated!', true);
});
it("Given no selected playlist, when I try to save a playlist, an error message will be notified", function() {
scope.selectedPlaylist = null;
scope.savePlaylist();
expect(notifications.updateMessage).toHaveBeenCalledWith('Please select a playlist to save.');
expect(subsonic.savePlaylist).not.toHaveBeenCalled();
});
});
describe("On startup,", function() {