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

88 lines
2.6 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/vue'
import { expect, it } from 'vitest'
2022-05-03 16:51:59 +00:00
import factory from '@/__tests__/factory'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
2022-07-10 09:31:55 +00:00
import { commonStore, songStore } from '@/stores'
import { mediaInfoService, playbackService } from '@/services'
2022-07-10 13:50:41 +00:00
import AlbumInfoComponent from './AlbumInfo.vue'
2022-07-10 09:31:55 +00:00
let album: Album
2022-05-03 16:51:59 +00:00
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
2022-07-10 09:31:55 +00:00
private async renderComponent (mode: MediaInfoDisplayMode = 'aside', info?: AlbumInfo) {
commonStore.state.use_last_fm = true
if (info === undefined) {
info = factory<AlbumInfo>('album-info')
}
album = factory<Album>('album', { name: 'IV' })
const fetchMock = this.mock(mediaInfoService, 'fetchForAlbum').mockResolvedValue(info)
2022-07-10 13:50:41 +00:00
const rendered = this.render(AlbumInfoComponent, {
2022-07-10 09:31:55 +00:00
props: {
album,
mode
},
global: {
stubs: {
TrackList: this.stub(),
AlbumThumbnail: this.stub('thumbnail')
}
2022-07-10 09:31:55 +00:00
}
})
await this.tick(1)
expect(fetchMock).toHaveBeenCalledWith(album)
return rendered
}
protected test () {
it.each<[MediaInfoDisplayMode]>([['aside'], ['full']])('renders in %s mode', async (mode) => {
await this.renderComponent(mode)
2022-05-03 16:51:59 +00:00
screen.getByTestId('album-info-tracks')
2022-07-10 09:31:55 +00:00
if (mode === 'aside') {
screen.getByTestId('thumbnail')
} else {
expect(screen.queryByTestId('thumbnail')).toBeNull()
}
expect(screen.getByTestId('album-info').classList.contains(mode)).toBe(true)
2022-07-10 09:31:55 +00:00
})
it('triggers showing full wiki for aside mode', async () => {
await this.renderComponent('aside')
expect(screen.queryByTestId('full')).toBeNull()
2022-07-10 09:31:55 +00:00
await this.user.click(screen.getByRole('button', { name: 'Full Wiki' }))
2022-05-03 16:51:59 +00:00
expect(screen.queryByTestId('summary')).toBeNull()
screen.getByTestId('full')
})
2022-05-03 16:51:59 +00:00
2022-07-10 09:31:55 +00:00
it('shows full wiki for full mode', async () => {
await this.renderComponent('full')
2022-07-10 09:31:55 +00:00
screen.getByTestId('full')
expect(screen.queryByTestId('summary')).toBeNull()
expect(screen.queryByRole('button', { name: 'Full Wiki' })).toBeNull()
2022-07-10 09:31:55 +00:00
})
it('plays', async () => {
const songs = factory<Song>('song', 3)
2022-07-10 09:31:55 +00:00
const fetchMock = this.mock(songStore, 'fetchForAlbum').mockResolvedValue(songs)
const playMock = this.mock(playbackService, 'queueAndPlay')
await this.renderComponent()
2022-07-10 09:31:55 +00:00
await this.user.click(screen.getByTitle('Play all songs in IV'))
2022-07-10 09:31:55 +00:00
await this.tick(2)
expect(fetchMock).toHaveBeenCalledWith(album)
expect(playMock).toHaveBeenCalledWith(songs)
})
}
}