fix(test): AlbumArtOverlay tests

This commit is contained in:
Phan An 2022-07-21 11:24:33 +02:00
parent e6dd82503b
commit 44985fa23a
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC

View file

@ -1,44 +1,47 @@
import { expect, it } from 'vitest'
import { albumStore } from '@/stores'
import factory from '@/__tests__/factory'
import UnitTestCase from '@/__tests__/UnitTestCase'
import AlbumArtOverlay from './AlbumArtOverlay.vue'
import { waitFor } from '@testing-library/vue'
let album: Album
let albumId: number
new class extends UnitTestCase {
private renderComponent () {
album = factory<Album>('album')
private async renderComponent () {
albumId = 42
return this.render(AlbumArtOverlay, {
const rendered = this.render(AlbumArtOverlay, {
props: {
album
album: albumId
}
})
await this.tick()
return rendered
}
protected test () {
it('fetches and displays the album thumbnail', async () => {
const mock = this.mock(albumStore, 'fetchThumbnail')
mock.mockResolvedValue('https://localhost/thumb.jpg')
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('https://localhost/thumb.jpg')
const { html } = this.renderComponent()
await this.tick(2)
const { html } = await this.renderComponent()
expect(mock).toHaveBeenCalledWith(album)
expect(html()).toMatchSnapshot()
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(albumId)
expect(html()).toMatchSnapshot()
})
})
it('displays nothing if fetching fails', async () => {
const mock = this.mock(albumStore, 'fetchThumbnail', () => {
throw new Error()
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockRejectedValue(new Error())
const { html } = await this.renderComponent()
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(albumId)
expect(html()).toMatchSnapshot()
})
const { html } = this.renderComponent()
await this.tick(2)
expect(mock).toHaveBeenCalledWith(album)
expect(html()).toMatchSnapshot()
})
}
}