Use explicit imports for lodash functions

This commit is contained in:
An Phan 2016-03-31 15:44:36 +08:00
parent b2d0bce09b
commit 481d012d6c
23 changed files with 162 additions and 125 deletions

View file

@ -21,7 +21,7 @@
<script>
import isMobile from 'ismobilejs';
import _ from 'lodash';
import { invoke } from 'lodash';
import $ from 'jquery';
import lyrics from './lyrics.vue';
@ -77,7 +77,7 @@
resetState() {
this.currentView = 'lyrics';
this.song = songStore.stub;
_.invoke(this.$refs, 'resetState');
invoke(this.$refs, 'resetState');
},
},

View file

@ -81,7 +81,7 @@
</template>
<script>
import _ from 'lodash';
import { sample } from 'lodash';
import playback from '../../../services/playback';
import songStore from '../../../stores/song';
@ -121,7 +121,7 @@
computed: {
greeting() {
return _.sample(this.greetings).replace('%s', userStore.current.name);
return sample(this.greetings).replace('%s', userStore.current.name);
},
},

View file

@ -48,7 +48,7 @@
</template>
<script>
import _ from 'lodash';
import { clone } from 'lodash';
import isMobile from 'ismobilejs';
import userStore from '../../../stores/user';
@ -81,7 +81,7 @@
*/
store() {
userStore.store(this.newUser.name, this.newUser.email, this.newUser.password, () => {
this.newUser = _.clone(userStore.stub);
this.newUser = clone(userStore.stub);
this.creating = false;
});
},

View file

