2022-07-12 16:49:15 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { eventBus } from '@/utils'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen, waitFor } from '@testing-library/vue'
|
2022-10-08 10:54:25 +00:00
|
|
|
import { playlistStore, songStore } from '@/stores'
|
2022-07-12 16:49:15 +00:00
|
|
|
import { downloadService } from '@/services'
|
|
|
|
import PlaylistScreen from './PlaylistScreen.vue'
|
|
|
|
|
|
|
|
let playlist: Playlist
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected test () {
|
|
|
|
it('renders the playlist', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
await this.renderComponent(factory('song', 10))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
2022-11-29 10:18:58 +00:00
|
|
|
screen.getByTestId('song-list')
|
|
|
|
expect(screen.queryByTestId('screen-empty-state')).toBeNull()
|
2022-07-12 16:49:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('displays the empty state if playlist is empty', async () => {
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent([])
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
2022-11-29 10:18:58 +00:00
|
|
|
screen.getByTestId('screen-empty-state')
|
|
|
|
expect(screen.queryByTestId('song-list')).toBeNull()
|
2022-07-12 16:49:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('downloads the playlist', async () => {
|
|
|
|
const downloadMock = this.mock(downloadService, 'fromPlaylist')
|
2024-06-01 18:02:27 +00:00
|
|
|
await this.renderComponent(factory('song', 10))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
await this.tick(2)
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Download All' }))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => expect(downloadMock).toHaveBeenCalledWith(playlist))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes the playlist', async () => {
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent([])
|
2022-07-12 16:49:15 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Delete this playlist' }))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => expect(emitMock).toHaveBeenCalledWith('PLAYLIST_DELETE', playlist))
|
|
|
|
})
|
2022-11-08 19:35:18 +00:00
|
|
|
|
|
|
|
it('refreshes the playlist', async () => {
|
2022-11-29 10:18:58 +00:00
|
|
|
const { fetchMock } = await this.renderComponent([])
|
2022-11-08 19:35:18 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByRole('button', { name: 'Refresh' }))
|
2022-11-08 19:35:18 +00:00
|
|
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist, true)
|
|
|
|
})
|
2022-07-12 16:49:15 +00:00
|
|
|
}
|
2024-04-23 21:01:27 +00:00
|
|
|
|
2024-06-02 17:15:31 +00:00
|
|
|
private async renderComponent (songs: Playable[]) {
|
2024-06-01 18:02:27 +00:00
|
|
|
playlist = playlist || factory('playlist')
|
2024-07-26 14:33:14 +00:00
|
|
|
this.be(factory('user', { id: playlist.user_id }))
|
|
|
|
|
2024-04-23 21:01:27 +00:00
|
|
|
playlistStore.init([playlist])
|
2024-06-02 17:15:31 +00:00
|
|
|
playlist.playables = songs
|
2024-04-23 21:01:27 +00:00
|
|
|
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
|
|
|
|
const rendered = this.render(PlaylistScreen)
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: `playlists/${playlist.id}`,
|
|
|
|
screen: 'Playlist'
|
|
|
|
}, { id: playlist.id })
|
|
|
|
|
|
|
|
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(playlist, false))
|
|
|
|
|
|
|
|
return { rendered, fetchMock }
|
|
|
|
}
|
2022-07-12 16:49:15 +00:00
|
|
|
}
|