koel/resources/assets/js/__tests__/ComponentTestCase.ts
2022-05-09 14:25:19 +02:00

78 lines
2 KiB
TypeScript

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'
declare type Methods<T> = { [K in keyof T]: T[K] extends Closure ? K : never; }[keyof T] & (string | symbol);
export default abstract class ComponentTestCase {
private backupMethods = new Map()
public constructor () {
this.beforeEach()
this.afterEach()
this.test()
}
protected beforeEach (cb?: Closure) {
beforeEach(() => {
commonStore.state.allowDownload = true
commonStore.state.useiTunes = true
cb && cb()
})
}
protected afterEach (cb?: Closure) {
afterEach(() => {
cleanup()
this.restoreAllMocks()
isMobile.any = false
cb && cb()
})
}
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)
this.backupMethods.set([obj, methodName], obj[methodName])
// @ts-ignore
obj[methodName] = mock
return mock
}
protected restoreAllMocks () {
this.backupMethods.forEach((fn, [obj, methodName]) => (obj[methodName] = fn))
this.backupMethods = new Map()
}
protected render (component: any, options: RenderOptions = {}) {
return render(component, deepmerge({
global: {
directives: {
'koel-clickaway': clickaway,
'koel-focus': focus,
'koel-droppable': droppable
}
}
}, options))
}
protected stub (testId = 'stub') {
return defineComponent({
template: `<br data-testid="${testId}"/>`
})
}
protected async tick (count = 1) {
for (let i = 0; i < count; ++i) {
await nextTick()
}
}
protected abstract test ()
}