koel/resources/assets/js/components/artist/ArtistInfo.spec.ts

85 lines
2.6 KiB
TypeScript
Raw Normal View History

import { expect, it } from 'vitest'
2022-05-04 21:01:35 +00:00
import factory from '@/__tests__/factory'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-07-10 13:50:41 +00:00
import { commonStore, songStore } from '@/stores'
import { fireEvent } from '@testing-library/vue'
import { mediaInfoService, playbackService } from '@/services'
2022-07-10 13:50:41 +00:00
import ArtistInfoComponent from './ArtistInfo.vue'
2022-09-14 16:45:08 +00:00
let artist: Artist
2022-05-04 21:01:35 +00:00
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
2022-07-10 13:50:41 +00:00
private async renderComponent (mode: MediaInfoDisplayMode = 'aside', info?: ArtistInfo) {
commonStore.state.use_last_fm = true
if (info === undefined) {
info = factory<ArtistInfo>('artist-info')
}
artist = factory<Artist>('artist', { name: 'Led Zeppelin' })
const fetchMock = this.mock(mediaInfoService, 'fetchForArtist').mockResolvedValue(info)
const rendered = this.render(ArtistInfoComponent, {
props: {
artist,
mode
},
global: {
stubs: {
ArtistThumbnail: this.stub('thumbnail')
}
2022-07-10 13:50:41 +00:00
}
})
await this.tick(1)
expect(fetchMock).toHaveBeenCalledWith(artist)
return rendered
}
protected test () {
2022-07-10 13:50:41 +00:00
it.each<[MediaInfoDisplayMode]>([['aside'], ['full']])('renders in %s mode', async (mode) => {
const { getByTestId, queryByTestId } = await this.renderComponent(mode)
if (mode === 'aside') {
getByTestId('thumbnail')
} else {
expect(queryByTestId('thumbnail'))
}
2022-07-10 13:50:41 +00:00
expect(getByTestId('artist-info').classList.contains(mode)).toBe(true)
})
it('triggers showing full bio for aside mode', async () => {
2022-09-14 16:45:08 +00:00
const { queryByTestId, getByTestId } = await this.renderComponent('aside')
2022-07-10 13:50:41 +00:00
expect(queryByTestId('full')).toBeNull()
2022-09-14 16:45:08 +00:00
await fireEvent.click(getByTestId('more-btn'))
2022-07-10 13:50:41 +00:00
expect(queryByTestId('summary')).toBeNull()
expect(queryByTestId('full')).not.toBeNull()
})
it('shows full bio for full mode', async () => {
const { queryByTestId } = await this.renderComponent('full')
expect(queryByTestId('full')).not.toBeNull()
expect(queryByTestId('summary')).toBeNull()
expect(queryByTestId('more-btn')).toBeNull()
})
2022-07-10 13:50:41 +00:00
it('plays', async () => {
const songs = factory<Song>('song', 3)
2022-07-10 13:50:41 +00:00
const fetchMock = this.mock(songStore, 'fetchForArtist').mockResolvedValue(songs)
const playMock = this.mock(playbackService, 'queueAndPlay')
const { getByTitle } = await this.renderComponent()
2022-07-10 13:50:41 +00:00
await fireEvent.click(getByTitle('Play all songs by Led Zeppelin'))
await this.tick(2)
2022-07-10 13:50:41 +00:00
expect(fetchMock).toHaveBeenCalledWith(artist)
expect(playMock).toHaveBeenCalledWith(songs)
})
}
}