Fix tests

This commit is contained in:
Phan An 2017-04-25 22:09:32 +08:00
parent 15eb5a1596
commit 32430411f6
10 changed files with 274 additions and 452 deletions

View file

@ -62,21 +62,6 @@ export const albumStore = {
return this.cache[id]
},
/**
* Get the total length of an album by summing up its songs' duration.
* The length will also be converted into a H:i:s format and stored as fmtLength.
*
* @param {Object} album
*
* @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, 'fmtLength', secondsToHis(album.length))
return album.fmtLength
},
/**
* Add new album/albums into the current collection.
*

View file

@ -0,0 +1,223 @@
export default {
artists: [
{
id: 1,
name: 'Unknown Artist'
},
{
id: 2,
name: 'Various Artists'
},
{
id: 3,
name: 'All-4-One'
},
{
id: 4,
name: 'Boy Dylan'
},
{
id: 5,
name: 'James Blunt'
}
],
albums: [
{
id: 1193,
artist_id: 3,
name: 'All-4-One',
cover: '/public/img/covers/565c0f7067425.jpeg'
},
{
id: 1194,
artist_id: 3,
name: 'And The Music Speaks',
cover: '/public/img/covers/unknown-album.png'
},
{
id: 1195,
artist_id: 3,
name: 'Space Jam',
cover: '/public/img/covers/565c0f7115e0f.png'
},
{
id: 1217,
artist_id: 4,
name: 'Highway 61 Revisited',
cover: '/public/img/covers/565c0f76dc6e8.jpeg'
},
{
id: 1218,
artist_id: 4,
name: 'Pat Garrett & Billy the Kid',
cover: '/public/img/covers/unknown-album.png'
},
{
id: 1219,
artist_id: 4,
name: "The Times They Are A-Changin",
cover: '/public/img/covers/unknown-album.png'
},
{
id: 1268,
artist_id: 5,
name: 'Back To Bedlam',
cover: '/public/img/covers/unknown-album.png'
}
],
songs: [
{
id: '39189f4545f9d5671fb3dc964f0080a0',
album_id: 1193,
contributing_artist_id: 3,
title: 'I Swear',
length: 259.92,
playCount: 4
},
{
id: 'a6a550f7d950d2a2520f9bf1a60f025a',
album_id: 1194,
contributing_artist_id: 3,
title: 'I can love you like that',
length: 262.61,
playCount: 2
},
{
id: 'd86c30fd34f13c1aff8db59b7fc9c610',
album_id: 1195,
contributing_artist_id: 3,
title: 'I turn to you',
length: 293.04
},
{
id: 'e6d3977f3ffa147801ca5d1fdf6fa55e',
album_id: 1217,
contributing_artist_id: 4,
title: 'Like a rolling stone',
length: 373.63
},
{
id: 'aa16bbef6a9710eb9a0f41ecc534fad5',
album_id: 1218,
contributing_artist_id: 4,
title: "Knockin' on heaven's door",
length: 151.9
},
{
id: 'cb7edeac1f097143e65b1b2cde102482',
album_id: 1219,
contributing_artist_id: 4,
title: "The times they are a-changin'",
length: 196
},
{
id: '0ba9fb128427b32683b9eb9140912a70',
album_id: 1268,
contributing_artist_id: 5,
title: 'No bravery',
length: 243.12
},
{
id: '123fd1ad32240ecab28a4e86ed5173',
album_id: 1268,
contributing_artist_id: 5,
title: 'So long, Jimmy',
length: 265.04
},
{
id: '6a54c674d8b16732f26df73f59c63e21',
album_id: 1268,
contributing_artist_id: 5,
title: 'Wisemen',
length: 223.14
},
{
id: '6df7d82a9a8701e40d1c291cf14a16bc',
album_id: 1268,
contributing_artist_id: 5,
title: 'Goodbye my lover',
length: 258.61
},
{
id: '74a2000d343e4587273d3ad14e2fd741',
album_id: 1268,
contributing_artist_id: 5,
title: 'High',
length: 245.86
},
{
id: '7900ab518f51775fe6cf06092c074ee5',
album_id: 1268,
contributing_artist_id: 5,
title: "You're beautiful",
length: 213.29
},
{
id: '803910a51f9893347e087af851e38777',
album_id: 1268,
contributing_artist_id: 5,
title: 'Cry',
length: 246.91
},
{
id: 'd82b0d4d4803ebbcb61000a5b6a868f5',
album_id: 1268,
contributing_artist_id: 5,
title: 'Tears and rain',
length: 244.45
}
],
interactions: [
{
id: 1,
song_id: '7900ab518f51775fe6cf06092c074ee5',
liked: false,
play_count: 1
},
{
id: 2,
song_id: '95c0ffc33c08c8c14ea5de0a44d5df3c',
liked: false,
play_count: 2
},
{
id: 3,
song_id: 'c83b201502eb36f1084f207761fa195c',
liked: false,
play_count: 1
},
{
id: 4,
song_id: 'cb7edeac1f097143e65b1b2cde102482',
liked: true,
play_count: 3
},
{
id: 5,
song_id: 'ccc38cc14bb95aefdf6da4b34adcf548',
liked: false,
play_count: 4
}
],
currentUser: {
id: 1,
name: 'Phan An',
email: 'me@phanan.net',
is_admin: true
},
users: [
{
id: 1,
name: 'Phan An',
email: 'me@phanan.net',
is_admin: true
},
{
id: 2,
name: 'John Doe',
email: 'john@doe.tld',
is_admin: false
}
]
}

View file

@ -1,32 +0,0 @@
export default [
{
id: 1,
song_id: '7900ab518f51775fe6cf06092c074ee5',
liked: false,
play_count: 1
},
{
id: 2,
song_id: '95c0ffc33c08c8c14ea5de0a44d5df3c',
liked: false,
play_count: 2
},
{
id: 3,
song_id: 'c83b201502eb36f1084f207761fa195c',
liked: false,
play_count: 1
},
{
id: 4,
song_id: 'cb7edeac1f097143e65b1b2cde102482',
liked: true,
play_count: 3
},
{
id: 5,
song_id: 'ccc38cc14bb95aefdf6da4b34adcf548',
liked: false,
play_count: 4
}
]

View file

@ -1,214 +0,0 @@
export default [
{
id: 1,
name: 'All-4-One',
albums: [
{
id: 1193,
artist_id: 1,
name: 'All-4-One',
cover: '/public/img/covers/565c0f7067425.jpeg',
songs: [
{
id: '39189f4545f9d5671fb3dc964f0080a0',
album_id: 1193,
title: 'I Swear',
length: 259.92,
playCount: 4
}
]
},
{
id: 1194,
artist_id: 1,
name: 'And The Music Speaks',
cover: '/public/img/covers/unknown-album.png',
songs: [
{
id: 'a6a550f7d950d2a2520f9bf1a60f025a',
album_id: 1194,
title: 'I can love you like that',
length: 262.61,
playCount: 2
}
]
},
{
id: 1195,
artist_id: 1,
name: 'Space Jam',
cover: '/public/img/covers/565c0f7115e0f.png',
songs: [
{
id: 'd86c30fd34f13c1aff8db59b7fc9c610',
album_id: 1195,
title: 'I turn to you',
length: 293.04
}
]
}
]
},
{
id: 2,
name: 'Bob Dylan',
albums: [
{
id: 1217,
artist_id: 2,
name: 'Highway 61 Revisited',
cover: '/public/img/covers/565c0f76dc6e8.jpeg',
songs: [
{
id: 'e6d3977f3ffa147801ca5d1fdf6fa55e',
album_id: 1217,
title: 'Like a rolling stone',
length: 373.63
}
]
},
{
id: 1218,
artist_id: 2,
name: 'Pat Garrett & Billy the Kid',
cover: '/public/img/covers/unknown-album.png',
songs: [
{
id: 'aa16bbef6a9710eb9a0f41ecc534fad5',
album_id: 1218,
title: 'Knockin\' on heaven\'s door',
length: 151.9
}
]
},
{
id: 1219,
artist_id: 2,
name: 'The Times They Are A-Changin\'',
cover: '/public/img/covers/unknown-album.png',
songs: [
{
id: 'cb7edeac1f097143e65b1b2cde102482',
album_id: 1219,
title: 'The times they are a-changin\'',
length: 196
}
]
}
]
},
{
id: 3,
name: 'James Blunt',
albums: [
{
id: 1268,
artist_id: 3,
name: 'Back To Bedlam',
cover: '/public/img/covers/unknown-album.png',
songs: [
{
id: '0ba9fb128427b32683b9eb9140912a70',
album_id: 1268,
title: 'No bravery',
length: 243.12
},
{
id: '123fd1ad32240ecab28a4e86ed5173',
album_id: 1268,
title: 'So long, Jimmy',
length: 265.04
},
{
id: '6a54c674d8b16732f26df73f59c63e21',
album_id: 1268,
title: 'Wisemen',
length: 223.14
},
{
id: '6df7d82a9a8701e40d1c291cf14a16bc',
album_id: 1268,
title: 'Goodbye my lover',
length: 258.61
},
{
id: '74a2000d343e4587273d3ad14e2fd741',
album_id: 1268,
title: 'High',
length: 245.86
},
{
id: '7900ab518f51775fe6cf06092c074ee5',
album_id: 1268,
title: 'You\'re beautiful',
length: 213.29
},
{
id: '803910a51f9893347e087af851e38777',
album_id: 1268,
title: 'Cry',
length: 246.91
},
{
id: 'd82b0d4d4803ebbcb61000a5b6a868f5',
album_id: 1268,
title: 'Tears and rain',
length: 244.45
}
]
}
]
}
]
export const singleAlbum = {
id: 9999,
artist_id: 99,
name: 'Foo bar',
cover: '/foo.jpg',
songs: [
{
id: '39189f4545f0d5671fc3dc964f0080a0',
album_id: 9999,
title: 'A Foo Song',
length: 100,
playCount: 4
}, {
id: '39189f4545f9d5671fc3dc96cf1080a0',
album_id: 9999,
title: 'A Bar Song',
length: 200,
playCount: 7
}
]
}
export const singleArtist = {
id: 999,
name: 'John Cena',
albums: [
{
id: 9991,
artist_id: 999,
name: 'It\'s John Cena!!!!',
cover: '/tmp/john.jpg',
songs: [
{
id: 'e6d3977f3ffa147801ca5d1fdf6fa55f',
album_id: 9991,
title: 'John Cena to the Rescue',
length: 300
}
]
}
]
}
export const singleSong = {
id: 'dccb0d4d4803ebbcb61000a5b6a868f5',
album_id: 1193,
title: 'Foo and Bar',
length: 100,
playCount: 4,
lyrics: ''
}

View file

@ -1,23 +0,0 @@
export default {
currentUser: {
id: 1,
name: 'Phan An',
email: 'me@phanan.net',
is_admin: true
},
users: [
{
id: 1,
name: 'Phan An',
email: 'me@phanan.net',
is_admin: true
},
{
id: 2,
name: 'John Doe',
email: 'john@doe.tld',
is_admin: false
}
]
}

View file

@ -2,65 +2,47 @@ require('chai').should()
import { cloneDeep, last } from 'lodash'
import { albumStore, artistStore } from '../../stores'
import { default as artists, singleAlbum, singleSong } from '../blobs/media'
import data from '../blobs/data'
const { artists, albums } = data
describe('stores/album', () => {
beforeEach(() => albumStore.init(cloneDeep(artists)))
beforeEach(() => {
artistStore.init(cloneDeep(artists))
albumStore.init(cloneDeep(albums))
})
afterEach(() => albumStore.state.albums = [])
afterEach(() => {
artistStore.state.artists = []
albumStore.state.albums = []
})
describe('#init', () => {
it('correctly gathers albums', () => {
albumStore.state.albums.length.should.equal(7)
})
it('correctly sets albums length', () => {
albumStore.state.albums[0].length.should.equal(259.92)
})
it('correctly sets album artists', () => {
albumStore.state.albums[0].artist.id.should.equal(1)
albumStore.state.albums[0].artist.id.should.equal(3)
})
})
describe('#byId', () => {
it('correctly gets an album by ID', () => {
albumStore.byId(1193).name.should.equal('All-4-One')
})
})
describe('#compact', () => {
it('correctly compacts albums', () => {
albumStore.compact()
albumStore.state.albums.length.should.equal(0)
})
})
describe('#all', () => {
it('correctly returns all songs', () => {
it('correctly returns all albums', () => {
albumStore.all.length.should.equal(7)
})
})
describe('#getLength', () => {
it('correctly calculates an albums length', () => {
albumStore.getLength(albumStore.state.albums[6])
albumStore.state.albums[6].length.should.equal(1940.42); // I'm sorry…
})
})
describe('#add', () => {
beforeEach(() => {
albumStore.add(cloneDeep(singleAlbum))
})
it('correctly adds a new album into the state', () => {
last(albumStore.state.albums).id.should.equal(9999)
})
it('correctly recalculates the length', () => {
last(albumStore.state.albums).length.should.equal(300)
})
it('correctly recalculates the play count', () => {
last(albumStore.state.albums).playCount.should.equal(11)
})
})
describe('#remove', () => {
beforeEach(() => {
albumStore.remove(albumStore.state.albums[0]); // ID 1193
})
it('correctly removes an album', () => {
albumStore.state.albums.length.should.equal(6)
})
})
})

View file

@ -2,7 +2,8 @@ require('chai').should()
import { cloneDeep, last } from 'lodash'
import { artistStore } from '../../stores'
import { default as artists, singleAlbum, singleArtist } from '../blobs/media'
import data from '../blobs/data'
const artists = data.artists
describe('stores/artist', () => {
beforeEach(() => artistStore.init(cloneDeep(artists)))
@ -10,38 +11,22 @@ describe('stores/artist', () => {
describe('#init', () => {
it('correctly gathers artists', () => {
artistStore.state.artists.length.should.equal(3)
})
it('correctly gets artist images', () => {
artistStore.state.artists[0].image.should.equal('/public/img/covers/565c0f7067425.jpeg')
})
it('correctly counts songs by artists', () => {
artistStore.state.artists[0].songCount = 3
artistStore.state.artists.length.should.equal(5)
})
})
describe('#getImage', () => {
it('correctly gets an artists image', () => {
artistStore.getImage(artistStore.state.artists[0]).should.equal('/public/img/covers/565c0f7067425.jpeg')
describe('#byId', () => {
it('correctly gets an artist by ID', () => {
artistStore.byId(3).name.should.equal('All-4-One')
})
})
describe('#add', () => {
beforeEach(() => artistStore.add(cloneDeep(singleArtist)))
it('correctly adds an artist', () => {
last(artistStore.state.artists).name.should.equal('John Cena')
})
})
describe('#remove', () => {
beforeEach(() => artistStore.remove(artistStore.state.artists[0]))
it('correctly removes an artist', () => {
artistStore.state.artists.length.should.equal(2)
artistStore.state.artists[0].name.should.equal('Bob Dylan')
describe('#compact', () => {
it('correctly compact artists', () => {
artistStore.compact()
// because we've not processed songs/albums, all artists here have no songs
// and should be removed after compact()ing
artistStore.state.artists.length.should.equal(0)
})
})
})

View file

@ -1,9 +1,11 @@
require('chai').should()
import { queueStore } from '../../stores'
import artists from '../blobs/media'
import data from '../blobs/data'
const songs = artists[2].albums[0].songs
const { songs: allSongs } = data
// only get the songs by James Blunt
const songs = allSongs.filter(song => song.contributing_artist_id === 5)
describe('stores/queue', () => {
beforeEach(() => {
@ -32,7 +34,7 @@ describe('stores/queue', () => {
describe('#queue', () => {
beforeEach(() => queueStore.state.songs = songs)
const song = artists[0].albums[0].songs[0]
const song = allSongs[0]
it('correctly appends a song to end of the queue', () => {
queueStore.queue(song)

View file

@ -2,12 +2,15 @@ require('chai').should()
import { cloneDeep, last } from 'lodash'
import { songStore, albumStore, artistStore, preferenceStore } from '../../stores'
import artists from '../blobs/media'
import interactions from '../blobs/interactions'
import data from '../blobs/data'
const { songs, artists, albums, interactions } = data
describe('stores/song', () => {
beforeEach(() => {
artistStore.init(artists)
albumStore.init(albums)
songStore.init(songs)
})
describe('#init', () => {
@ -54,97 +57,6 @@ describe('stores/song', () => {
})
})
describe('#syncUpdatedSong', () => {
beforeEach(() => artistStore.init(artists))
const updatedSong = {
id: "39189f4545f9d5671fb3dc964f0080a0",
album_id: 1193,
title: "I Swear A Lot",
album: {
id: 1193,
arist_id: 1,
artist: {
id: 1,
name: 'All-4-One'
}
}
}
it ('correctly syncs an updated song with no album changes', () => {
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)
song.album_id = 1194
song.album = {
id: 1194,
artist_id: 1,
artist: {
id: 1,
name: 'All-4-One',
},
}
songStore.syncUpdatedSong(song)
songStore.byId(song.id).album.name.should.equal('And The Music Speaks')
})
it ('correctly syncs an updated song into a new album of same artist', () => {
const song = cloneDeep(updatedSong)
song.album_id = 1000
song.album = {
id: 1000,
artist_id: 1,
name: 'Brand New Album from All-4-One',
artist: {
id: 1,
name: 'All-4-One'
}
}
songStore.syncUpdatedSong(song)
// A new album should be created...
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)
song.album_id = 10000
song.album = {
id: 10000,
name: "It's... John Cena!!!",
artist_id: 10000,
artist: {
id: 10000,
name: 'John Cena'
}
}
songStore.syncUpdatedSong(song)
// A new artist should be created...
const lastArtist = last(artistStore.all)
lastArtist.name.should.equal('John Cena')
// A new album should be created
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)
// And the song belongs to the album.
songStore.byId(song.id).album.should.equal(lastAlbum)
})
})
describe('#addRecentlyPlayed', () => {
it('correctly adds a recently played song', () => {
songStore.addRecentlyPlayed(songStore.byId('cb7edeac1f097143e65b1b2cde102482'))

View file

@ -1,7 +1,9 @@
require('chai').should()
import { userStore } from '../../stores'
import data from '../blobs/users'
import data from '../blobs/data'
const { users } = data
describe('stores/user', () => {
beforeEach(() => userStore.init(data.users, data.currentUser))