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

57 lines
1.6 KiB
TypeScript
Raw Normal View History

import { fireEvent } from '@testing-library/vue'
import { expect, it } from 'vitest'
2022-05-03 17:39:46 +00:00
import factory from '@/__tests__/factory'
import { queueStore, songStore } from '@/stores'
2022-05-03 17:39:46 +00:00
import { playbackService } from '@/services'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-05-03 17:39:46 +00:00
import AlbumTrackListItem from './AlbumTrackListItem.vue'
2022-07-22 17:14:33 +00:00
import { SongsKey } from '@/symbols'
import { ref } from 'vue'
2022-05-03 17:39:46 +00:00
2022-07-22 17:14:33 +00:00
new class extends UnitTestCase {
private renderComponent (matchedSong?: Song) {
const songsToMatchAgainst = factory<Song[]>('song', 10)
const album = factory<Album>('album')
2022-05-03 17:39:46 +00:00
2022-07-22 17:14:33 +00:00
const track = factory<AlbumTrack>('album-track', {
title: 'Fahrstuhl to Heaven',
length: 280
})
2022-05-03 17:39:46 +00:00
2022-07-22 17:14:33 +00:00
const matchMock = this.mock(songStore, 'match', matchedSong)
2022-05-03 17:39:46 +00:00
2022-07-22 17:14:33 +00:00
const rendered = this.render(AlbumTrackListItem, {
props: {
album,
track
},
global: {
provide: {
[SongsKey]: [ref(songsToMatchAgainst)]
}
}
})
expect(matchMock).toHaveBeenCalledWith('Fahrstuhl to Heaven', songsToMatchAgainst)
return rendered
}
protected test () {
2022-07-22 17:14:33 +00:00
it('renders', () => expect(this.renderComponent().html()).toMatchSnapshot())
it('plays', async () => {
2022-07-22 17:14:33 +00:00
const matchedSong = factory<Song>('song')
const queueMock = this.mock(queueStore, 'queueIfNotQueued')
const playMock = this.mock(playbackService, 'play')
2022-07-22 17:14:33 +00:00
const { getByTitle } = this.renderComponent(matchedSong)
await fireEvent.click(getByTitle('Click to play'))
2022-07-22 17:14:33 +00:00
expect(queueMock).toHaveBeenNthCalledWith(1, matchedSong)
expect(playMock).toHaveBeenNthCalledWith(1, matchedSong)
})
}
}