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-11-29 10:18:58 +00:00
|
|
|
import { screen, waitFor } from '@testing-library/vue'
|
2022-09-08 05:06:49 +00:00
|
|
|
import { playlistStore, songStore } from '@/stores'
|
|
|
|
import { playbackService } from '@/services'
|
2022-12-07 14:11:40 +00:00
|
|
|
import { MessageToasterStub } from '@/__tests__/stubs'
|
2022-09-08 05:06:49 +00:00
|
|
|
import PlaylistFolderContextMenu from './PlaylistFolderContextMenu.vue'
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
private async renderComponent (folder: PlaylistFolder) {
|
2022-12-02 16:17:37 +00:00
|
|
|
this.render(PlaylistFolderContextMenu)
|
|
|
|
eventBus.emit('PLAYLIST_FOLDER_CONTEXT_MENU_REQUESTED', { pageX: 420, pageY: 42 } as MouseEvent, folder)
|
2022-09-08 05:06:49 +00:00
|
|
|
await this.tick(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
|
|
|
it('renames', async () => {
|
|
|
|
const folder = factory<PlaylistFolder>('playlist-folder')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(folder)
|
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('Rename'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_EDIT_PLAYLIST_FOLDER_FORM', folder)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes', async () => {
|
|
|
|
const folder = factory<PlaylistFolder>('playlist-folder')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(folder)
|
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_FOLDER_DELETE', folder)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('plays', async () => {
|
|
|
|
const folder = this.createPlayableFolder()
|
|
|
|
const songs = factory<Song>('song', 3)
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylistFolder').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2022-10-08 10:54:25 +00:00
|
|
|
const goMock = this.mock(this.router, 'go')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(folder)
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Play All'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(folder)
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
expect(goMock).toHaveBeenCalledWith('queue')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-12-07 14:11:40 +00:00
|
|
|
it('warns if attempting to play with no songs in folder', async () => {
|
|
|
|
const folder = this.createPlayableFolder()
|
|
|
|
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylistFolder').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
const warnMock = this.mock(MessageToasterStub.value, 'warning')
|
|
|
|
|
|
|
|
await this.renderComponent(folder)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Play All'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(folder)
|
|
|
|
expect(queueMock).not.toHaveBeenCalled()
|
|
|
|
expect(goMock).not.toHaveBeenCalled()
|
|
|
|
expect(warnMock).toHaveBeenCalledWith('No songs available.')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
it('shuffles', async () => {
|
|
|
|
const folder = this.createPlayableFolder()
|
|
|
|
const songs = factory<Song>('song', 3)
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylistFolder').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2022-10-08 10:54:25 +00:00
|
|
|
const goMock = this.mock(this.router, 'go')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(folder)
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Shuffle All'))
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(folder)
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs, true)
|
|
|
|
expect(goMock).toHaveBeenCalledWith('queue')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not show shuffle option if folder is empty', async () => {
|
|
|
|
const folder = factory<PlaylistFolder>('playlist-folder')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(folder)
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('Shuffle All')).toBeNull()
|
|
|
|
expect(screen.queryByText('Play All')).toBeNull()
|
2022-09-08 05:06:49 +00:00
|
|
|
})
|
2022-12-07 14:11:40 +00:00
|
|
|
|
|
|
|
it('warns if attempting to shuffle with no songs in folder', async () => {
|
|
|
|
const folder = this.createPlayableFolder()
|
|
|
|
|
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylistFolder').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
|
|
|
const goMock = this.mock(this.router, 'go')
|
|
|
|
const warnMock = this.mock(MessageToasterStub.value, 'warning')
|
|
|
|
|
|
|
|
await this.renderComponent(folder)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Shuffle All'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(folder)
|
|
|
|
expect(queueMock).not.toHaveBeenCalled()
|
|
|
|
expect(goMock).not.toHaveBeenCalled()
|
|
|
|
expect(warnMock).toHaveBeenCalledWith('No songs available.')
|
|
|
|
})
|
|
|
|
})
|
2022-09-08 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private createPlayableFolder () {
|
|
|
|
const folder = factory<PlaylistFolder>('playlist-folder')
|
|
|
|
this.mock(playlistStore, 'byFolder', factory<Playlist>('playlist', 3, { folder_id: folder.id }))
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
}
|