Jamstash/app/subsonic/subsonic-service_test.js
Hyzual 834e67946c Adds back scrobbling and loading a track and the queue from localStorage.
- Splits loadTrackPosition into two functions : one for the track (which isn't finished), one for the queue.
- Adds main-controller.js' first unit tests for both these functions.
- Adds scrobbling functionnality. It is now a part of the Subsonic service, since we it's Subsonic that ultimately does the scroblling.
- Adds unit tests for both the service and the directive. The test for updatetime was crazy hard to do because I had to find a way to trigger my own fake event and it wasn't permitted by jplayer.
- Adds the load function to the player, it is used only when loading a song from localStorage.
- Removes ng-click from play/pause in the player template. jPlayer adds its own handlers on them, no need to do it twice.
2015-01-17 19:13:42 +01:00

174 lines
7.9 KiB
JavaScript

describe("Subsonic service -", function() {
'use strict';
var subsonic, mockBackend, mockGlobals, response;
beforeEach(function() {
// We redefine it because in some tests we need to alter the settings
mockGlobals = {
settings: {
AutoPlaylistSize: 3,
Protocol: 'jsonp'
},
BaseURL: function () {
return 'http://demo.subsonic.com/rest';
},
BaseParams: function () {
return 'u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=jsonp';
}
};
module('jamstash.subsonic.service', function ($provide) {
$provide.value('globals', mockGlobals);
});
inject(function (_subsonic_, $httpBackend) {
subsonic = _subsonic_;
mockBackend = $httpBackend;
});
response = {"subsonic-response": {status: "ok", version: "1.10.2"}};
});
afterEach(function() {
mockBackend.verifyNoOutstandingExpectation();
mockBackend.verifyNoOutstandingRequest();
});
it("scrobble - Given a song, when I scrobble it, it returns true if there was no error", function() {
var song = { id: 45872 };
var url = 'http://demo.subsonic.com/rest/scrobble.view?' +
'callback=JSON_CALLBACK&u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=jsonp&id=45872&submission=true';
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.scrobble(song);
mockBackend.flush();
expect(promise).toBeResolved();
});
describe("getStarred -", function() {
var url = 'http://demo.subsonic.com/rest/getStarred.view?'+
'callback=JSON_CALLBACK&u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=jsonp';
it("Given that I have 2 starred albums, 1 starred artist and 3 starred songs in my library, when getting everything starred, it returns them all", function() {
response["subsonic-response"].starred = {artist: [{id: 2245}], album: [{id: 1799},{id: 20987}], song: [{id: 2478},{id: 14726},{id: 742}]};
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.getStarred();
mockBackend.flush();
expect(promise).toBeResolvedWith({artist: [
{id: 2245}
], album: [
{id: 1799},{id: 20987}
], song: [
{id: 2478},{id: 14726},{id: 742}
]
});
});
it("Given that the global protocol setting is 'json' and given that I have 3 starred songs in my library, when getting everything starred, it uses GET and returns 3 starred songs", function() {
mockGlobals.settings.Protocol = 'json';
mockGlobals.BaseParams = function() { return 'u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=json'; };
var getUrl = 'http://demo.subsonic.com/rest/getStarred.view?' +
'u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=json';
response["subsonic-response"].starred = {song: [
{id: "2147"},{id:"9847"},{id:"214"}
]};
mockBackend.expectGET(getUrl).respond(200, JSON.stringify(response));
var promise = subsonic.getStarred();
mockBackend.flush();
expect(promise).toBeResolvedWith({song: [
{id: "2147"},{id:"9847"},{id:"214"}]
});
});
it("Given that there is absolutely nothing starred in my library, when getting everything starred, it returns an error object with a message", function() {
response["subsonic-response"].starred = {};
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.getStarred();
mockBackend.flush();
expect(promise).toBeRejectedWith({reason: 'Nothing is starred on the Subsonic server.'});
});
// TODO: Hyz: Those tests should be at a higher level, we are repeating them for everything...
it("Given that the Subsonic server is not responding, when getting everything starred, it returns an error object with a message", function() {
mockBackend.whenJSONP(url).respond(503, 'Service Unavailable');
var promise = subsonic.getStarred();
mockBackend.flush();
expect(promise).toBeRejectedWith({reason: 'Error when contacting the Subsonic server.', httpError: 503});
});
it("Given a missing parameter, when getting the starred songs, it returns an error object with a message", function() {
mockGlobals.BaseParams = function() { return 'u=Hyzual&v=1.10.2&c=Jamstash&f=jsonp';};
var missingPasswordUrl = 'http://demo.subsonic.com/rest/getStarred.view?'+
'callback=JSON_CALLBACK&u=Hyzual&v=1.10.2&c=Jamstash&f=jsonp';
var errorResponse = {"subsonic-response" : {
"status" : "failed",
"version" : "1.10.2",
"error" : {"code" : 10,"message" : "Required parameter is missing."}
}};
mockBackend.whenJSONP(missingPasswordUrl).respond(200, errorResponse);
var promise = subsonic.getStarred();
mockBackend.flush();
expect(promise).toBeRejectedWith({reason: 'Error when contacting the Subsonic server.', subsonicError: {code: 10, message:'Required parameter is missing.'}});
});
}); //end getStarred
describe("getRandomStarredSongs -", function() {
var url = 'http://demo.subsonic.com/rest/getStarred.view?'+
'callback=JSON_CALLBACK&u=Hyzual&p=enc:cGFzc3dvcmQ=&v=1.10.2&c=Jamstash&f=jsonp';
describe("Given that the global setting AutoPlaylist Size is 3", function() {
it("and given that I have more than 3 starred songs in my library, when getting random starred songs, the result should be limited to 3 starred songs", function() {
var library = [
{id: "11841"},{id: "12061"},{id: "17322"},{id: "1547"},{id: "14785"}
];
response["subsonic-response"].starred = {song: library};
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.getRandomStarredSongs();
// We create a spy in order to get the results of the promise
var success = jasmine.createSpy("success");
promise.then(success);
mockBackend.flush();
expect(promise).toBeResolved();
expect(success).toHaveBeenCalled();
var randomlyPickedSongs = success.calls.mostRecent().args[0];
for (var i = 0; i < randomlyPickedSongs.length; i++) {
expect(library).toContain(randomlyPickedSongs[i]);
}
});
it("and given that I have only 1 starred song in my library, when getting random starred songs, it returns my starred song", function() {
response["subsonic-response"].starred = {song: [{id: "11841"}]};
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.getRandomStarredSongs();
mockBackend.flush();
expect(promise).toBeResolvedWith([{id: "11841"}]);
});
it("and given that I don't have any starred song in my library, when getting random starred songs, it returns an error object with a message", function() {
response["subsonic-response"].starred = {song: []};
mockBackend.whenJSONP(url).respond(200, JSON.stringify(response));
var promise = subsonic.getRandomStarredSongs();
mockBackend.flush();
expect(promise).toBeRejectedWith({reason: 'No starred songs found on the Subsonic server.'});
});
});
});
});