mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
feat(test): add store tests
This commit is contained in:
parent
2bdd2ff021
commit
6a2e977138
16 changed files with 426 additions and 423 deletions
|
@ -1,8 +0,0 @@
|
|||
export default {
|
||||
get: jest.fn((): Promise<void> => Promise.resolve()),
|
||||
post: jest.fn((): Promise<void> => Promise.resolve()),
|
||||
patch: jest.fn((): Promise<void> => Promise.resolve()),
|
||||
put: jest.fn((): Promise<void> => Promise.resolve()),
|
||||
delete: jest.fn((): Promise<void> => Promise.resolve()),
|
||||
request: jest.fn(() => Promise.resolve({ data: [] }))
|
||||
}
|
|
@ -7,175 +7,209 @@ const currentUser = factory<User>('user', {
|
|||
is_admin: true
|
||||
})
|
||||
|
||||
const unknownArtist = factory<Artist>('artist', { id: 1, name: 'Unknown Artist' })
|
||||
const variousArtist = factory<Artist>('artist', { id: 2, name: 'Various Artist' })
|
||||
const all4One = factory<Artist>('artist', { id: 3, name: 'All-4-One' })
|
||||
const bobDylan = factory<Artist>('artist', { id: 4, name: 'Bob Dylan' })
|
||||
const jamesBlunt = factory<Artist>('artist', { id: 5, name: 'James Blunt' })
|
||||
|
||||
const all4OneAlbum = factory<Album>('album', {
|
||||
artist: all4One,
|
||||
id: 1193,
|
||||
artist_id: 3,
|
||||
name: 'All-4-One',
|
||||
cover: '/img/covers/565c0f7067425.jpeg'
|
||||
})
|
||||
|
||||
const musicSpeaks = factory<Album>('album', {
|
||||
artist: all4One,
|
||||
id: 1194,
|
||||
artist_id: 3,
|
||||
name: 'And The Music Speaks',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
})
|
||||
|
||||
const spaceJam = factory<Album>('album', {
|
||||
artist: all4One,
|
||||
id: 1195,
|
||||
artist_id: 3,
|
||||
name: 'Space Jam',
|
||||
cover: '/img/covers/565c0f7115e0f.png'
|
||||
})
|
||||
|
||||
const highway = factory<Album>('album', {
|
||||
artist: bobDylan,
|
||||
id: 1217,
|
||||
artist_id: 4,
|
||||
name: 'Highway 61 Revisited',
|
||||
cover: '/img/covers/565c0f76dc6e8.jpeg'
|
||||
})
|
||||
|
||||
const patGarrett = factory<Album>('album', {
|
||||
artist: bobDylan,
|
||||
id: 1218,
|
||||
artist_id: 4,
|
||||
name: 'Pat Garrett & Billy the Kid',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
})
|
||||
|
||||
const theTimes = factory<Album>('album', {
|
||||
artist: bobDylan,
|
||||
id: 1219,
|
||||
artist_id: 4,
|
||||
name: "The Times They Are A-Changin",
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
})
|
||||
|
||||
const backToBedlam = factory<Album>('album', {
|
||||
artist: jamesBlunt,
|
||||
id: 1268,
|
||||
artist_id: 5,
|
||||
name: 'Back To Bedlam',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
})
|
||||
|
||||
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'
|
||||
}
|
||||
],
|
||||
artists: [unknownArtist, variousArtist, all4One, bobDylan, jamesBlunt],
|
||||
albums: [
|
||||
{
|
||||
id: 1193,
|
||||
artist_id: 3,
|
||||
name: 'All-4-One',
|
||||
cover: '/img/covers/565c0f7067425.jpeg'
|
||||
},
|
||||
{
|
||||
id: 1194,
|
||||
artist_id: 3,
|
||||
name: 'And The Music Speaks',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
},
|
||||
{
|
||||
id: 1195,
|
||||
artist_id: 3,
|
||||
name: 'Space Jam',
|
||||
cover: '/img/covers/565c0f7115e0f.png'
|
||||
},
|
||||
{
|
||||
id: 1217,
|
||||
artist_id: 4,
|
||||
name: 'Highway 61 Revisited',
|
||||
cover: '/img/covers/565c0f76dc6e8.jpeg'
|
||||
},
|
||||
{
|
||||
id: 1218,
|
||||
artist_id: 4,
|
||||
name: 'Pat Garrett & Billy the Kid',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
},
|
||||
{
|
||||
id: 1219,
|
||||
artist_id: 4,
|
||||
name: "The Times They Are A-Changin",
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
},
|
||||
{
|
||||
id: 1268,
|
||||
artist_id: 5,
|
||||
name: 'Back To Bedlam',
|
||||
cover: '/img/covers/unknown-album.png'
|
||||
}
|
||||
all4OneAlbum,
|
||||
musicSpeaks,
|
||||
spaceJam,
|
||||
highway,
|
||||
patGarrett,
|
||||
theTimes,
|
||||
backToBedlam
|
||||
],
|
||||
|
||||
songs: [
|
||||
{
|
||||
factory<Song>('song', {
|
||||
artist: all4One,
|
||||
album: all4OneAlbum,
|
||||
id: '39189f4545f9d5671fb3dc964f0080a0',
|
||||
album_id: 1193,
|
||||
artist_id: 3,
|
||||
album_id: all4OneAlbum.id,
|
||||
artist_id: all4One.id,
|
||||
title: 'I Swear',
|
||||
length: 259.92,
|
||||
playCount: 4
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: all4One,
|
||||
album: musicSpeaks,
|
||||
id: 'a6a550f7d950d2a2520f9bf1a60f025a',
|
||||
album_id: 1194,
|
||||
artist_id: 3,
|
||||
album_id: musicSpeaks.id,
|
||||
artist_id: all4One.id,
|
||||
title: 'I can love you like that',
|
||||
length: 262.61,
|
||||
playCount: 2
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: all4One,
|
||||
album: spaceJam,
|
||||
id: 'd86c30fd34f13c1aff8db59b7fc9c610',
|
||||
album_id: 1195,
|
||||
artist_id: 3,
|
||||
album_id: spaceJam.id,
|
||||
artist_id: all4One.id,
|
||||
title: 'I turn to you',
|
||||
length: 293.04
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: bobDylan,
|
||||
album: highway,
|
||||
id: 'e6d3977f3ffa147801ca5d1fdf6fa55e',
|
||||
album_id: 1217,
|
||||
artist_id: 4,
|
||||
album_id: highway.id,
|
||||
artist_id: bobDylan.id,
|
||||
title: 'Like a rolling stone',
|
||||
length: 373.63
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: bobDylan,
|
||||
album: patGarrett,
|
||||
id: 'aa16bbef6a9710eb9a0f41ecc534fad5',
|
||||
album_id: 1218,
|
||||
artist_id: 4,
|
||||
album_id: patGarrett.id,
|
||||
artist_id: bobDylan.id,
|
||||
title: "Knockin' on heaven's door",
|
||||
length: 151.9
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: bobDylan,
|
||||
album: theTimes,
|
||||
id: 'cb7edeac1f097143e65b1b2cde102482',
|
||||
album_id: 1219,
|
||||
artist_id: 4,
|
||||
album_id: theTimes.id,
|
||||
artist_id: bobDylan.id,
|
||||
title: "The times they are a-changin'",
|
||||
length: 196
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '0ba9fb128427b32683b9eb9140912a70',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'No bravery',
|
||||
length: 243.12
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '123fd1ad32240ecab28a4e86ed5173',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'So long, Jimmy',
|
||||
length: 265.04
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '6a54c674d8b16732f26df73f59c63e21',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'Wisemen',
|
||||
length: 223.14
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '6df7d82a9a8701e40d1c291cf14a16bc',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'Goodbye my lover',
|
||||
length: 258.61
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '74a2000d343e4587273d3ad14e2fd741',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'High',
|
||||
length: 245.86
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '7900ab518f51775fe6cf06092c074ee5',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: "You're beautiful",
|
||||
length: 213.29
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: '803910a51f9893347e087af851e38777',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'Cry',
|
||||
length: 246.91
|
||||
},
|
||||
{
|
||||
}),
|
||||
factory<Song>('song', {
|
||||
artist: jamesBlunt,
|
||||
album: backToBedlam,
|
||||
id: 'd82b0d4d4803ebbcb61000a5b6a868f5',
|
||||
album_id: 1268,
|
||||
artist_id: 5,
|
||||
album_id: backToBedlam.id,
|
||||
artist_id: jamesBlunt.id,
|
||||
title: 'Tears and rain',
|
||||
length: 244.45
|
||||
}
|
||||
})
|
||||
],
|
||||
interactions: [
|
||||
{
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
// @ts-nocheck
|
||||
import { cloneDeep } from 'lodash'
|
||||
import { albumStore, artistStore } from '@/stores'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
|
||||
describe('stores/album', () => {
|
||||
beforeEach(() => {
|
||||
artistStore.init(cloneDeep(data.artists))
|
||||
albumStore.init(cloneDeep(data.albums))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
artistStore.state.artists = []
|
||||
albumStore.state.albums = []
|
||||
})
|
||||
|
||||
it('gathers albums', () => {
|
||||
expect(albumStore.state.albums).toHaveLength(7)
|
||||
})
|
||||
|
||||
it('sets album artists', () => {
|
||||
expect(albumStore.state.albums[0].artist.id).toBe(3)
|
||||
})
|
||||
|
||||
it('gets an album by ID', () => {
|
||||
expect(albumStore.byId(1193).name).toBe('All-4-One')
|
||||
})
|
||||
|
||||
it('compacts albums', () => {
|
||||
albumStore.compact()
|
||||
expect(albumStore.state.albums).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('returns all albums', () => {
|
||||
expect(albumStore.all).toHaveLength(7)
|
||||
})
|
||||
})
|
|
@ -1,25 +0,0 @@
|
|||
// @ts-nocheck
|
||||
import { cloneDeep } from 'lodash'
|
||||
import { artistStore } from '@/stores'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
|
||||
describe('stores/artist', () => {
|
||||
beforeEach(() => artistStore.init(cloneDeep(data.artists)))
|
||||
|
||||
afterEach(() => (artistStore.state.artists = []))
|
||||
|
||||
it('gathers artists', () => {
|
||||
expect(artistStore.state.artists).toHaveLength(5)
|
||||
})
|
||||
|
||||
it('gets an artist by ID', () => {
|
||||
expect(artistStore.byId(3).name).toBe('All-4-One')
|
||||
})
|
||||
|
||||
it('compact artists', () => {
|
||||
artistStore.compact()
|
||||
// because we've not processed songs/albums, all artists here have no songs
|
||||
// and should be removed after compacting
|
||||
expect(artistStore.state.artists).toHaveLength(0)
|
||||
})
|
||||
})
|
|
@ -1,35 +0,0 @@
|
|||
import { localStorageService } from '@/services'
|
||||
import { preferenceStore } from '@/stores'
|
||||
import { mock } from '@/__tests__/__helpers__'
|
||||
import factory from '@/__tests__/factory'
|
||||
|
||||
const user = factory<User>('user', { id: 1 })
|
||||
|
||||
describe('stores/preference', () => {
|
||||
beforeEach(() => {
|
||||
preferenceStore.init(user)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetModules()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('sets preferences', () => {
|
||||
const m = mock(localStorageService, 'set')
|
||||
preferenceStore.set('volume', 5)
|
||||
expect(m).toHaveBeenCalledWith('preferences_1', expect.objectContaining({ volume: 5 }))
|
||||
|
||||
// test the proxy
|
||||
preferenceStore.volume = 6
|
||||
expect(m).toHaveBeenCalledWith('preferences_1', expect.objectContaining({ volume: 6 }))
|
||||
})
|
||||
|
||||
it('returns preference values', () => {
|
||||
preferenceStore.set('volume', 4.2)
|
||||
expect(preferenceStore.get('volume')).toBe(4.2)
|
||||
|
||||
// test the proxy
|
||||
expect(preferenceStore.volume).toBe(4.2)
|
||||
})
|
||||
})
|
|
@ -1,93 +0,0 @@
|
|||
// @ts-nocheck
|
||||
import { queueStore } from '@/stores'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
|
||||
describe('stores/queue', () => {
|
||||
const JAMES_BLUNT_ARTIST_ID = 5
|
||||
let songs
|
||||
|
||||
beforeEach(() => {
|
||||
songs = data.songs.filter(song => song.artist_id === JAMES_BLUNT_ARTIST_ID)
|
||||
queueStore.state.songs = songs
|
||||
queueStore.state.current = songs[1]
|
||||
})
|
||||
|
||||
it('returns all queued songs', () => {
|
||||
expect(queueStore.all).toBe(songs)
|
||||
})
|
||||
|
||||
it('returns the first queued song', () => {
|
||||
expect(queueStore.first.title).toBe('No bravery')
|
||||
})
|
||||
|
||||
it('returns the last queued song', () => {
|
||||
expect(queueStore.last.title).toBe('Tears and rain')
|
||||
})
|
||||
|
||||
describe('queues and unqueues', () => {
|
||||
let thatSongByAll4One
|
||||
|
||||
beforeEach(() => {
|
||||
queueStore.state.songs = songs
|
||||
thatSongByAll4One = data.songs[0]
|
||||
})
|
||||
|
||||
it('appends a song to end of the queue', () => {
|
||||
queueStore.queue(thatSongByAll4One)
|
||||
expect(queueStore.last.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('prepends a song to top of the queue', () => {
|
||||
queueStore.queueToTop(thatSongByAll4One)
|
||||
expect(queueStore.first.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('replaces the whole queue', () => {
|
||||
queueStore.replaceQueueWith(data.songs[0])
|
||||
expect(queueStore.all).toHaveLength(1)
|
||||
expect(queueStore.first.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('removes a song from queue', () => {
|
||||
queueStore.unqueue(queueStore.state.songs[0])
|
||||
expect(queueStore.first.title).toBe('So long, Jimmy') // Oh the irony.
|
||||
})
|
||||
|
||||
it('removes mutiple songs from queue', () => {
|
||||
queueStore.unqueue([queueStore.state.songs[0], queueStore.state.songs[1]])
|
||||
expect(queueStore.first.title).toBe('Wisemen')
|
||||
})
|
||||
|
||||
it('removes all songs from queue', () => {
|
||||
queueStore.clear()
|
||||
expect(queueStore.state.songs).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns the current song', () => {
|
||||
expect(queueStore.current.title).toBe('So long, Jimmy')
|
||||
})
|
||||
|
||||
it('sets the current song', () => {
|
||||
queueStore.current = queueStore.state.songs[0]
|
||||
expect(queueStore.current.title).toBe('No bravery')
|
||||
})
|
||||
|
||||
it('gets the next song in queue', () => {
|
||||
expect(queueStore.next.title).toBe('Wisemen')
|
||||
})
|
||||
|
||||
it('returns undefined as next song if at end of queue', () => {
|
||||
queueStore.current = queueStore.state.songs[queueStore.state.songs.length - 1]
|
||||
expect(queueStore.next).toBeUndefined()
|
||||
})
|
||||
|
||||
it('gets the previous song in queue', () => {
|
||||
expect(queueStore.previous.title).toBe('No bravery')
|
||||
})
|
||||
|
||||
it('returns undefined as previous song if at beginning of queue', () => {
|
||||
queueStore.current = queueStore.state.songs[0]
|
||||
expect(queueStore.previous).toBeUndefined()
|
||||
})
|
||||
})
|
|
@ -1,48 +0,0 @@
|
|||
// @ts-nocheck
|
||||
import { songStore, albumStore, artistStore } from '@/stores'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
|
||||
describe('stores/song', () => {
|
||||
beforeEach(() => {
|
||||
artistStore.init(data.artists)
|
||||
albumStore.init(data.albums)
|
||||
songStore.init(data.songs)
|
||||
songStore.initInteractions(data.interactions)
|
||||
})
|
||||
|
||||
it('gathers all songs', () => {
|
||||
expect(songStore.state.songs).toHaveLength(14)
|
||||
})
|
||||
|
||||
it('converts length to formatted lengths', () => {
|
||||
expect(songStore.state.songs[0].fmtLength).toBe('04:19')
|
||||
})
|
||||
|
||||
it('sets albums', () => {
|
||||
expect(songStore.state.songs[0].album.id).toBe(1193)
|
||||
})
|
||||
|
||||
it('returns all songs', () => {
|
||||
expect(songStore.state.songs).toHaveLength(14)
|
||||
})
|
||||
|
||||
it('gets a song by ID', () => {
|
||||
expect(songStore.byId('e6d3977f3ffa147801ca5d1fdf6fa55e').title).toBe('Like a rolling stone')
|
||||
})
|
||||
|
||||
it('gets multiple songs by IDs', () => {
|
||||
const songs = songStore.byIds(['e6d3977f3ffa147801ca5d1fdf6fa55e', 'aa16bbef6a9710eb9a0f41ecc534fad5'])
|
||||
expect(songs[0].title).toBe('Like a rolling stone')
|
||||
expect(songs[1].title).toBe("Knockin' on heaven's door")
|
||||
})
|
||||
|
||||
it('sets interaction status', () => {
|
||||
const song = songStore.byId('cb7edeac1f097143e65b1b2cde102482')
|
||||
expect(song.liked).toBe(true)
|
||||
expect(song.playCount).toBe(3)
|
||||
})
|
||||
|
||||
it('guesses a song', () => {
|
||||
expect(songStore.guess('i swear', albumStore.byId(1193))!.id).toBe('39189f4545f9d5671fb3dc964f0080a0')
|
||||
})
|
||||
})
|
|
@ -1,40 +0,0 @@
|
|||
import { userStore } from '@/stores'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
|
||||
const { users, currentUser } = data
|
||||
|
||||
describe('stores/user', () => {
|
||||
beforeEach(() => userStore.init(users, currentUser))
|
||||
|
||||
it('sets data state', () => {
|
||||
expect(userStore.state.users).toBe(users)
|
||||
expect(userStore.state.current).toBe(currentUser)
|
||||
})
|
||||
|
||||
it('returns all users', () => {
|
||||
expect(userStore.all).toBe(users)
|
||||
})
|
||||
|
||||
it('gets a user by ID', () => {
|
||||
expect(userStore.byId(1)).toBe(users[0])
|
||||
})
|
||||
|
||||
it('gets the current user', () => {
|
||||
expect(userStore.current.id).toBe(1)
|
||||
})
|
||||
|
||||
it('sets the current user', () => {
|
||||
userStore.current = users[1]
|
||||
expect(userStore.current.id).toBe(2)
|
||||
})
|
||||
|
||||
it('sets the current user’s avatar', () => {
|
||||
userStore.setAvatar()
|
||||
expect(userStore.current.avatar).toBe('https://www.gravatar.com/avatar/b9611f1bba1aacbe6f5de5856695a202?s=256&d=mp')
|
||||
})
|
||||
|
||||
it('sets a user’s avatar', () => {
|
||||
userStore.setAvatar(users[1])
|
||||
expect(users[1].avatar).toBe('https://www.gravatar.com/avatar/5024672cfe53f113b746e1923e373058?s=256&d=mp')
|
||||
})
|
||||
})
|
36
resources/assets/js/stores/albumStore.spec.ts
Normal file
36
resources/assets/js/stores/albumStore.spec.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { cloneDeep } from 'lodash'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
import { expect, it } from 'vitest'
|
||||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import { albumStore, artistStore } from '@/stores'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => {
|
||||
artistStore.init(cloneDeep(data.artists))
|
||||
albumStore.init(cloneDeep(data.albums))
|
||||
})
|
||||
}
|
||||
|
||||
protected afterEach () {
|
||||
super.afterEach(() => {
|
||||
artistStore.state.artists = []
|
||||
albumStore.state.albums = []
|
||||
})
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('gathers albums', () => expect(albumStore.state.albums).toHaveLength(7))
|
||||
it('sets album artists', () => expect(albumStore.state.albums[0].artist.id).toBe(3))
|
||||
it('gets an album by ID', () => expect(albumStore.byId(1193).name).toBe('All-4-One'))
|
||||
|
||||
it('compacts albums', () => {
|
||||
// because we've not processed songs, all albums here have no songs
|
||||
// and should be removed after compacting
|
||||
albumStore.compact()
|
||||
expect(albumStore.state.albums).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('returns all albums', () => expect(albumStore.all).toHaveLength(7))
|
||||
}
|
||||
}
|
27
resources/assets/js/stores/artistStore.spec.ts
Normal file
27
resources/assets/js/stores/artistStore.spec.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { cloneDeep } from 'lodash'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
import { expect, it } from 'vitest'
|
||||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import { artistStore } from '@/stores'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => artistStore.init(cloneDeep(data.artists)))
|
||||
}
|
||||
|
||||
protected afterEach () {
|
||||
super.afterEach(() => (artistStore.state.artists = []))
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('gathers artists', () => expect(artistStore.state.artists).toHaveLength(5))
|
||||
it('gets an artist by ID', () => expect(artistStore.byId(3)!.name).toBe('All-4-One'))
|
||||
|
||||
it('compact artists', () => {
|
||||
artistStore.compact()
|
||||
// because we've not processed songs/albums, all artists here have no songs
|
||||
// and should be removed after compacting
|
||||
expect(artistStore.state.artists).toHaveLength(0)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -25,7 +25,6 @@ interface CommonStoreState {
|
|||
favorites: Song[]
|
||||
interactions: Interaction[]
|
||||
latestVersion: string
|
||||
originalMediaPath: string | undefined
|
||||
playlists: Playlist[]
|
||||
queued: Song[]
|
||||
recentlyPlayed: string[]
|
||||
|
@ -48,7 +47,6 @@ export const commonStore = {
|
|||
favorites: [],
|
||||
interactions: [],
|
||||
latestVersion: '',
|
||||
originalMediaPath: '',
|
||||
playlists: [],
|
||||
queued: [],
|
||||
recentlyPlayed: [],
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import factory from '@/__tests__/factory'
|
||||
import { playlistStore } from '@/stores'
|
||||
import { expect, it } from 'vitest'
|
||||
|
||||
const ruleGroups: SmartPlaylistRuleGroup[] = [
|
||||
{
|
||||
|
@ -59,20 +61,21 @@ const serializedRuleGroups = [
|
|||
}
|
||||
]
|
||||
|
||||
describe('stores/playlist', () => {
|
||||
it('serializes playlist for storage', () => {
|
||||
expect(playlistStore.serializeSmartPlaylistRulesForStorage(ruleGroups)).toEqual(serializedRuleGroups)
|
||||
})
|
||||
|
||||
it('set up a smart playlist with properly unserialized rules', () => {
|
||||
// @ts-ignore because we're only using this factory here for convenience.
|
||||
// By right, the database "serializedRuleGroups" can't be used for Playlist type.
|
||||
const playlist = factory<Playlist>('playlist', {
|
||||
is_smart: true,
|
||||
rules: serializedRuleGroups
|
||||
new class extends UnitTestCase {
|
||||
protected test () {
|
||||
it('serializes playlist for storage', () => {
|
||||
expect(playlistStore.serializeSmartPlaylistRulesForStorage(ruleGroups)).toEqual(serializedRuleGroups)
|
||||
})
|
||||
|
||||
playlistStore.setupSmartPlaylist(playlist)
|
||||
expect(playlist.rules).toEqual(ruleGroups)
|
||||
})
|
||||
})
|
||||
it('sets up a smart playlist with properly unserialized rules', () => {
|
||||
const playlist = factory<Playlist>('playlist', {
|
||||
is_smart: true,
|
||||
rules: serializedRuleGroups as unknown as SmartPlaylistRuleGroup[]
|
||||
})
|
||||
|
||||
playlistStore.setupSmartPlaylist(playlist)
|
||||
|
||||
expect(playlist.rules).toEqual(ruleGroups)
|
||||
})
|
||||
}
|
||||
}
|
31
resources/assets/js/stores/preferenceStore.spec.ts
Normal file
31
resources/assets/js/stores/preferenceStore.spec.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import { expect, it } from 'vitest'
|
||||
import factory from '@/__tests__/factory'
|
||||
import { localStorageService } from '@/services'
|
||||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import { preferenceStore } from '@/stores'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => preferenceStore.init(factory<User>('user', { id: 1 })))
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('sets preferences', () => {
|
||||
const mock = this.mock(localStorageService, 'set')
|
||||
preferenceStore.set('volume', 5)
|
||||
expect(mock).toHaveBeenCalledWith('preferences_1', expect.objectContaining({ volume: 5 }))
|
||||
|
||||
// test the proxy
|
||||
preferenceStore.volume = 6
|
||||
expect(mock).toHaveBeenCalledWith('preferences_1', expect.objectContaining({ volume: 6 }))
|
||||
})
|
||||
|
||||
it('returns preference values', () => {
|
||||
preferenceStore.set('volume', 4.2)
|
||||
expect(preferenceStore.get('volume')).toBe(4.2)
|
||||
|
||||
// test the proxy
|
||||
expect(preferenceStore.volume).toBe(4.2)
|
||||
})
|
||||
}
|
||||
}
|
78
resources/assets/js/stores/queueStore.spec.ts
Normal file
78
resources/assets/js/stores/queueStore.spec.ts
Normal file
|
@ -0,0 +1,78 @@
|
|||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
import { queueStore } from '@/stores/queueStore'
|
||||
import { expect, it } from 'vitest'
|
||||
|
||||
const ARTIST_ID = 5
|
||||
let songs
|
||||
let songToQueue: Song
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => {
|
||||
songs = data.songs.filter(song => song.artist_id === ARTIST_ID)
|
||||
queueStore.state.songs = songs
|
||||
queueStore.state.current = songs[1]
|
||||
|
||||
songToQueue = data.songs[0]
|
||||
})
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('returns all queued songs', () => expect(queueStore.all).toEqual(songs))
|
||||
it('returns the first queued song', () => expect(queueStore.first.title).toBe('No bravery'))
|
||||
it('returns the last queued song', () => expect(queueStore.last.title).toBe('Tears and rain'))
|
||||
|
||||
it('appends a song to end of the queue', () => {
|
||||
queueStore.queue(songToQueue)
|
||||
expect(queueStore.last.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('prepends a song to top of the queue', () => {
|
||||
queueStore.queueToTop(songToQueue)
|
||||
expect(queueStore.first.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('replaces the whole queue', () => {
|
||||
queueStore.replaceQueueWith(songToQueue)
|
||||
expect(queueStore.all).toHaveLength(1)
|
||||
expect(queueStore.first.title).toBe('I Swear')
|
||||
})
|
||||
|
||||
it('removes a song from queue', () => {
|
||||
queueStore.unqueue(queueStore.state.songs[0])
|
||||
expect(queueStore.first.title).toBe('So long, Jimmy')
|
||||
})
|
||||
|
||||
it('removes multiple songs from queue', () => {
|
||||
queueStore.unqueue([queueStore.state.songs[0], queueStore.state.songs[1]])
|
||||
expect(queueStore.first.title).toBe('Wisemen')
|
||||
})
|
||||
|
||||
it('removes all songs from queue', () => {
|
||||
queueStore.clear()
|
||||
expect(queueStore.state.songs).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('returns the current song', () => expect(queueStore.current?.title).toBe('So long, Jimmy'))
|
||||
|
||||
it('sets the current song', () => {
|
||||
queueStore.current = queueStore.state.songs[0]
|
||||
expect(queueStore.current.title).toBe('No bravery')
|
||||
})
|
||||
|
||||
it('gets the next song in queue', () => expect(queueStore.next?.title).toBe('Wisemen'))
|
||||
|
||||
it('returns undefined as next song if at end of queue', () => {
|
||||
queueStore.current = queueStore.state.songs[queueStore.state.songs.length - 1]
|
||||
expect(queueStore.next).toBeUndefined()
|
||||
})
|
||||
|
||||
it('gets the previous song in queue', () => expect(queueStore.previous?.title).toBe('No bravery'))
|
||||
|
||||
it('returns undefined as previous song if at beginning of queue', () => {
|
||||
queueStore.current = queueStore.state.songs[0]
|
||||
expect(queueStore.previous).toBeUndefined()
|
||||
})
|
||||
}
|
||||
}
|
44
resources/assets/js/stores/songStore.spec.ts
Normal file
44
resources/assets/js/stores/songStore.spec.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
import { expect, it } from 'vitest'
|
||||
import { artistStore } from '@/stores/artistStore'
|
||||
import { albumStore } from '@/stores/albumStore'
|
||||
import { songStore } from '@/stores/songStore'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => {
|
||||
artistStore.init(data.artists)
|
||||
albumStore.init(data.albums)
|
||||
songStore.init(data.songs)
|
||||
songStore.initInteractions(data.interactions)
|
||||
})
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('gathers all songs', () => expect(songStore.state.songs).toHaveLength(14))
|
||||
it('converts length to formatted lengths', () => expect(songStore.state.songs[0].fmtLength).toBe('04:19'))
|
||||
it('sets albums', () => expect(songStore.state.songs[0].album.id).toBe(1193))
|
||||
it('returns all songs', () => expect(songStore.state.songs).toHaveLength(14))
|
||||
|
||||
it('gets a song by ID', () => {
|
||||
expect(songStore.byId('e6d3977f3ffa147801ca5d1fdf6fa55e').title).toBe('Like a rolling stone')
|
||||
})
|
||||
|
||||
it('gets multiple songs by IDs', () => {
|
||||
const songs = songStore.byIds(['e6d3977f3ffa147801ca5d1fdf6fa55e', 'aa16bbef6a9710eb9a0f41ecc534fad5'])
|
||||
expect(songs[0].title).toBe('Like a rolling stone')
|
||||
expect(songs[1].title).toBe("Knockin' on heaven's door")
|
||||
})
|
||||
|
||||
it('sets interaction status', () => {
|
||||
const song = songStore.byId('cb7edeac1f097143e65b1b2cde102482')
|
||||
expect(song.liked).toBe(true)
|
||||
expect(song.playCount).toBe(3)
|
||||
})
|
||||
|
||||
it('guesses a song', () => {
|
||||
expect(songStore.guess('i swear', albumStore.byId(1193))!.id).toBe('39189f4545f9d5671fb3dc964f0080a0')
|
||||
})
|
||||
}
|
||||
}
|
38
resources/assets/js/stores/userStore.spec.ts
Normal file
38
resources/assets/js/stores/userStore.spec.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import data from '@/__tests__/blobs/data'
|
||||
import { expect, it } from 'vitest'
|
||||
import { userStore } from '@/stores/userStore'
|
||||
|
||||
const { users, currentUser } = data
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => userStore.init(users, currentUser))
|
||||
}
|
||||
|
||||
protected test () {
|
||||
it('sets data state', () => {
|
||||
expect(userStore.state.users).toEqual(users)
|
||||
expect(userStore.state.current).toEqual(currentUser)
|
||||
})
|
||||
|
||||
it('returns all users', () => expect(userStore.all).toEqual(users))
|
||||
it('gets a user by ID', () => expect(userStore.byId(1)).toEqual(users[0]))
|
||||
it('gets the current user', () => expect(userStore.current.id).toBe(1))
|
||||
|
||||
it('sets the current user', () => {
|
||||
userStore.current = users[1]
|
||||
expect(userStore.current.id).toBe(2)
|
||||
})
|
||||
|
||||
it('sets the current user’s avatar', () => {
|
||||
userStore.setAvatar()
|
||||
expect(userStore.current.avatar).toBe('https://www.gravatar.com/avatar/b9611f1bba1aacbe6f5de5856695a202?s=256&d=mp')
|
||||
})
|
||||
|
||||
it('sets a user’s avatar', () => {
|
||||
userStore.setAvatar(users[1])
|
||||
expect(users[1].avatar).toBe('https://www.gravatar.com/avatar/5024672cfe53f113b746e1923e373058?s=256&d=mp')
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue