2022-07-23 10:36:56 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2022-09-14 16:45:08 +00:00
|
|
|
import { recentlyPlayedStore, songStore } from '.'
|
2022-07-23 10:36:56 +00:00
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected test () {
|
|
|
|
it('fetches the recently played songs', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const songs = factory('song', 3)
|
2022-09-15 09:07:25 +00:00
|
|
|
const getMock = this.mock(http, 'get').mockResolvedValue(songs)
|
2022-07-23 10:36:56 +00:00
|
|
|
const syncMock = this.mock(songStore, 'syncWithVault', songs)
|
|
|
|
|
|
|
|
await recentlyPlayedStore.fetch()
|
|
|
|
|
2022-07-27 09:30:04 +00:00
|
|
|
expect(getMock).toHaveBeenCalledWith('songs/recently-played')
|
2022-07-23 10:36:56 +00:00
|
|
|
expect(syncMock).toHaveBeenCalledWith(songs)
|
2024-05-19 05:49:42 +00:00
|
|
|
expect(recentlyPlayedStore.state.playables).toEqual(songs)
|
2022-07-23 10:36:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fetches when attempting to add a new song and the state is empty', async () => {
|
2024-05-19 05:49:42 +00:00
|
|
|
recentlyPlayedStore.state.playables = []
|
2024-06-01 18:02:27 +00:00
|
|
|
const songs = factory('song', 3)
|
2022-07-23 10:36:56 +00:00
|
|
|
const fetchMock = this.mock(recentlyPlayedStore, 'fetch').mockResolvedValue(songs)
|
|
|
|
|
2024-06-01 18:02:27 +00:00
|
|
|
await recentlyPlayedStore.add(factory('song'))
|
2022-07-23 10:36:56 +00:00
|
|
|
|
|
|
|
expect(fetchMock).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a song to the state', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const newSong = factory('song')
|
|
|
|
const songs = factory('song', 10)
|
2022-07-23 10:36:56 +00:00
|
|
|
const exceptSongs = songs.slice(0, 7)
|
|
|
|
|
|
|
|
// We don't want to keep the reference to the original songs
|
2024-05-19 05:49:42 +00:00
|
|
|
recentlyPlayedStore.state.playables = JSON.parse(JSON.stringify(songs))
|
|
|
|
recentlyPlayedStore.excerptState.playables = JSON.parse(JSON.stringify(exceptSongs))
|
2022-07-23 10:36:56 +00:00
|
|
|
|
|
|
|
await recentlyPlayedStore.add(newSong)
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
expect(recentlyPlayedStore.state.playables).toEqual([newSong, ...songs])
|
|
|
|
expect(recentlyPlayedStore.excerptState.playables).toEqual([newSong, ...songs.slice(0, 6)])
|
2022-07-23 10:36:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('deduplicates when adding a song to the state', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const songs = factory('song', 10)
|
2022-07-23 10:36:56 +00:00
|
|
|
const newSong = songs[1]
|
|
|
|
const exceptSongs = songs.slice(0, 7)
|
|
|
|
|
|
|
|
// We don't want to keep the reference to the original songs
|
2024-05-19 05:49:42 +00:00
|
|
|
recentlyPlayedStore.state.playables = JSON.parse(JSON.stringify(songs))
|
|
|
|
recentlyPlayedStore.excerptState.playables = JSON.parse(JSON.stringify(exceptSongs))
|
2022-07-23 10:36:56 +00:00
|
|
|
|
|
|
|
await recentlyPlayedStore.add(newSong)
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
expect(recentlyPlayedStore.state.playables).toEqual([newSong, songs[0], ...songs.slice(2)])
|
|
|
|
expect(recentlyPlayedStore.excerptState.playables).toEqual([newSong, songs[0], ...songs.slice(2, 7)])
|
2022-07-23 10:36:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|