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

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-05-03 16:51:59 +00:00
import AlbumCard from './AlbumCard.vue'
import { cleanup, fireEvent } from '@testing-library/vue'
import { beforeEach, expect, it, vi } from 'vitest'
2022-05-02 07:21:14 +00:00
import { commonStore } from '@/stores'
import { downloadService, playbackService } from '@/services'
2022-05-03 16:51:59 +00:00
import { mockHelper, render } from '@/__tests__/__helpers__'
2022-05-02 18:53:19 +00:00
import factory from '@/__tests__/factory'
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-03 16:51:59 +00:00
beforeEach(() => {
vi.restoreAllMocks()
mockHelper.restoreMocks()
cleanup()
2022-05-02 07:21:14 +00:00
2022-05-03 16:51:59 +00:00
album = factory<Album>('album', {
name: 'IV',
songs: factory<Song>('song', 10)
2022-05-02 07:21:14 +00:00
})
2022-05-03 16:51:59 +00:00
commonStore.state.allowDownload = true
})
it('renders', () => {
const { getByText, getByTestId } = render(AlbumCard, {
props: {
album
}
2022-05-02 07:21:14 +00:00
})
2022-05-03 16:51:59 +00:00
expect(getByTestId('name').innerText).equal('IV')
getByText(/^10 songs • .+ 0 plays$/)
getByTestId('shuffle-album')
getByTestId('download-album')
})
it('downloads', async () => {
const mock = mockHelper.mock(downloadService, 'fromAlbum')
const { getByTestId } = render(AlbumCard, {
props: {
album
}
2022-05-02 07:21:14 +00:00
})
2022-05-03 16:51:59 +00:00
await fireEvent.click(getByTestId('download-album'))
expect(mock).toHaveBeenCalledTimes(1)
})
2022-05-02 07:21:14 +00:00
2022-05-03 16:51:59 +00:00
it('shuffles', async () => {
const mock = mockHelper.mock(playbackService, 'playAllInAlbum')
2022-05-02 07:21:14 +00:00
2022-05-03 16:51:59 +00:00
const { getByTestId } = render(AlbumCard, {
props: {
album
}
2022-05-02 07:21:14 +00:00
})
2022-05-03 16:51:59 +00:00
await fireEvent.click(getByTestId('shuffle-album'))
expect(mock).toHaveBeenCalled()
2022-05-02 07:21:14 +00:00
})