2022-05-09 09:59:31 +00:00
|
|
|
import deepmerge from 'deepmerge'
|
|
|
|
import isMobile from 'ismobilejs'
|
|
|
|
import { cleanup, render, RenderOptions } from '@testing-library/vue'
|
2022-05-09 12:25:19 +00:00
|
|
|
import { afterEach, beforeEach, vi } from 'vitest'
|
2022-05-09 09:59:31 +00:00
|
|
|
import { clickaway, droppable, focus } from '@/directives'
|
|
|
|
import { defineComponent, nextTick } from 'vue'
|
2022-05-11 09:12:26 +00:00
|
|
|
import { commonStore, userStore } from '@/stores'
|
|
|
|
import factory from '@/__tests__/factory'
|
2022-05-09 09:59:31 +00:00
|
|
|
|
|
|
|
declare type Methods<T> = { [K in keyof T]: T[K] extends Closure ? K : never; }[keyof T] & (string | symbol);
|
|
|
|
|
|
|
|
export default abstract class ComponentTestCase {
|
2022-05-09 12:25:19 +00:00
|
|
|
private backupMethods = new Map()
|
|
|
|
|
2022-05-09 09:59:31 +00:00
|
|
|
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()
|
2022-05-09 12:25:19 +00:00
|
|
|
this.restoreAllMocks()
|
2022-05-09 09:59:31 +00:00
|
|
|
isMobile.any = false
|
|
|
|
cb && cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:12:26 +00:00
|
|
|
protected actingAs (user: User) {
|
|
|
|
userStore.state.current = user
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
protected actingAsAdmin () {
|
|
|
|
return this.actingAs(factory.states('admin')<User>('user'))
|
|
|
|
}
|
|
|
|
|
2022-05-11 07:21:57 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-09 12:25:19 +00:00
|
|
|
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()
|
|
|
|
}
|
2022-05-09 09:59:31 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 12:25:19 +00:00
|
|
|
|
|
|
|
protected abstract test ()
|
2022-05-09 09:59:31 +00:00
|
|
|
}
|