@ -67,7 +67,7 @@
</template>
<script>
import _ from 'lodash';
import { every } from 'lodash';
import utils from '../../services/utils';
import artistStore from '../../stores/artist';
@ -131,7 +131,7 @@
* @return {boolean}
*/
bySameArtist() {
return _.every(this.songs, song => {
return every(this.songs, song => {
return song.album.artist.id === this.songs[0].album.artist.id;
});
},
@ -142,7 +142,7 @@
* @return {boolean}
*/
inSameAlbum() {
return _.every(this.songs, song => {
return every(this.songs, song => {
return song.album.id === this.songs[0].album.id;
});
},

View file

@ -25,7 +25,7 @@
</template>
<script>
import _ from 'lodash';
import { assign, last } from 'lodash';
import VueClickaway from 'vue-clickaway';
import songMenuMethods from '../../mixins/song-menu-methods';
@ -39,7 +39,7 @@
return {
newPlaylistName: '',
playlistState: playlistStore.state,
mergedSettings: _.assign({
mergedSettings: assign({
canQueue: true,
canLike: true,
}, this.settings),
@ -70,7 +70,7 @@
this.newPlaylistName = '';
// Activate the new playlist right away
this.$root.loadPlaylist(_.last(this.playlistState.playlists));
this.$root.loadPlaylist(last(this.playlistState.playlists));
});
this.close();

View file

@ -21,7 +21,7 @@
</template>
<script>
import _ from 'lodash';
import { pluck } from 'lodash';
import $ from 'jquery';
import playback from '../../services/playback';
@ -60,7 +60,7 @@
* Allow dragging the album (actually, its songs).
*/
dragStart(e) {
const songIds = _.pluck(this.album.songs, 'id');
const songIds = pluck(this.album.songs, 'id');
e.dataTransfer.setData('text/plain', songIds);
e.dataTransfer.effectAllowed = 'move';

View file

@ -19,7 +19,7 @@
</template>
<script>
import _ from 'lodash';
import { pluck } from 'lodash';
import $ from 'jquery';
import playback from '../../services/playback';
@ -49,7 +49,7 @@
* Allow dragging the artist (actually, their songs).
*/
dragStart(e) {
const songIds = _.pluck(artistStore.getSongsByArtist(this.artist), 'id');
const songIds = pluck(artistStore.getSongsByArtist(this.artist), 'id');
e.dataTransfer.setData('text/plain', songIds);
e.dataTransfer.effectAllowed = 'move';

View file

@ -63,7 +63,7 @@
</template>
<script>
import _ from 'lodash';
import { find, invoke, where, map, pluck } from 'lodash';
import isMobile from 'ismobilejs';
import $ from 'jquery';
@ -223,7 +223,7 @@
getComponentBySongId(id) {
// A Vue component can be removed (as a result of filter for example), so we check for its $el as well.
if (!this.componentCache[id] || !this.componentCache[id].$el) {
this.componentCache[id] = _.find(this.$refs.rows, { song: { id } });
this.componentCache[id] = find(this.$refs.rows, { song: { id } });
}
return this.componentCache[id];
@ -239,7 +239,7 @@
return;
}
_.invoke(this.$refs.rows, 'select');
invoke(this.$refs.rows, 'select');
this.gatherSelected();
},
@ -249,8 +249,8 @@
* @return {Array.<Object>} An array of Song objects
*/
gatherSelected() {
const selectedRows = _.where(this.$refs.rows, { selected: true });
const ids = _.map(selectedRows, row => row.song.id);
const selectedRows = where(this.$refs.rows, { selected: true });
const ids = map(selectedRows, row => row.song.id);
this.selectedSongs = songStore.byIds(ids);
},
@ -322,7 +322,7 @@
* Clear the current selection on this song list.
*/
clearSelection() {
_.invoke(this.$refs.rows, 'deselect');
invoke(this.$refs.rows, 'deselect');
this.gatherSelected();
},
@ -345,7 +345,7 @@
this.$nextTick(() => {
// We can opt for something like application/x-koel.text+plain here to sound fancy,
// but forget it.
const songIds = _.pluck(this.selectedSongs, 'id');
const songIds = pluck(this.selectedSongs, 'id');
e.dataTransfer.setData('text/plain', songIds);
e.dataTransfer.effectAllowed = 'move';

View file

@ -48,7 +48,7 @@
</template>
<script>
import _ from 'lodash';
import { clone, assign } from 'lodash';
import userStore from '../../stores/user';
@ -89,7 +89,7 @@
}
// Keep a cached version of the user for rolling back.
this.cached = _.clone(this.user);
this.cached = clone(this.user);
this.editing = true;
},
@ -98,7 +98,7 @@
*/
cancelEdit() {
// Restore the original user's properties
_.assign(this.user, this.cached);
assign(this.user, this.cached);
this.editing = false;
},

View file

@ -41,7 +41,7 @@
</template>
<script>
import _ from 'lodash';
import { pluck } from 'lodash';
import $ from 'jquery';
import rangeslider from 'rangeslider.js';
@ -209,7 +209,7 @@
* Save the current user's equalizer preferences into local storage.
*/
save() {
equalizerStore.set(this.preampGainValue, _.pluck(this.bands, 'filter.gain.value'));
equalizerStore.set(this.preampGainValue, pluck(this.bands, 'filter.gain.value'));
},
},

View file

@ -2,7 +2,7 @@
* Add necessary functionalities into a view that contains a song-list component.
*/
import _ from 'lodash';
import { assign } from 'lodash';
import playback from '../services/playback';
import addToMenu from '../components/shared/add-to-menu.vue';
@ -56,7 +56,7 @@ export default {
},
'songlist:changed': function (meta) {
this.meta = _.assign(this.meta, meta);
this.meta = assign(this.meta, meta);
},
},
};

View file

@ -1,4 +1,4 @@
import _ from 'lodash';
import { shuffle } from 'lodash';
import $ from 'jquery';
import queueStore from '../stores/queue';
@ -331,10 +331,10 @@ export default {
/**
* Queue up songs (replace them into the queue) and start playing right away.
*
* @param {?Array.<Object>} songs An array of song objects. Defaults to all songs if null.
* @param {Boolean=false} shuffle Whether to shuffle the songs before playing.
* @param {?Array.<Object>} songs An array of song objects. Defaults to all songs if null.
* @param {Boolean=false} shuffled Whether to shuffle the songs before playing.
*/
queueAndPlay(songs = null, shuffle = false) {
queueAndPlay(songs = null, shuffled = false) {
if (!songs) {
songs = songStore.all;
}
@ -343,8 +343,8 @@ export default {
return;
}
if (shuffle) {
songs = _.shuffle(songs);
if (shuffled) {
songs = shuffle(songs);
}
queueStore.queue(songs, true);

View file

@ -1,5 +1,14 @@
import Vue from 'vue';
import _ from 'lodash';
import {
reduce,
each,
find,
union,
difference,
take,
filter,
sortByOrder
} from 'lodash';
import utils from '../services/utils';
import stub from '../stubs/album';
@ -20,10 +29,10 @@ export default {
*/
init(artists) {
// Traverse through the artists array and add their albums into our master album list.
this.state.albums = _.reduce(artists, (albums, artist) => {
this.state.albums = reduce(artists, (albums, artist) => {
// While we're doing so, for each album, we get its length
// and keep a back reference to the artist too.
_.each(artist.albums, album => {
each(artist.albums, album => {
this.setupAlbum(album, artist);
});
@ -53,7 +62,7 @@ export default {
},
byId(id) {
return _.find(this.all, 'id', id);
return find(this.all, 'id', id);
},
/**
@ -65,7 +74,7 @@ export default {
* @return {String} The H:i:s format of the album length.
*/
getLength(album) {
Vue.set(album, 'length', _.reduce(album.songs, (length, song) => length + song.length, 0));
Vue.set(album, 'length', reduce(album.songs, (length, song) => length + song.length, 0));
Vue.set(album, 'fmtLength', utils.secondsToHis(album.length));
return album.fmtLength;
@ -78,7 +87,7 @@ export default {
*/
append(album) {
this.state.albums.push(this.setupAlbum(album, album.artist));
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0);
},
/**
@ -90,14 +99,14 @@ export default {
addSongsIntoAlbum(album, songs) {
songs = [].concat(songs);
album.songs = _.union(album.songs ? album.songs : [], songs);
album.songs = union(album.songs ? album.songs : [], songs);
songs.forEach(song => {
song.album_id = album.id;
song.album = album;
});
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0);
this.getLength(album);
},
@ -108,8 +117,8 @@ export default {
* @param {Array.<Object>|Object} songs
*/
removeSongsFromAlbum(album, songs) {
album.songs = _.difference(album.songs, [].concat(songs));
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
album.songs = difference(album.songs, [].concat(songs));
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0);
this.getLength(album);
},
@ -131,7 +140,7 @@ export default {
*/
remove(albums) {
albums = [].concat(albums);
this.state.albums = _.difference(this.state.albums, albums);
this.state.albums = difference(this.state.albums, albums);
// Remove from the artist as well
albums.forEach(album => {
@ -148,10 +157,10 @@ export default {
*/
getMostPlayed(n = 6) {
// Only non-unknown albums with actually play count are applicable.
const applicable = _.filter(this.state.albums, album => {
const applicable = filter(this.state.albums, album => {
return album.playCount && album.id !== 1;
});
return _.take(_.sortByOrder(applicable, 'playCount', 'desc'), n);
return take(sortByOrder(applicable, 'playCount', 'desc'), n);
},
};

View file

@ -1,5 +1,14 @@
import Vue from 'vue';
import _ from 'lodash';
import {
reduce,
each,
find,
union,
difference,
take,
filter,
sortByOrder
} from 'lodash';
import config from '../config';
import stub from '../stubs/artist';
@ -21,7 +30,7 @@ export default {
this.state.artists = artists;
// Traverse through artists array to get the cover and number of songs for each.
_.each(this.state.artists, artist => {
each(this.state.artists, artist => {
this.setupArtist(artist);
});
@ -31,7 +40,7 @@ export default {
setupArtist(artist) {
this.getImage(artist);
Vue.set(artist, 'playCount', 0);
Vue.set(artist, 'songCount', _.reduce(artist.albums, (count, album) => count + album.songs.length, 0));
Vue.set(artist, 'songCount', reduce(artist.albums, (count, album) => count + album.songs.length, 0));
Vue.set(artist, 'info', null);
return artist;
@ -42,7 +51,7 @@ export default {
},
byId(id) {
return _.find(this.all, 'id', id);
return find(this.all, 'id', id);
},
/**
@ -57,14 +66,14 @@ export default {
addAlbumsIntoArtist(artist, albums) {
albums = [].concat(albums);
artist.albums = _.union(artist.albums ? artist.albums : [], albums);
artist.albums = union(artist.albums ? artist.albums : [], albums);
albums.forEach(album => {
album.artist_id = artist.id;
album.artist = artist;
});
artist.playCount = _.reduce(artist.albums, (count, album) => count + album.playCount, 0);
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
},
/**
@ -74,8 +83,8 @@ export default {
* @param {Array.<Object>|Object} albums
*/
removeAlbumsFromArtist(artist, albums) {
artist.albums = _.difference(artist.albums, [].concat(albums));
artist.playCount = _.reduce(artist.albums, (count, album) => count + album.playCount, 0);
artist.albums = difference(artist.albums, [].concat(albums));
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
},
/**
@ -95,7 +104,7 @@ export default {
* @param {Array.<Object>|Object} artists
*/
remove(artists) {
this.state.artists = _.difference(this.state.artists, [].concat(artists));
this.state.artists = difference(this.state.artists, [].concat(artists));
},
/**
@ -107,7 +116,7 @@ export default {
*/
getSongsByArtist(artist) {
if (!artist.songs) {
artist.songs = _.reduce(artist.albums, (songs, album) => songs.concat(album.songs), []);
artist.songs = reduce(artist.albums, (songs, album) => songs.concat(album.songs), []);
}
return artist.songs;
@ -151,10 +160,10 @@ export default {
*/
getMostPlayed(n = 6) {
// Only non-unknown artists with actually play count are applicable.
const applicable = _.filter(this.state.artists, artist => {
const applicable = filter(this.state.artists, artist => {
return artist.playCount && artist.id !== 1;
});
return _.take(_.sortByOrder(applicable, 'playCount', 'desc'), n);
return take(sortByOrder(applicable, 'playCount', 'desc'), n);
},
};

View file

@ -1,4 +1,9 @@
import _ from 'lodash';
import {
each,
pluck,
difference,
union
} from 'lodash';
import http from '../services/http';
@ -62,7 +67,7 @@ export default {
* @param {Object} song
*/
remove(song) {
this.state.songs = _.difference(this.state.songs, [song]);
this.state.songs = difference(this.state.songs, [song]);
},
/**
@ -74,10 +79,10 @@ export default {
like(songs, cb = null) {
// Don't wait for the HTTP response to update the status, just set them to Liked right away.
// This may cause a minor problem if the request fails somehow, but do we care?
_.each(songs, song => song.liked = true);
this.state.songs = _.union(this.state.songs, songs);
each(songs, song => song.liked = true);
this.state.songs = union(this.state.songs, songs);
http.post('interaction/batch/like', { songs: _.pluck(songs, 'id') }, () => {
http.post('interaction/batch/like', { songs: pluck(songs, 'id') }, () => {
if (cb) {
cb();
}
@ -93,10 +98,10 @@ export default {
unlike(songs, cb = null) {
// Don't wait for the HTTP response to update the status, just set them to Unliked right away.
// This may cause a minor problem if the request fails somehow, but do we care?
_.each(songs, song => song.liked = false);
this.state.songs = _.difference(this.state.songs, songs);
each(songs, song => song.liked = false);
this.state.songs = difference(this.state.songs, songs);
http.post('interaction/batch/unlike', { songs: _.pluck(songs, 'id') }, () => {
http.post('interaction/batch/unlike', { songs: pluck(songs, 'id') }, () => {
if (cb) {
cb();
}

View file

@ -1,4 +1,9 @@
import _ from 'lodash';
import {
each,
pluck,
difference,
union
} from 'lodash';
import NProgress from 'nprogress';
import http from '../services/http';
@ -15,7 +20,7 @@ export default {
init(playlists) {
this.state.playlists = playlists;
_.each(this.state.playlists, this.getSongs);
each(this.state.playlists, this.getSongs);
},
/**
@ -46,7 +51,7 @@ export default {
store(name, songs, cb = null) {
if (songs.length) {
// Extract the IDs from the song objects.
songs = _.pluck(songs, 'id');
songs = pluck(songs, 'id');
}
NProgress.start();
@ -73,7 +78,7 @@ export default {
NProgress.start();
http.delete(`playlist/${playlist.id}`, {}, () => {
this.state.playlists = _.without(this.state.playlists, playlist);
this.state.playlists = without(this.state.playlists, playlist);
if (cb) {
cb();
@ -90,13 +95,13 @@ export default {
*/
addSongs(playlist, songs, cb = null) {
const count = playlist.songs.length;
playlist.songs = _.union(playlist.songs, songs);
playlist.songs = union(playlist.songs, songs);
if (count === playlist.songs.length) {
return;
}
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
http.put(`playlist/${playlist.id}/sync`, { songs: pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}
@ -111,9 +116,9 @@ export default {
* @param {?Function} cb
*/
removeSongs(playlist, songs, cb = null) {
playlist.songs = _.difference(playlist.songs, songs);
playlist.songs = difference(playlist.songs, songs);
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
http.put(`playlist/${playlist.id}/sync`, { songs: pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}

View file

@ -1,4 +1,4 @@
import _ from 'lodash';
import { extend, has } from 'lodash';
import userStore from './user';
import ls from '../services/ls';
@ -31,7 +31,7 @@ export default {
}
this.storeKey = `preferences_${user.id}`;
_.extend(this.state, ls.get(this.storeKey, this.state));
extend(this.state, ls.get(this.storeKey, this.state));
},
set(key, val) {
@ -40,7 +40,7 @@ export default {
},
get(key) {
return _.has(this.state, key) ? this.state[key] : null;
return has(this.state, key) ? this.state[key] : null;
},
save() {

View file

@ -1,4 +1,13 @@
import _ from 'lodash';
import {
first,
last,
includes,
union,
difference,
indexOf,
pluck,
shuffle
} from 'lodash';
export default {
state: {
@ -46,7 +55,7 @@ export default {
* @return {?Object}
*/
get first() {
return _.first(this.state.songs);
return first(this.state.songs);
},
/**
@ -55,7 +64,7 @@ export default {
* @return {?Object}
*/
get last() {
return _.last(this.state.songs);
return last(this.state.songs);
},
/**
@ -66,7 +75,7 @@ export default {
* @return {Boolean}
*/
contains(song) {
return _.includes(this.all, song);
return includes(this.all, song);
},
/**
@ -84,9 +93,9 @@ export default {
this.state.songs = songs;
} else {
if (toTop) {
this.state.songs = _.union(songs, this.state.songs);
this.state.songs = union(songs, this.state.songs);
} else {
this.state.songs = _.union(this.state.songs, songs);
this.state.songs = union(this.state.songs, songs);
}
}
},
@ -113,7 +122,7 @@ export default {
* @param {Object|String|Array.<Object>} songs The song(s) to unqueue
*/
unqueue(songs) {
this.state.songs = _.difference(this.state.songs, [].concat(songs));
this.state.songs = difference(this.state.songs, [].concat(songs));
},
/**
@ -153,7 +162,7 @@ export default {
* @return {?Integer}
*/
indexOf(song) {
return _.indexOf(this.state.songs, song);
return indexOf(this.state.songs, song);
},
/**
@ -163,10 +172,10 @@ export default {
*/
get next() {
if (!this.current) {
return _.first(this.state.songs);
return first(this.state.songs);
}
const idx = _.pluck(this.state.songs, 'id').indexOf(this.current.id) + 1;
const idx = pluck(this.state.songs, 'id').indexOf(this.current.id) + 1;
return idx >= this.state.songs.length ? null : this.state.songs[idx];
},
@ -178,10 +187,10 @@ export default {
*/
get previous() {
if (!this.current) {
return _.last(this.state.songs);
return last(this.state.songs);
}
const idx = _.pluck(this.state.songs, 'id').indexOf(this.current.id) - 1;
const idx = pluck(this.state.songs, 'id').indexOf(this.current.id) - 1;
return idx < 0 ? null : this.state.songs[idx];
},
@ -214,6 +223,6 @@ export default {
* @return {Array.<Object>} The shuffled array of song objects
*/
shuffle() {
return (this.state.songs = _.shuffle(this.state.songs));
return (this.state.songs = shuffle(this.state.songs));
},
};

View file

@ -1,5 +1,5 @@
import Vue from 'vue';
import _ from 'lodash';
import { without, pluck, take, remove, sortByOrder } from 'lodash';
import http from '../services/http';
import utils from '../services/utils';
@ -162,7 +162,7 @@ export default {
*/
addRecent(song) {
// First we make sure that there's no duplicate.
this.state.recent = _.without(this.state.recent, song);
this.state.recent = without(this.state.recent, song);
// Then we prepend the song into the list.
this.state.recent.unshift(song);
@ -259,7 +259,7 @@ export default {
http.put('songs', {
data,
songs: _.pluck(songs, 'id'),
songs: pluck(songs, 'id'),
}, response => {
response.data.forEach(song => {
this.syncUpdatedSong(song);
@ -384,7 +384,7 @@ export default {
* @return {Array.<Object>}
*/
getRecent(n = 10) {
return _.take(this.state.recent, n);
return take(this.state.recent, n);
},
/**
@ -395,10 +395,10 @@ export default {
* @return {Array.<Object>}
*/
getMostPlayed(n = 10) {
const songs = _.take(_.sortByOrder(this.state.songs, 'playCount', 'desc'), n);
const songs = take(sortByOrder(this.state.songs, 'playCount', 'desc'), n);
// Remove those with playCount=0
_.remove(songs, song => !song.playCount);
remove(songs, song => !song.playCount);
return songs;
},

View file

@ -1,4 +1,4 @@
import _ from 'lodash';
import { each, find, without } from 'lodash';
import md5 from 'blueimp-md5';
import Vue from 'vue';
import NProgress from 'nprogress';
@ -25,7 +25,7 @@ export default {
this.state.current = currentUser;
// Set the avatar for each of the users…
_.each(this.state.users, this.setAvatar);
each(this.state.users, this.setAvatar);
// …and the current user as well.
this.setAvatar();
@ -48,7 +48,7 @@ export default {
* @return {Object}
*/
byId(id) {
return _.find(this.state.users, {id});
return find(this.state.users, {id});
},
/**
@ -195,7 +195,7 @@ export default {
NProgress.start();
http.delete(`user/${user.id}`, {}, () => {
this.state.users = _.without(this.state.users, user);
this.state.users = without(this.state.users, user);
// Mama, just killed a man
// Put a gun against his head

View file

@ -1,12 +1,12 @@
require('chai').should();
import _ from 'lodash';
import { cloneDeep, last } from 'lodash';
import albumStore from '../../stores/album';
import artistStore from '../../stores/artist';
import { default as artists, singleAlbum, singleSong } from '../blobs/media';
describe('stores/album', () => {
beforeEach(() => albumStore.init(_.cloneDeep(artists)));
beforeEach(() => albumStore.init(cloneDeep(artists)));
afterEach(() => albumStore.state.albums = []);
@ -39,19 +39,19 @@ describe('stores/album', () => {
describe('#append', () => {
beforeEach(() => {
albumStore.append(_.cloneDeep(singleAlbum));
albumStore.append(cloneDeep(singleAlbum));
});
it('correctly appends a new album into the state', () => {
_.last(albumStore.state.albums).id.should.equal(9999);
last(albumStore.state.albums).id.should.equal(9999);
});
it('correctly recalculates the length', () => {
_.last(albumStore.state.albums).length.should.equal(300);
last(albumStore.state.albums).length.should.equal(300);
});
it('correctly recalculates the play count', () => {
_.last(albumStore.state.albums).playCount.should.equal(11);
last(albumStore.state.albums).playCount.should.equal(11);
});
});
@ -67,7 +67,7 @@ describe('stores/album', () => {
describe('#addSongsIntoAlbum', () => {
beforeEach(() => {
albumStore.addSongsIntoAlbum(albumStore.state.albums[0], _.cloneDeep(singleSong));
albumStore.addSongsIntoAlbum(albumStore.state.albums[0], cloneDeep(singleSong));
});
it('correctly adds a song into an album', () => {

View file

@ -1,11 +1,11 @@
require('chai').should();
import _ from 'lodash';
import { cloneDeep, last } from 'lodash';
import artistStore from '../../stores/artist';
import { default as artists, singleAlbum, singleArtist } from '../blobs/media';
describe('stores/artist', () => {
beforeEach(() => artistStore.init(_.cloneDeep(artists)));
beforeEach(() => artistStore.init(cloneDeep(artists)));
afterEach(() => artistStore.state.artists = []);
describe('#init', () => {
@ -35,10 +35,10 @@ describe('stores/artist', () => {
});
describe('#append', () => {
beforeEach(() => artistStore.append(_.cloneDeep(singleArtist)));
beforeEach(() => artistStore.append(cloneDeep(singleArtist)));
it('correctly appends an artist', () => {
_.last(artistStore.state.artists).name.should.equal('John Cena');
last(artistStore.state.artists).name.should.equal('John Cena');
});
});
@ -53,7 +53,7 @@ describe('stores/artist', () => {
describe('#addAlbumsIntoArtist', () => {
beforeEach(() => {
artistStore.addAlbumsIntoArtist(artistStore.state.artists[0], _.cloneDeep(singleAlbum));
artistStore.addAlbumsIntoArtist(artistStore.state.artists[0], cloneDeep(singleAlbum));
});
it('correctly adds albums into an artist', () => {
@ -61,7 +61,7 @@ describe('stores/artist', () => {
});
it('correctly sets the album artist', () => {
const addedAlbum = _.last(artistStore.state.artists[0].albums);
const addedAlbum = last(artistStore.state.artists[0].albums);
addedAlbum.artist.should.equal(artistStore.state.artists[0]);
addedAlbum.artist_id.should.equal(artistStore.state.artists[0].id);
});

View file

@ -1,5 +1,5 @@
require('chai').should();
import _ from 'lodash';
import { cloneDeep, last } from 'lodash';
import songStore from '../../stores/song';
import albumStore from '../../stores/album';
@ -74,12 +74,12 @@ describe('stores/song', () => {
};
it ('correctly syncs an updated song with no album changes', () => {
songStore.syncUpdatedSong(_.cloneDeep(updatedSong));
songStore.syncUpdatedSong(cloneDeep(updatedSong));
songStore.byId(updatedSong.id).title.should.equal('I Swear A Lot');
});
it ('correctly syncs an updated song into an existing album of same artist', () => {
const song = _.cloneDeep(updatedSong);
const song = cloneDeep(updatedSong);
song.album_id = 1194;
song.album = {
id: 1194,
@ -95,7 +95,7 @@ describe('stores/song', () => {
});
it ('correctly syncs an updated song into a new album of same artist', () => {
const song = _.cloneDeep(updatedSong);
const song = cloneDeep(updatedSong);
song.album_id = 9999;
song.album = {
id: 9999,
@ -110,14 +110,14 @@ describe('stores/song', () => {
songStore.syncUpdatedSong(song);
// A new album should be created...
_.last(albumStore.all).name.should.equal('Brand New Album from All-4-One');
last(albumStore.all).name.should.equal('Brand New Album from All-4-One');
// ...and assigned with the song.
songStore.byId(song.id).album.name.should.equal('Brand New Album from All-4-One');
});
it ('correctly syncs an updated song into a new album of a new artist', () => {
const song = _.cloneDeep(updatedSong);
const song = cloneDeep(updatedSong);
song.album_id = 10000;
song.album = {
id: 10000,
@ -132,15 +132,15 @@ describe('stores/song', () => {
songStore.syncUpdatedSong(song);
// A new artist should be created...
const lastArtist = _.last(artistStore.all);
const lastArtist = last(artistStore.all);
lastArtist.name.should.equal('John Cena');
// A new album should be created
const lastAlbum = _.last(albumStore.all);
const lastAlbum = last(albumStore.all);
lastAlbum.name.should.equal("It's... John Cena!!!");
// The album must belong to John Cena of course!
_.last(lastArtist.albums).should.equal(lastAlbum);
last(lastArtist.albums).should.equal(lastAlbum);
// And the song belongs to the album.
songStore.byId(song.id).album.should.equal(lastAlbum);