Refactor queue.js
- Added angular-ui's ui-sortable directive - Removed our custom drag & drop handlers, they are replaced by ui-sortable. - Refactored queue.js to better comply with https://github.com/johnpapa/angular-styleguide#controllers. Having the exposed
This commit is contained in:
parent
5d385149f3
commit
9f24576d04
9 changed files with 230 additions and 169 deletions
|
@ -2,7 +2,7 @@
|
|||
describe("Queue controller", function () {
|
||||
'use strict';
|
||||
|
||||
var player, scope, SelectedSongs;
|
||||
var QueueContoller, $scope, player, SelectedSongs;
|
||||
var song;
|
||||
|
||||
beforeEach(function () {
|
||||
|
@ -10,133 +10,74 @@ describe("Queue controller", function () {
|
|||
|
||||
SelectedSongs = jasmine.createSpyObj("SelectedSongs", ["get"]);
|
||||
|
||||
inject(function ($controller, $rootScope, globals, _player_) {
|
||||
scope = $rootScope.$new();
|
||||
player = _player_;
|
||||
player = jasmine.createSpyObj("player", [
|
||||
"emptyQueue",
|
||||
"getPlayingSong",
|
||||
"removeSongs",
|
||||
"reorderQueue",
|
||||
"shuffleQueue"
|
||||
]);
|
||||
|
||||
$controller('QueueController', {
|
||||
$scope: scope,
|
||||
globals: globals,
|
||||
player: player,
|
||||
inject(function ($controller, $rootScope) {
|
||||
$scope = $rootScope.$new();
|
||||
|
||||
QueueContoller = $controller('QueueController', {
|
||||
$scope : $scope,
|
||||
player : player,
|
||||
SelectedSongs: SelectedSongs
|
||||
});
|
||||
});
|
||||
song = { id: 7310 };
|
||||
player.queue = [];
|
||||
});
|
||||
|
||||
it("When I play a song, it calls play in the player service", function () {
|
||||
spyOn(player, "play");
|
||||
scope.playSong(song);
|
||||
expect(player.play).toHaveBeenCalledWith(song);
|
||||
});
|
||||
|
||||
it("When I empty the queue, it calls emptyQueue in the player service and closes fancybox", function () {
|
||||
spyOn(player, "emptyQueue");
|
||||
it("emptyQueue() - When I empty the queue, then the player service's emptyQueuewill be called and fancybox will be closed", function () {
|
||||
spyOn($.fancybox, "close");
|
||||
|
||||
scope.emptyQueue();
|
||||
QueueContoller.emptyQueue();
|
||||
|
||||
expect(player.emptyQueue).toHaveBeenCalled();
|
||||
expect($.fancybox.close).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("When I shuffle the queue, then the player's shuffleQueue will be called and the queue will be scrolled back to the first element", function () {
|
||||
spyOn(player, "shuffleQueue");
|
||||
it("shuffleQueue() - When I shuffle the queue, then the player's shuffleQueue will be called and the queue will be scrolled back to the first element", function () {
|
||||
spyOn($.fn, 'scrollTo');
|
||||
|
||||
scope.shuffleQueue();
|
||||
QueueContoller.shuffleQueue();
|
||||
|
||||
expect(player.shuffleQueue).toHaveBeenCalled();
|
||||
expect($.fn.scrollTo).toHaveBeenCalledWith('.header', jasmine.any(Number));
|
||||
});
|
||||
|
||||
it("When I add one song to the queue, it calls addSong in the player service", function () {
|
||||
spyOn(player, "addSong");
|
||||
scope.addSongToQueue(song);
|
||||
expect(player.addSong).toHaveBeenCalledWith(song);
|
||||
it("removeSelectedSongsFromQueue() - When I remove all the selected songs from the queue, then the player's removeSongs will be called with the selected songs", function () {
|
||||
SelectedSongs.get.and.returnValue([
|
||||
{ id: 6390 },
|
||||
{ id: 2973 }
|
||||
]);
|
||||
|
||||
QueueContoller.removeSelectedSongsFromQueue();
|
||||
|
||||
expect(player.removeSongs).toHaveBeenCalledWith([
|
||||
{ id: 6390 },
|
||||
{ id: 2973 }
|
||||
]);
|
||||
});
|
||||
|
||||
it("When I remove a song from the queue, it calls removeSong in the player service", function () {
|
||||
spyOn(player, "removeSong");
|
||||
scope.removeSongFromQueue(song);
|
||||
expect(player.removeSong).toHaveBeenCalledWith(song);
|
||||
});
|
||||
it("isPlayingSong() - Given a song, when I want to know if it's playing, then the player service will be called and its return will be compared with the given song", function () {
|
||||
song = { id: 2537 };
|
||||
player.getPlayingSong.and.returnValue(song);
|
||||
|
||||
it("When I remove all the selected songs from the queue, it calls removeSongs in the player service", function () {
|
||||
spyOn(player, "removeSongs");
|
||||
var secondSong = { id: 6791 };
|
||||
SelectedSongs.get.and.returnValue([song, secondSong]);
|
||||
scope.removeSelectedSongsFromQueue();
|
||||
expect(player.removeSongs).toHaveBeenCalledWith([song, secondSong]);
|
||||
});
|
||||
expect(QueueContoller.isPlayingSong(song)).toBeTruthy();
|
||||
|
||||
it("asks the player service if a given song is the currently playing song", function () {
|
||||
spyOn(player, "getPlayingSong").and.returnValue(song);
|
||||
expect(scope.isPlayingSong(song)).toBeTruthy();
|
||||
expect(player.getPlayingSong).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("when the player service's current song changes, it scrolls the queue to display it", function () {
|
||||
spyOn(player, "getPlayingSong").and.callFake(function () {
|
||||
return song;
|
||||
});
|
||||
it("When the player service's current song changes, then the queue will be scrolled down to display the new playing song", function () {
|
||||
song = { id: 5239 };
|
||||
player.getPlayingSong.and.returnValue(song);
|
||||
spyOn($.fn, "scrollTo");
|
||||
|
||||
scope.$apply();
|
||||
$scope.$apply();
|
||||
|
||||
expect($.fn.scrollTo).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("reorders the queue by drag and drop - ", function () {
|
||||
var mockUI;
|
||||
beforeEach(function () {
|
||||
player.queue = [
|
||||
{ id: 2246 },
|
||||
{ id: 8869 },
|
||||
{ id: 285 }
|
||||
];
|
||||
mockUI = {
|
||||
item: {}
|
||||
};
|
||||
});
|
||||
|
||||
it("given a song in the queue, when I start dragging it, it records what its starting position in the queue was", function () {
|
||||
mockUI.item.index = jasmine.createSpy("index").and.returnValue('1');
|
||||
mockUI.item.data = jasmine.createSpy("data");
|
||||
|
||||
scope.dragStart({}, mockUI);
|
||||
|
||||
expect(mockUI.item.index).toHaveBeenCalled();
|
||||
expect(mockUI.item.data).toHaveBeenCalledWith('start', '1');
|
||||
});
|
||||
|
||||
it("given a song in the queue that I started dragging, when I drop it, its position in the queue has changed", function () {
|
||||
mockUI.item.index = jasmine.createSpy("index").and.returnValue('0');
|
||||
mockUI.item.data = jasmine.createSpy("data").and.returnValue('1');
|
||||
|
||||
scope.dragEnd({}, mockUI);
|
||||
|
||||
expect(mockUI.item.index).toHaveBeenCalled();
|
||||
expect(mockUI.item.data).toHaveBeenCalledWith('start');
|
||||
// The second song should now be first
|
||||
expect(player.queue).toEqual([
|
||||
{ id: 8869 },
|
||||
{ id: 2246 },
|
||||
{ id: 285 }
|
||||
]);
|
||||
});
|
||||
|
||||
// TODO: Hyz: Maybe it should be an end-to-end test
|
||||
it("given that the player is playing the second song (B), when I swap the first (A) and the second song (B), the player's next song should be A", function () {
|
||||
player.play({ id: 8869 });
|
||||
mockUI.item.index = jasmine.createSpy("index").and.returnValue('0');
|
||||
mockUI.item.data = jasmine.createSpy("data").and.returnValue('1');
|
||||
|
||||
scope.dragEnd({}, mockUI);
|
||||
|
||||
player.nextTrack();
|
||||
expect(player._playingIndex).toBe(1);
|
||||
expect(player._playingSong).toEqual({ id: 2246 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue