2022-09-08 05:06:49 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
|
|
|
import { eventBus } from '@/utils'
|
|
|
|
import factory from '@/__tests__/factory'
|
2022-12-07 14:11:40 +00:00
|
|
|
import { screen, waitFor } from '@testing-library/vue'
|
|
|
|
import { songStore } from '@/stores'
|
|
|
|
import { playbackService } from '@/services'
|
|
|
|
import { MessageToasterStub } from '@/__tests__/stubs'
|
2022-09-08 05:06:49 +00:00
|
|
|
import PlaylistContextMenu from './PlaylistContextMenu.vue'
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
private async renderComponent (playlist: Playlist) {
|
2022-12-02 16:17:37 +00:00
|
|
|
this.render(PlaylistContextMenu)
|
|
|
|
eventBus.emit('PLAYLIST_CONTEXT_MENU_REQUESTED', { pageX: 420, pageY: 42 } as MouseEvent, playlist)
|
2022-09-08 05:06:49 +00:00
|
|
|
await this.tick(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
2022-11-27 15:29:29 +00:00
|
|
|
it('edits a standard playlist', async () => {
|
2022-09-08 05:06:49 +00:00
|
|
|
const playlist = factory<Playlist>('playlist')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(playlist)
|
2022-09-08 05:06:49 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
2022-12-06 10:28:48 +00:00
|
|
|
await this.user.click(screen.getByText('Edit…'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_EDIT_PLAYLIST_FORM', playlist)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('edits a smart playlist', async () => {
|
|
|
|
const playlist = factory.states('smart')<Playlist>('playlist')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(playlist)
|
2022-09-08 05:06:49 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
2022-12-06 10:28:48 +00:00
|
|
|
await this.user.click(screen.getByText('Edit…'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_EDIT_PLAYLIST_FORM', playlist)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes a playlist', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(playlist)
|
2022-09-08 05:06:49 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Delete'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('PLAYLIST_DELETE', playlist)
|
|
|
|
})
|
2022-12-07 14:11:40 +00:00
|
|
|
|
|
|
|
it('plays', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
|
|
|
const songs = factory<Song>('song', 3)
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
await this.renderComponent(playlist)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Play'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist)
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
expect(goMock).toHaveBeenCalledWith('queue')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('warns if attempting to play an empty playlist', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
const warnMock = this.mock(MessageToasterStub.value, 'warning')
|
|
|
|
|
|
|
|
await this.renderComponent(playlist)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Play'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist)
|
|
|
|
expect(queueMock).not.toHaveBeenCalled()
|
|
|
|
expect(goMock).not.toHaveBeenCalled()
|
|
|
|
expect(warnMock).toHaveBeenCalledWith('The playlist is empty.')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('shuffles', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
|
|
|
const songs = factory<Song>('song', 3)
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
await this.renderComponent(playlist)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Shuffle'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist)
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs, true)
|
|
|
|
expect(goMock).toHaveBeenCalledWith('queue')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('warns if attempting to shuffle an empty playlist', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
const warnMock = this.mock(MessageToasterStub.value, 'warning')
|
|
|
|
|
|
|
|
await this.renderComponent(playlist)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Shuffle'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist)
|
|
|
|
expect(queueMock).not.toHaveBeenCalled()
|
|
|
|
expect(goMock).not.toHaveBeenCalled()
|
|
|
|
expect(warnMock).toHaveBeenCalledWith('The playlist is empty.')
|
|
|
|
})
|
|
|
|
})
|
2022-09-08 05:06:49 +00:00
|
|
|
}
|
|
|
|
}
|