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'
|
|
|
|
import { fireEvent, getByTestId, 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 {
|
|
|
|
private async renderComponent (songs: Song[]) {
|
|
|
|
playlist = playlist || factory<Playlist>('playlist')
|
2022-10-08 10:54:25 +00:00
|
|
|
playlistStore.init([playlist])
|
|
|
|
|
2022-07-12 16:49:15 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
|
|
|
|
const rendered = this.render(PlaylistScreen)
|
2022-10-08 10:54:25 +00:00
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: `playlists/${playlist.id}`,
|
|
|
|
screen: 'Playlist'
|
|
|
|
}, { id: playlist.id.toString() })
|
2022-07-12 16:49:15 +00:00
|
|
|
|
2022-11-08 19:35:18 +00:00
|
|
|
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(playlist, false))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
return { rendered, fetchMock }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('renders the playlist', async () => {
|
2022-11-08 19:35:18 +00:00
|
|
|
const { rendered: { getByTestId, queryByTestId } } = (await this.renderComponent(factory<Song>('song', 10)))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
getByTestId('song-list')
|
|
|
|
expect(queryByTestId('screen-empty-state')).toBeNull()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('displays the empty state if playlist is empty', async () => {
|
2022-11-08 19:35:18 +00:00
|
|
|
const { rendered: { getByTestId, queryByTestId } } = (await this.renderComponent([]))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
getByTestId('screen-empty-state')
|
|
|
|
expect(queryByTestId('song-list')).toBeNull()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('downloads the playlist', async () => {
|
|
|
|
const downloadMock = this.mock(downloadService, 'fromPlaylist')
|
2022-11-08 19:35:18 +00:00
|
|
|
const { rendered: { getByText } } = (await this.renderComponent(factory<Song>('song', 10)))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await this.tick()
|
|
|
|
await fireEvent.click(getByText('Download All'))
|
|
|
|
|
|
|
|
await waitFor(() => expect(downloadMock).toHaveBeenCalledWith(playlist))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes the playlist', async () => {
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-11-08 19:35:18 +00:00
|
|
|
const { rendered: { getByTitle } } = (await this.renderComponent([]))
|
2022-07-12 16:49:15 +00:00
|
|
|
|
|
|
|
await fireEvent.click(getByTitle('Delete this playlist'))
|
|
|
|
|
|
|
|
await waitFor(() => expect(emitMock).toHaveBeenCalledWith('PLAYLIST_DELETE', playlist))
|
|
|
|
})
|
2022-11-08 19:35:18 +00:00
|
|
|
|
|
|
|
it('refreshes the playlist', async () => {
|
|
|
|
const { rendered: { getByTitle }, fetchMock } = (await this.renderComponent([]))
|
|
|
|
|
|
|
|
await fireEvent.click(getByTitle('Refresh'))
|
|
|
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist, true)
|
|
|
|
})
|
2022-07-12 16:49:15 +00:00
|
|
|
}
|
|
|
|
}
|