feat(test): add AlbumArtOverlay component tests

This commit is contained in:
Phan An 2022-05-11 09:21:57 +02:00
parent 5cf365879e
commit c4507b1555
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
3 changed files with 55 additions and 3 deletions

View file

@ -2,7 +2,6 @@ import deepmerge from 'deepmerge'
import isMobile from 'ismobilejs'
import { cleanup, render, RenderOptions } from '@testing-library/vue'
import { afterEach, beforeEach, vi } from 'vitest'
import { noop } from '@/utils'
import { clickaway, droppable, focus } from '@/directives'
import { defineComponent, nextTick } from 'vue'
import { commonStore } from '@/stores'
@ -35,8 +34,13 @@ export default abstract class ComponentTestCase {
})
}
protected mock<T, M extends Methods<Required<T>>> (obj: T, methodName: M, implementation: any = noop) {
const mock = vi.fn().mockImplementation(implementation instanceof Function ? implementation : () => implementation)
protected mock<T, M extends Methods<Required<T>>> (obj: T, methodName: M, implementation?: any) {
const mock = vi.fn()
if (implementation !== undefined) {
mock.mockImplementation(implementation instanceof Function ? implementation : () => implementation)
}
this.backupMethods.set([obj, methodName], obj[methodName])
// @ts-ignore

View file

@ -0,0 +1,43 @@
import { expect, it } from 'vitest'
import { albumStore } from '@/stores'
import factory from '@/__tests__/factory'
import ComponentTestCase from '@/__tests__/ComponentTestCase'
import AlbumArtOverlay from './AlbumArtOverlay.vue'
let album: Album
new class extends ComponentTestCase {
private renderComponent () {
album = factory<Album>('album')
return this.render(AlbumArtOverlay, {
props: {
album
}
})
}
protected test () {
it('fetches and displays the album thumbnail', async () => {
const mock = this.mock(albumStore, 'getThumbnail', new Promise(resolve => 'https://localhost/thumb.jpg'))
const { html } = this.renderComponent()
await this.tick(2)
expect(mock).toHaveBeenCalledWith(album)
expect(html()).toMatchSnapshot()
})
it('displays nothing if fetching fails', async () => {
const mock = this.mock(albumStore, 'getThumbnail', () => {
throw new Error()
})
const { html } = this.renderComponent()
await this.tick(2)
expect(mock).toHaveBeenCalledWith(album)
expect(html()).toMatchSnapshot()
})
}
}

View file

@ -0,0 +1,5 @@
// Vitest Snapshot v1
exports[`displays nothing if fetching fails 1`] = `"<div style=\\"background-image: none;\\" data-testid=\\"album-art-overlay\\" data-v-75d06710=\\"\\"></div>"`;
exports[`fetches and displays the album thumbnail 1`] = `"<div style=\\"background-image: none;\\" data-testid=\\"album-art-overlay\\" data-v-75d06710=\\"\\"></div>"`;