2024-01-18 11:13:05 +00:00
|
|
|
import Router from '@/router'
|
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'
|
2024-04-23 21:01:27 +00:00
|
|
|
import { queueStore, songStore, userStore } from '@/stores'
|
2024-01-26 14:24:46 +00:00
|
|
|
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 PlaylistContextMenu from './PlaylistContextMenu.vue'
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected test () {
|
2022-11-27 15:29:29 +00:00
|
|
|
it('edits a standard playlist', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory.states('smart')('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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
|
|
|
const songs = factory('song', 3)
|
2022-12-07 14:11:40 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2024-01-18 11:13:05 +00:00
|
|
|
const goMock = this.mock(Router, 'go')
|
2022-12-07 14:11:40 +00:00
|
|
|
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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
2022-12-07 14:11:40 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2024-01-18 11:13:05 +00:00
|
|
|
const goMock = this.mock(Router, 'go')
|
2022-12-07 14:11:40 +00:00
|
|
|
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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
|
|
|
const songs = factory('song', 3)
|
2022-12-07 14:11:40 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2024-01-18 11:13:05 +00:00
|
|
|
const goMock = this.mock(Router, 'go')
|
2022-12-07 14:11:40 +00:00
|
|
|
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 () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
2022-12-07 14:11:40 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue([])
|
|
|
|
const queueMock = this.mock(playbackService, 'queueAndPlay')
|
2024-01-18 11:13:05 +00:00
|
|
|
const goMock = this.mock(Router, 'go')
|
2022-12-07 14:11:40 +00:00
|
|
|
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.')
|
|
|
|
})
|
|
|
|
})
|
2024-01-16 22:24:15 +00:00
|
|
|
|
|
|
|
it('queues', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
|
|
|
const songs = factory('song', 3)
|
2024-01-16 22:24:15 +00:00
|
|
|
const fetchMock = this.mock(songStore, 'fetchForPlaylist').mockResolvedValue(songs)
|
|
|
|
const queueMock = this.mock(queueStore, 'queueAfterCurrent')
|
|
|
|
const toastMock = this.mock(MessageToasterStub.value, 'success')
|
|
|
|
await this.renderComponent(playlist)
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('Add to Queue'))
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(playlist)
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
expect(toastMock).toHaveBeenCalledWith('Playlist added to queue.')
|
|
|
|
})
|
|
|
|
})
|
2024-01-18 11:13:05 +00:00
|
|
|
|
|
|
|
it('does not have an option to edit or delete if the playlist is not owned by the current user', async () => {
|
2024-06-01 18:02:27 +00:00
|
|
|
const user = factory('user')
|
|
|
|
const playlist = factory('playlist', {
|
2024-01-18 11:13:05 +00:00
|
|
|
user_id: user.id + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
await this.renderComponent(playlist, user)
|
|
|
|
|
|
|
|
expect(screen.queryByText('Edit…')).toBeNull()
|
|
|
|
expect(screen.queryByText('Delete')).toBeNull()
|
|
|
|
})
|
|
|
|
|
2024-01-26 14:24:46 +00:00
|
|
|
it('opens collaboration form', async () => {
|
2024-01-18 11:13:05 +00:00
|
|
|
this.enablePlusEdition()
|
2024-06-01 18:02:27 +00:00
|
|
|
const playlist = factory('playlist')
|
2024-01-18 11:13:05 +00:00
|
|
|
await this.renderComponent(playlist)
|
2024-01-26 14:24:46 +00:00
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2024-01-18 11:13:05 +00:00
|
|
|
|
2024-01-26 14:24:46 +00:00
|
|
|
await this.user.click(screen.getByText('Collaborate…'))
|
2024-01-18 11:13:05 +00:00
|
|
|
|
2024-01-26 14:24:46 +00:00
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_PLAYLIST_COLLABORATION', playlist)
|
2024-01-18 11:13:05 +00:00
|
|
|
})
|
2022-09-08 05:06:49 +00:00
|
|
|
}
|
2024-04-23 21:01:27 +00:00
|
|
|
|
|
|
|
private async renderComponent (playlist: Playlist, user: User | null = null) {
|
2024-06-01 18:02:27 +00:00
|
|
|
userStore.state.current = user || factory('user', {
|
2024-04-23 21:01:27 +00:00
|
|
|
id: playlist.user_id
|
|
|
|
})
|
|
|
|
|
|
|
|
this.render(PlaylistContextMenu)
|
|
|
|
eventBus.emit('PLAYLIST_CONTEXT_MENU_REQUESTED', { pageX: 420, pageY: 42 } as MouseEvent, playlist)
|
|
|
|
await this.tick(2)
|
|
|
|
}
|
2022-09-08 05:06:49 +00:00
|
|
|
}
|