2022-07-19 08:19:57 +00:00
|
|
|
import { expect, it } from 'vitest'
|
|
|
|
import factory from '@/__tests__/factory'
|
|
|
|
import UnitTestCase from '@/__tests__/UnitTestCase'
|
2022-07-26 14:46:02 +00:00
|
|
|
import { arrayify, eventBus } from '@/utils'
|
2022-11-29 10:18:58 +00:00
|
|
|
import { screen, waitFor } from '@testing-library/vue'
|
2022-07-19 08:19:57 +00:00
|
|
|
import { downloadService, playbackService } from '@/services'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { favoriteStore, playlistStore, queueStore, songStore } from '@/stores'
|
|
|
|
import { DialogBoxStub, MessageToasterStub } from '@/__tests__/stubs'
|
2022-07-26 14:46:02 +00:00
|
|
|
import SongContextMenu from './SongContextMenu.vue'
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
let songs: Song[]
|
|
|
|
|
|
|
|
new class extends UnitTestCase {
|
|
|
|
protected beforeEach () {
|
2024-01-01 11:40:21 +00:00
|
|
|
super.beforeEach(() => queueStore.state.songs = [])
|
2022-07-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async renderComponent (_songs?: Song | Song[]) {
|
2022-09-08 05:06:49 +00:00
|
|
|
songs = arrayify(_songs || factory<Song>('song', 5))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
const rendered = this.render(SongContextMenu)
|
2022-11-29 10:18:58 +00:00
|
|
|
eventBus.emit('SONG_CONTEXT_MENU_REQUESTED', { pageX: 420, pageY: 42 } as MouseEvent, songs)
|
2022-07-19 08:19:57 +00:00
|
|
|
await this.tick(2)
|
|
|
|
|
|
|
|
return rendered
|
|
|
|
}
|
|
|
|
|
|
|
|
private fillQueue () {
|
2022-09-08 05:06:49 +00:00
|
|
|
queueStore.state.songs = factory<Song>('song', 5)
|
|
|
|
queueStore.state.songs[2].playback_state = 'Playing'
|
2022-07-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected test () {
|
2022-09-14 12:12:06 +00:00
|
|
|
it('queues and plays', async () => {
|
2022-07-19 08:19:57 +00:00
|
|
|
const queueMock = this.mock(queueStore, 'queueIfNotQueued')
|
|
|
|
const playMock = this.mock(playbackService, 'play')
|
|
|
|
const song = factory<Song>('song', { playback_state: 'Stopped' })
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(song)
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Play'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(song)
|
|
|
|
expect(playMock).toHaveBeenCalledWith(song)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('pauses playback', async () => {
|
|
|
|
const pauseMock = this.mock(playbackService, 'pause')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(factory<Song>('song', { playback_state: 'Playing' }))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Pause'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(pauseMock).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('resumes playback', async () => {
|
|
|
|
const resumeMock = this.mock(playbackService, 'resume')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(factory<Song>('song', { playback_state: 'Paused' }))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Play'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(resumeMock).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('goes to album details screen', async () => {
|
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(factory<Song>('song'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Go to Album'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(goMock).toHaveBeenCalledWith(`album/${songs[0].album_id}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('goes to artist details screen', async () => {
|
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(factory<Song>('song'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Go to Artist'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(goMock).toHaveBeenCalledWith(`artist/${songs[0].artist_id}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('downloads', async () => {
|
|
|
|
const downloadMock = this.mock(downloadService, 'fromSongs')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Download'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(downloadMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('queues', async () => {
|
|
|
|
const queueMock = this.mock(queueStore, 'queue')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Queue'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('queues after current song', async () => {
|
|
|
|
this.fillQueue()
|
|
|
|
const queueMock = this.mock(queueStore, 'queueAfterCurrent')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('After Current Song'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('queues to bottom', async () => {
|
|
|
|
this.fillQueue()
|
|
|
|
const queueMock = this.mock(queueStore, 'queue')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Bottom of Queue'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('queues to top', async () => {
|
|
|
|
this.fillQueue()
|
|
|
|
const queueMock = this.mock(queueStore, 'queueToTop')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Top of Queue'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(queueMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
2022-10-24 15:27:17 +00:00
|
|
|
it('removes from queue', async () => {
|
|
|
|
this.fillQueue()
|
|
|
|
const removeMock = this.mock(queueStore, 'unqueue')
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: '/queue',
|
|
|
|
screen: 'Queue'
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Remove from Queue'))
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
expect(removeMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not show "Remove from Queue" when not on Queue screen', async () => {
|
|
|
|
this.fillQueue()
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: '/songs',
|
|
|
|
screen: 'Songs'
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('Remove from Queue')).toBeNull()
|
2022-10-24 15:27:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('adds to favorites', async () => {
|
2022-07-19 08:19:57 +00:00
|
|
|
const likeMock = this.mock(favoriteStore, 'like')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Favorites'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(likeMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
2022-10-24 15:27:17 +00:00
|
|
|
it('does not have an option to add to favorites for Favorites screen', async () => {
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: '/favorites',
|
|
|
|
screen: 'Favorites'
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('Favorites')).toBeNull()
|
2022-10-24 15:27:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('removes from favorites', async () => {
|
|
|
|
const unlikeMock = this.mock(favoriteStore, 'unlike')
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: '/favorites',
|
|
|
|
screen: 'Favorites'
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Remove from Favorites'))
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
expect(unlikeMock).toHaveBeenCalledWith(songs)
|
|
|
|
})
|
|
|
|
|
2022-07-19 08:19:57 +00:00
|
|
|
it('lists and adds to existing playlist', async () => {
|
2022-09-08 05:06:49 +00:00
|
|
|
playlistStore.state.playlists = factory<Playlist>('playlist', 3)
|
2022-07-19 08:19:57 +00:00
|
|
|
const addMock = this.mock(playlistStore, 'addSongs')
|
2022-07-26 14:46:02 +00:00
|
|
|
this.mock(MessageToasterStub.value, 'success')
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
playlistStore.state.playlists.forEach(playlist => screen.queryByText(playlist.name))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText(playlistStore.state.playlists[0].name))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(addMock).toHaveBeenCalledWith(playlistStore.state.playlists[0], songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not list smart playlists', async () => {
|
2022-09-08 05:06:49 +00:00
|
|
|
playlistStore.state.playlists = factory<Playlist>('playlist', 3)
|
2022-07-19 08:19:57 +00:00
|
|
|
playlistStore.state.playlists.push(factory.states('smart')<Playlist>('playlist', { name: 'My Smart Playlist' }))
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('My Smart Playlist')).toBeNull()
|
2022-07-19 08:19:57 +00:00
|
|
|
})
|
|
|
|
|
2022-10-24 15:27:17 +00:00
|
|
|
it('removes from playlist', async () => {
|
|
|
|
const playlist = factory<Playlist>('playlist')
|
|
|
|
playlistStore.state.playlists.push(playlist)
|
|
|
|
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: `/playlists/${playlist.id}`,
|
|
|
|
screen: 'Playlist'
|
|
|
|
}, { id: String(playlist.id) })
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
const removeSongsMock = this.mock(playlistStore, 'removeSongs')
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Remove from Playlist'))
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(removeSongsMock).toHaveBeenCalledWith(playlist, songs)
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('PLAYLIST_SONGS_REMOVED', playlist, songs)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not have an option to remove from playlist if not on Playlist screen', async () => {
|
|
|
|
await this.router.activateRoute({
|
|
|
|
path: '/songs',
|
|
|
|
screen: 'Songs'
|
|
|
|
})
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('Remove from Playlist')).toBeNull()
|
2022-10-24 15:27:17 +00:00
|
|
|
})
|
|
|
|
|
2022-07-19 08:19:57 +00:00
|
|
|
it('allows edit songs if current user is admin', async () => {
|
2024-01-15 22:26:50 +00:00
|
|
|
await this.beAdmin().renderComponent()
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
// mock after render to ensure that the component is mounted properly
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
2022-12-06 10:28:48 +00:00
|
|
|
await this.user.click(screen.getByText('Edit…'))
|
2022-07-19 08:19:57 +00:00
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_EDIT_SONG_FORM', songs)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not allow edit songs if current user is not admin', async () => {
|
2024-01-15 22:26:50 +00:00
|
|
|
await this.be().renderComponent()
|
2022-12-06 10:28:48 +00:00
|
|
|
expect(screen.queryByText('Edit…')).toBeNull()
|
2022-07-19 08:19:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('has an option to copy shareable URL', async () => {
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.renderComponent(factory<Song>('song'))
|
|
|
|
screen.getByText('Copy Shareable URL')
|
2022-07-19 08:19:57 +00:00
|
|
|
})
|
2022-09-15 09:07:25 +00:00
|
|
|
|
|
|
|
it('deletes song', async () => {
|
|
|
|
const confirmMock = this.mock(DialogBoxStub.value, 'confirm', true)
|
2022-11-19 18:04:21 +00:00
|
|
|
const toasterMock = this.mock(MessageToasterStub.value, 'success')
|
2022-09-15 09:07:25 +00:00
|
|
|
const deleteMock = this.mock(songStore, 'deleteFromFilesystem')
|
2024-01-15 22:26:50 +00:00
|
|
|
await this.beAdmin().renderComponent()
|
2022-09-15 09:07:25 +00:00
|
|
|
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
await this.user.click(screen.getByText('Delete from Filesystem'))
|
2022-09-15 09:07:25 +00:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(confirmMock).toHaveBeenCalled()
|
|
|
|
expect(deleteMock).toHaveBeenCalledWith(songs)
|
2022-11-19 18:04:21 +00:00
|
|
|
expect(toasterMock).toHaveBeenCalledWith('Deleted 5 songs from the filesystem.')
|
2022-09-15 09:07:25 +00:00
|
|
|
expect(emitMock).toHaveBeenCalledWith('SONGS_DELETED', songs)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not have an option to delete songs if current user is not admin', async () => {
|
2024-01-15 22:26:50 +00:00
|
|
|
await this.be().renderComponent()
|
2022-11-29 10:18:58 +00:00
|
|
|
expect(screen.queryByText('Delete from Filesystem')).toBeNull()
|
2022-09-15 09:07:25 +00:00
|
|
|
})
|
2022-12-06 10:28:48 +00:00
|
|
|
|
|
|
|
it('creates playlist from selected songs', async () => {
|
2024-01-15 22:26:50 +00:00
|
|
|
await this.be().renderComponent()
|
2022-12-06 10:28:48 +00:00
|
|
|
|
|
|
|
// mock after render to ensure that the component is mounted properly
|
|
|
|
const emitMock = this.mock(eventBus, 'emit')
|
|
|
|
|
|
|
|
await this.user.click(screen.getByText('New Playlist…'))
|
|
|
|
|
|
|
|
expect(emitMock).toHaveBeenCalledWith('MODAL_SHOW_CREATE_PLAYLIST_FORM', null, songs)
|
|
|
|
})
|
2022-07-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
}
|