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

94 lines
2.7 KiB
TypeScript
Raw Normal View History

import { screen, waitFor } from '@testing-library/vue'
2022-07-10 17:15:56 +00:00
import { expect, it } from 'vitest'
import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-07-10 17:33:53 +00:00
import { albumStore, commonStore, songStore } from '@/stores'
import { downloadService } from '@/services'
import { eventBus } from '@/utils'
2022-07-11 17:35:58 +00:00
import AlbumScreen from './AlbumScreen.vue'
2022-07-10 17:33:53 +00:00
let album: Album
2022-07-10 17:15:56 +00:00
new class extends UnitTestCase {
2022-10-21 20:06:43 +00:00
private async renderComponent () {
2022-07-10 17:15:56 +00:00
commonStore.state.use_last_fm = true
2022-07-10 17:33:53 +00:00
album = factory<Album>('album', {
id: 42,
2022-07-10 17:15:56 +00:00
name: 'Led Zeppelin IV',
2022-07-10 17:33:53 +00:00
artist_id: 123,
artist_name: 'Led Zeppelin'
2022-07-10 17:15:56 +00:00
})
const resolveAlbumMock = this.mock(albumStore, 'resolve').mockResolvedValue(album)
const songs = factory<Song>('song', 13)
2022-07-10 17:15:56 +00:00
const fetchSongsMock = this.mock(songStore, 'fetchForAlbum').mockResolvedValue(songs)
await this.router.activateRoute({
path: 'albums/42',
screen: 'Album'
}, { id: '42' })
this.render(AlbumScreen, {
2022-07-10 17:33:53 +00:00
global: {
stubs: {
SongList: this.stub('song-list'),
AlbumCard: this.stub('album-card'),
AlbumInfo: this.stub('album-info')
2022-07-10 17:33:53 +00:00
}
2022-07-10 17:15:56 +00:00
}
})
await waitFor(() => {
expect(resolveAlbumMock).toHaveBeenCalledWith(album.id)
expect(fetchSongsMock).toHaveBeenCalledWith(album.id)
})
await this.tick(2)
2022-07-10 17:15:56 +00:00
}
protected test () {
2022-07-10 17:33:53 +00:00
it('downloads', async () => {
const downloadMock = this.mock(downloadService, 'fromAlbum')
await this.renderComponent()
2022-07-10 17:33:53 +00:00
await this.user.click(screen.getByRole('button', { name: 'Download All' }))
2022-07-10 17:33:53 +00:00
expect(downloadMock).toHaveBeenCalledWith(album)
})
it('goes back to list if album is deleted', async () => {
const goMock = this.mock(this.router, 'go')
2022-07-10 17:33:53 +00:00
const byIdMock = this.mock(albumStore, 'byId', null)
await this.renderComponent()
eventBus.emit('SONGS_UPDATED')
await waitFor(() => {
expect(byIdMock).toHaveBeenCalledWith(album.id)
expect(goMock).toHaveBeenCalledWith('albums')
})
2022-07-10 17:15:56 +00:00
})
it('shows the song list', async () => {
await this.renderComponent()
screen.getByTestId('song-list')
})
it('shows other albums from the same artist', async () => {
const albums = factory<Album>('album', 3)
albums.push(album)
const fetchMock = this.mock(albumStore, 'fetchForArtist').mockResolvedValue(albums)
await this.renderComponent()
await this.user.click(screen.getByRole('radio', { name: 'Other Albums' }))
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(album.artist_id)
expect(screen.getAllByTestId('album-card')).toHaveLength(3) // current album is excluded
})
})
2022-07-10 17:15:56 +00:00
}
}