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

60 lines
1.7 KiB
TypeScript
Raw Normal View History

import { fireEvent } 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 { 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', {
name: 'IV',
play_count: 30,
song_count: 10,
length: 123
})
return this.render(AlbumCard, {
props: {
album
}
})
}
protected test () {
it('renders', () => {
const { getByText, getByTestId } = this.renderComponent()
expect(getByTestId('name').textContent).toBe('IV')
getByText(/^10 songs.+02:03.+30 plays$/)
getByTestId('shuffle-album')
getByTestId('download-album')
})
it('downloads', async () => {
const mock = this.mock(downloadService, 'fromAlbum')
const { getByTestId } = this.renderComponent()
await fireEvent.click(getByTestId('download-album'))
expect(mock).toHaveBeenCalledTimes(1)
})
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)
const { getByTestId } = this.renderComponent()
await fireEvent.click(getByTestId('shuffle-album'))
await this.tick()
expect(fetchMock).toHaveBeenCalledWith(album)
expect(shuffleMock).toHaveBeenCalledWith(songs, true)
})
}
}