koel/resources/assets/js/components/screens/PlaylistScreen.spec.ts

78 lines
2.4 KiB
TypeScript
Raw Normal View History

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 { screen, waitFor } from '@testing-library/vue'
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[]) {
2022-12-03 15:55:22 +00:00
playlist = playlist || factory<Playlist>('playlist')
playlistStore.init([playlist])
2022-07-12 16:49:15 +00:00
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
2022-12-03 15:55:22 +00:00
this.render(PlaylistScreen)
await this.router.activateRoute({
path: `playlists/${playlist.id}`,
screen: 'Playlist'
}, { id: playlist.id.toString() })
2022-07-12 16:49:15 +00:00
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(playlist, false))
2022-07-12 16:49:15 +00:00
return { fetchMock }
2022-07-12 16:49:15 +00:00
}
protected test () {
it('renders the playlist', async () => {
await this.renderComponent(factory<Song>('song', 10))
2022-07-12 16:49:15 +00:00
await waitFor(() => {
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 () => {
await this.renderComponent([])
2022-07-12 16:49:15 +00:00
await waitFor(() => {
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')
await this.renderComponent(factory<Song>('song', 10))
2022-07-12 16:49:15 +00:00
await this.tick()
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')
await this.renderComponent([])
2022-07-12 16:49:15 +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))
})
it('refreshes the playlist', async () => {
const { fetchMock } = await this.renderComponent([])
await this.user.click(screen.getByRole('button', { name: 'Refresh' }))
expect(fetchMock).toHaveBeenCalledWith(playlist, true)
})
2022-07-12 16:49:15 +00:00
}
}