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

So we can move closer to consistent error-reporting and protocol usage.
- refactors subonic-controller's getPodcasts().
- Adds unit-tests.
This commit is contained in:
Hyzual 2015-03-04 01:44:14 +01:00
parent d5c6b45dba
commit f4c37bc8dd
4 changed files with 92 additions and 23 deletions

View file

@ -504,29 +504,20 @@ angular.module('jamstash.subsonic.service', ['jamstash.settings', 'jamstash.util
deferred.resolve(genres); deferred.resolve(genres);
return deferred.promise; return deferred.promise;
}, },
getPodcasts: function (refresh) {
var deferred = $q.defer(); getPodcasts: function () {
if (globals.settings.Debug) { console.log("LOAD PODCASTS"); } var exception = {reason: 'No podcast found on the Subsonic server.'};
$.ajax({ var promise = this.subsonicRequest('getPodcasts.view')
url: globals.BaseURL() + '/getPodcasts.view?' + globals.BaseParams(), .then(function (subsonicResponse) {
method: 'GET', if (subsonicResponse.podcasts !== undefined && subsonicResponse.podcasts.channel !== undefined && subsonicResponse.podcasts.channel.length > 0) {
dataType: globals.settings.Protocol, return subsonicResponse.podcasts.channel;
timeout: globals.settings.Timeout, } else {
success: function (data) { return $q.reject(exception);
if (data["subsonic-response"].podcasts.channel !== undefined) {
var items = [];
if (data["subsonic-response"].podcasts.channel.length > 0) {
items = data["subsonic-response"].podcasts.channel;
} else {
items[0] = data["subsonic-response"].podcasts.channel;
}
podcasts = items;
}
deferred.resolve(podcasts);
} }
}); });
return deferred.promise; return promise;
}, },
getPodcast: function (id, action) { getPodcast: function (id, action) {
var deferred = $q.defer(); var deferred = $q.defer();
content.selectedPodcast = id; content.selectedPodcast = id;

View file

@ -526,4 +526,41 @@ describe("Subsonic service -", function() {
expect(promise).toBeResolved(); expect(promise).toBeResolved();
}); });
describe("When I load the podcasts,", function() {
var url;
beforeEach(function() {
url = url = 'http://demo.subsonic.com/rest/getPodcasts.view?'+
'c=Jamstash&callback=JSON_CALLBACK&f=jsonp&p=enc:cGFzc3dvcmQ%3D&u=Hyzual&v=1.10.2';
});
it("given that there were podcasts in the library, then a promise will be resolved with an array of podcasts", function() {
response["subsonic-response"].podcasts = {
channel: [
{ id: 7820 },
{ id: 5174 },
{ id: 2404 }
]
};
mockBackend.expectJSONP(url).respond(JSON.stringify(response));
var promise = subsonic.getPodcasts();
mockBackend.flush();
expect(promise).toBeResolvedWith([
{ id: 7820 },
{ id: 5174 },
{ id: 2404 }
]);
});
it("given that there weren't any podcast in the library, then a rejected promise with an error message will be returned", function() {
response["subsonic-response"].podcasts = {};
mockBackend.expectJSONP(url).respond(JSON.stringify(response));
var promise = subsonic.getPodcasts();
mockBackend.flush();
expect(promise).toBeRejectedWith({reason: 'No podcast found on the Subsonic server.'});
});
});
}); });

View file

@ -402,6 +402,7 @@ angular.module('jamstash.subsonic.controller', ['jamstash.subsonic.service', 'ja
$scope.playlistsPublic = data.playlistsPublic; $scope.playlistsPublic = data.playlistsPublic;
}, function () { }, function () {
// Do not display a notification, there simply are no playlists. // Do not display a notification, there simply are no playlists.
// Otherwise, a notification will be displayed at every page reload.
$scope.playlists = []; $scope.playlists = [];
$scope.playlistsPublic = []; $scope.playlistsPublic = [];
}); });
@ -513,11 +514,18 @@ angular.module('jamstash.subsonic.controller', ['jamstash.subsonic.service', 'ja
$scope.Genres = data; $scope.Genres = data;
}); });
}; };
$scope.getPodcasts = function (refresh) {
subsonic.getPodcasts(refresh).then(function (data) { $scope.getPodcasts = function () {
$scope.podcasts = data; var promise = subsonic.getPodcasts();
$scope.handleErrors(promise).then(function (podcasts) {
$scope.podcasts = podcasts;
}, function () {
// Do not display a notification, there simply are no podcasts.
// Otherwise, a notification will be displayed at every page reload.
$scope.podcasts = [];
}); });
}; };
$scope.getPodcast = function (id, action) { $scope.getPodcast = function (id, action) {
subsonic.getPodcast(id, action).then(function (data) { subsonic.getPodcast(id, action).then(function (data) {
$scope.album = data.album; $scope.album = data.album;

View file

@ -465,6 +465,39 @@ describe("Subsonic controller", function() {
expect(notifications.updateMessage).toHaveBeenCalledWith('Please select a playlist to save.'); expect(notifications.updateMessage).toHaveBeenCalledWith('Please select a playlist to save.');
expect(subsonic.savePlaylist).not.toHaveBeenCalled(); expect(subsonic.savePlaylist).not.toHaveBeenCalled();
}); });
describe("When I load the podcasts,", function() {
beforeEach(function() {
subsonic.getPodcasts.and.returnValue(deferred.promise);
});
it("Given that there were podcasts in the library, then the podcasts will be published to the scope", function() {
scope.getPodcasts();
deferred.resolve([
{id: 9775},
{id: 5880},
{id: 5554}
]);
scope.$apply();
expect(subsonic.getPodcasts).toHaveBeenCalled();
expect(scope.podcasts).toEqual([
{id: 9775},
{id: 5880},
{id: 5554}
]);
});
it("Given that there weren't any podcast in the library, then an empty array will be published to the scope and the user won't be notified with an error message", function() {
scope.getPodcasts();
deferred.reject({reason: 'No podcast found on the Subsonic server.'});
scope.$apply();
expect(subsonic.getPodcasts).toHaveBeenCalled();
expect(scope.podcasts).toEqual([]);
expect(notifications.updateMessage).not.toHaveBeenCalled();
});
});
}); });
describe("On startup,", function() { describe("On startup,", function() {