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