Refactors subsonic.js' loadPlaylistsForMenu

It now uses submenu-service's getPlaylists() => DRY.
It's also unit-tested.
This commit is contained in:
Hyzual 2015-07-05 22:15:26 +02:00
parent 6e19277371
commit 7e0c5a176a
2 changed files with 53 additions and 30 deletions

View file

@ -181,7 +181,7 @@ angular.module('jamstash.subsonic.controller', [
}
}
};
$scope.$watch("SelectedAlbumSort.id", function (newValue, oldValue) {
$scope.$watch('SelectedAlbumSort.id', function (newValue, oldValue) {
if (newValue !== oldValue) {
if ($scope.song.length > 0) {
sortSubsonicSongs(newValue);
@ -197,7 +197,7 @@ angular.module('jamstash.subsonic.controller', [
}
});
$scope.$watch("SelectedMusicFolder", function (newValue, oldValue) {
$scope.$watch('SelectedMusicFolder', function (newValue, oldValue) {
if (newValue !== oldValue) {
var folderId;
if (newValue) {
@ -481,6 +481,7 @@ angular.module('jamstash.subsonic.controller', [
}
});
};
$scope.getAlbumByTag = function (id) { // Gets Album by ID3 tag
subsonic.getAlbumByTag(id).then(function (data) {
$scope.album = data.album;
@ -491,6 +492,7 @@ angular.module('jamstash.subsonic.controller', [
$scope.selectedPlaylist = data.selectedPlaylist;
});
};
$scope.search = function (query, type) {
if (query && query.length > 0) {
var promise = subsonic.search(query, type);
@ -514,6 +516,7 @@ angular.module('jamstash.subsonic.controller', [
});
}
};
$scope.toggleAZ = function () {
$scope.toggleSubmenu('#submenu_AZIndex', '#AZIndex', 'right', 44);
};
@ -585,36 +588,17 @@ angular.module('jamstash.subsonic.controller', [
}
};
$scope.loadPlaylistsForMenu = function (data, event) {
$.ajax({
url: globals.BaseURL() + '/getPlaylists.view?' + globals.BaseParams(),
method: 'GET',
dataType: globals.settings.Protocol,
timeout: globals.settings.Timeout,
success: function (data) {
var playlists = [];
$scope.playlistMenu = [];
if (typeof data["subsonic-response"].playlists.playlist != 'undefined') {
if (data["subsonic-response"].playlists.playlist.length > 0) {
playlists = data["subsonic-response"].playlists.playlist;
} else {
playlists[0] = data["subsonic-response"].playlists.playlist;
}
angular.forEach(playlists, function (item, key) {
if (item.owner == globals.settings.Username) {
$scope.playlistMenu.push(map.mapPlaylist(item));
}
});
if ($scope.playlistMenu.length > 0) {
$scope.$apply();
$scope.loadPlaylistsForMenu = function () {
var promise = subsonic.getPlaylists();
$scope.handleErrors(promise).then(function (data) {
$scope.playlistMenu = data.playlists.concat(data.playlistsPublic);
// TODO: Hyz: Refactor using some kind of directive ?
$scope.toggleSubmenu('#submenu_AddToPlaylist', '#action_AddToPlaylist', 'left', 124);
} else {
notifications.updateMessage('No Playlists :(', true);
}
}
}
}, function (error) {
notifications.updateMessage(error.reason, true);
});
};
$scope.addToPlaylist = function (id) {
var songs = [];
if ($scope.selectedSongs.length !== 0) {

View file

@ -727,6 +727,45 @@ describe("Subsonic controller", function () {
});
});
describe("loadPlaylistsForMenu() -", function () {
beforeEach(function () {
subsonic.getPlaylists.and.returnValue(deferred.promise);
// TODO: Hyz: Remove when refactored. It's from main-controller.js
scope.toggleSubmenu = jasmine.createSpy("toggleSubmenu");
});
it("Given that there were playlists in the library, when I load the playlists for the 'Add to playlist' menu, then the playlists will be retrieved from subsonic and published to the scope", function () {
scope.loadPlaylistsForMenu();
deferred.resolve({
playlists: [
{ id: 733 }
],
playlistsPublic: [
{ id: 962 }
]
});
scope.$apply();
expect(subsonic.getPlaylists).toHaveBeenCalled();
expect(scope.playlistMenu).toEqual([
{ id: 733 },
{ id: 962 }
]);
expect(scope.toggleSubmenu).toHaveBeenCalledWith('#submenu_AddToPlaylist', '#action_AddToPlaylist', 'left', 124);
});
it("Given that there weren't any playlists in the library, when I load the playlists for the 'Add to playlist' menu, then an error notification will be displayed", function () {
scope.loadPlaylistsForMenu();
deferred.reject({ reason: 'No playlist found on the Subsonic server.' });
scope.$apply();
expect(subsonic.getPlaylists).toHaveBeenCalled();
expect(scope.playlistMenu).toEqual([]);
expect(notifications.updateMessage).toHaveBeenCalledWith('No playlist found on the Subsonic server.', true);
expect(scope.toggleSubmenu).not.toHaveBeenCalled();
});
});
it("When I create a playlist, then it will ask for a name, use subsonic-service and reload the playlists", function () {
$window.prompt.and.returnValue('declassicize');
subsonic.newPlaylist.and.returnValue(deferred.promise);