koel/resources/assets/js/components/album/AlbumCard.spec.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/vue'
import { expect, it } from 'vitest'
import { downloadService, playbackService } from '@/services'
2022-05-02 18:53:19 +00:00
import factory from '@/__tests__/factory'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-05-04 20:47:12 +00:00
import AlbumCard from './AlbumCard.vue'
import { commonStore, songStore } from '@/stores'
2022-05-02 07:21:14 +00:00
2022-05-03 16:51:59 +00:00
let album: Album
2022-05-02 07:21:14 +00:00
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
private renderComponent () {
album = factory<Album>('album', {
id: 42,
name: 'IV',
2022-10-12 09:27:35 +00:00
artist_id: 17,
artist_name: 'Led Zeppelin'
})
return this.render(AlbumCard, {
props: {
album
},
global: {
stubs: {
AlbumArtistThumbnail: this.stub('thumbnail')
}
}
})
}
protected test () {
it('renders', () => expect(this.renderComponent().html()).toMatchSnapshot())
it('downloads', async () => {
const mock = this.mock(downloadService, 'fromAlbum')
this.renderComponent()
await this.user.click(screen.getByTitle('Download all songs in the album IV'))
expect(mock).toHaveBeenCalledTimes(1)
})
it('does not have an option to download if downloading is disabled', async () => {
commonStore.state.allow_download = false
this.renderComponent()
expect(screen.queryByText('Download')).toBeNull()
})
it('shuffles', async () => {
const songs = factory<Song>('song', 10)
const fetchMock = this.mock(songStore, 'fetchForAlbum').mockResolvedValue(songs)
const shuffleMock = this.mock(playbackService, 'queueAndPlay').mockResolvedValue(void 0)
this.renderComponent()
await this.user.click(screen.getByTitle('Shuffle all songs in the album IV'))
await this.tick()
expect(fetchMock).toHaveBeenCalledWith(album)
expect(shuffleMock).toHaveBeenCalledWith(songs, true)
})
}
}