koel/resources/assets/js/components/ui/AlbumArtOverlay.spec.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

import { expect, it } from 'vitest'
import { albumStore } from '@/stores'
2022-05-13 17:58:38 +00:00
import UnitTestCase from '@/__tests__/UnitTestCase'
import AlbumArtOverlay from './AlbumArtOverlay.vue'
2022-07-21 09:24:33 +00:00
import { waitFor } from '@testing-library/vue'
2022-07-21 09:24:33 +00:00
let albumId: number
2022-05-13 17:58:38 +00:00
new class extends UnitTestCase {
2022-07-21 09:24:33 +00:00
private async renderComponent () {
albumId = 42
2022-07-21 09:24:33 +00:00
const rendered = this.render(AlbumArtOverlay, {
props: {
2022-07-21 09:24:33 +00:00
album: albumId
}
})
2022-07-21 09:24:33 +00:00
await this.tick()
return rendered
}
protected test () {
it('fetches and displays the album thumbnail', async () => {
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockResolvedValue('http://localhost/thumb.jpg')
2022-07-21 09:24:33 +00:00
const { html } = await this.renderComponent()
2022-07-21 09:24:33 +00:00
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(albumId)
expect(html()).toMatchSnapshot()
})
})
it('displays nothing if fetching fails', async () => {
2022-07-21 09:24:33 +00:00
const fetchMock = this.mock(albumStore, 'fetchThumbnail').mockRejectedValue(new Error())
2022-07-21 09:24:33 +00:00
const { html } = await this.renderComponent()
2022-07-21 09:24:33 +00:00
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(albumId)
expect(html()).toMatchSnapshot()
})
})
}
}