2022-05-09 09:59:31 +00:00
|
|
|
import isMobile from 'ismobilejs'
|
2022-07-20 21:20:43 +00:00
|
|
|
import { isObject, mergeWith } from 'lodash'
|
2024-04-22 21:56:56 +00:00
|
|
|
import { cleanup, createEvent, fireEvent, 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 { defineComponent, nextTick } from 'vue'
|
2022-05-11 09:12:26 +00:00
|
|
|
import { commonStore, userStore } from '@/stores'
|
2022-10-09 10:53:10 +00:00
|
|
|
import { http } from '@/services'
|
2022-05-11 09:12:26 +00:00
|
|
|
import factory from '@/__tests__/factory'
|
2022-11-19 18:04:21 +00:00
|
|
|
import { DialogBoxKey, MessageToasterKey, OverlayKey, RouterKey } from '@/symbols'
|
|
|
|
import { DialogBoxStub, MessageToasterStub, OverlayStub } from '@/__tests__/stubs'
|
2022-10-08 10:54:25 +00:00
|
|
|
import { routes } from '@/config'
|
|
|
|
import Router from '@/router'
|
2022-11-29 10:18:58 +00:00
|
|
|
import userEvent from '@testing-library/user-event'
|
|
|
|
import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup'
|
2024-04-22 21:56:56 +00:00
|
|
|
import { EventType } from '@testing-library/dom/types/events'
|
2022-07-20 21:20:43 +00:00
|
|
|
|
|
|
|
// A deep-merge function that
|
|
|
|
// - supports symbols as keys (_.merge doesn't)
|
|
|
|
// - supports Vue's Ref type without losing reactivity (deepmerge doesn't)
|
|
|
|
// Credit: https://stackoverflow.com/a/60598589/794641
|
|
|
|
const deepMerge = (first: object, second: object) => {
|
|
|
|
return mergeWith(first, second, (a, b) => {
|
|
|
|
if (!isObject(b)) return b
|
|
|
|
|
2022-09-14 12:12:06 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 21:20:43 +00:00
|
|
|
return Array.isArray(a) ? [...a, ...b] : { ...a, ...b }
|
|
|
|
})
|
|
|
|
}
|
2022-05-09 09:59:31 +00:00
|
|
|
|
2024-04-23 21:01:27 +00:00
|
|
|
const setPropIfNotExists = (obj: object | null, prop: any, value: any) => {
|
|
|
|
if (!obj) return
|
|
|
|
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(obj, prop)) {
|
|
|
|
obj[prop] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:58:38 +00:00
|
|
|
export default abstract class UnitTestCase {
|
2022-10-08 10:54:25 +00:00
|
|
|
protected router: Router
|
2022-11-29 10:18:58 +00:00
|
|
|
protected user: UserEvent
|
2024-04-23 21:01:27 +00:00
|
|
|
private backupMethods = new Map()
|
2022-05-09 12:25:19 +00:00
|
|
|
|
2022-05-09 09:59:31 +00:00
|
|
|
public constructor () {
|
2022-10-08 10:54:25 +00:00
|
|
|
this.router = new Router(routes)
|
2022-10-09 10:53:10 +00:00
|
|
|
this.mock(http, 'request') // prevent actual HTTP requests from being made
|
2022-11-29 10:18:58 +00:00
|
|
|
this.user = userEvent.setup({ delay: null }) // @see https://github.com/testing-library/user-event/issues/833
|
|
|
|
|
2022-05-09 09:59:31 +00:00
|
|
|
this.beforeEach()
|
|
|
|
this.afterEach()
|
|
|
|
this.test()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected beforeEach (cb?: Closure) {
|
|
|
|
beforeEach(() => {
|
2023-12-28 22:32:58 +00:00
|
|
|
commonStore.state.song_length = 10
|
2024-01-04 11:35:36 +00:00
|
|
|
commonStore.state.allows_download = true
|
|
|
|
commonStore.state.uses_i_tunes = true
|
2024-08-29 17:54:58 +00:00
|
|
|
commonStore.state.supports_batch_downloading = true
|
2022-05-09 09:59:31 +00:00
|
|
|
cb && cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected afterEach (cb?: Closure) {
|
|
|
|
afterEach(() => {
|
2024-01-15 22:26:50 +00:00
|
|
|
isMobile.any = false
|
2023-12-28 22:32:58 +00:00
|
|
|
commonStore.state.song_length = 10
|
2022-05-09 09:59:31 +00:00
|
|
|
cleanup()
|
2022-05-09 12:25:19 +00:00
|
|
|
this.restoreAllMocks()
|
2024-01-15 22:26:50 +00:00
|
|
|
this.disablePlusEdition()
|
2022-05-09 09:59:31 +00:00
|
|
|
cb && cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-15 22:26:50 +00:00
|
|
|
protected be (user?: User) {
|
2024-06-01 18:02:27 +00:00
|
|
|
userStore.state.current = user || factory('user')
|
2022-05-11 09:12:26 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2024-01-15 22:26:50 +00:00
|
|
|
protected beAdmin () {
|
2024-06-01 18:02:27 +00:00
|
|
|
factory.states('admin')('user')
|
|
|
|
return this.be(factory.states('admin')('user'))
|
2022-05-11 09:12:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:45:48 +00:00
|
|
|
protected mock<T, M extends MethodOf<Required<T>>> (obj: T, methodName: M, implementation?: any) {
|
2022-05-11 07:21:57 +00:00
|
|
|
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 = {}) {
|
2022-07-20 21:20:43 +00:00
|
|
|
return render(component, deepMerge({
|
2022-05-09 09:59:31 +00:00
|
|
|
global: {
|
|
|
|
directives: {
|
2022-10-25 17:29:56 +00:00
|
|
|
'koel-focus': {},
|
2022-10-28 23:59:04 +00:00
|
|
|
'koel-tooltip': {},
|
2022-12-06 12:14:45 +00:00
|
|
|
'koel-hide-broken-icon': {},
|
|
|
|
'koel-overflow-fade': {}
|
2022-07-16 09:52:39 +00:00
|
|
|
},
|
|
|
|
components: {
|
2023-11-10 15:21:36 +00:00
|
|
|
Icon: this.stub('Icon')
|
2022-05-09 09:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-26 14:46:02 +00:00
|
|
|
}, this.supplyRequiredProvides(options)))
|
|
|
|
}
|
|
|
|
|
2024-01-15 22:26:50 +00:00
|
|
|
protected enablePlusEdition () {
|
|
|
|
commonStore.state.koel_plus = {
|
|
|
|
active: true,
|
|
|
|
short_key: '****-XXXX',
|
|
|
|
customer_name: 'John Doe',
|
|
|
|
customer_email: 'Koel Plus',
|
|
|
|
product_id: 'koel-plus',
|
|
|
|
}
|
2024-04-23 15:20:40 +00:00
|
|
|
|
|
|
|
return this
|
2024-01-15 22:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected disablePlusEdition () {
|
|
|
|
commonStore.state.koel_plus = {
|
|
|
|
active: false,
|
|
|
|
short_key: '',
|
|
|
|
customer_name: '',
|
|
|
|
customer_email: '',
|
|
|
|
product_id: '',
|
|
|
|
}
|
2024-04-23 15:20:40 +00:00
|
|
|
|
|
|
|
return this
|
2024-01-15 22:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 09:59:31 +00:00
|
|
|
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
|
|
|
|
2022-05-14 14:45:48 +00:00
|
|
|
protected setReadOnlyProperty<T> (obj: T, prop: keyof T, value: any) {
|
2022-07-25 18:23:30 +00:00
|
|
|
return Object.defineProperties(obj, {
|
|
|
|
[prop]: {
|
|
|
|
value,
|
|
|
|
configurable: true
|
|
|
|
}
|
2022-05-14 14:45:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-29 10:18:58 +00:00
|
|
|
protected async type (element: HTMLElement, value: string) {
|
|
|
|
await this.user.clear(element)
|
|
|
|
await this.user.type(element, value)
|
|
|
|
}
|
|
|
|
|
2024-04-23 21:01:27 +00:00
|
|
|
protected async trigger (element: HTMLElement, key: EventType | string, options?: {}) {
|
2024-04-22 21:56:56 +00:00
|
|
|
await fireEvent(element, createEvent[key](element, options))
|
|
|
|
}
|
|
|
|
|
2022-05-09 12:25:19 +00:00
|
|
|
protected abstract test ()
|
2024-04-23 21:01:27 +00:00
|
|
|
|
|
|
|
private supplyRequiredProvides (options: RenderOptions) {
|
|
|
|
options.global = options.global || {}
|
|
|
|
options.global.provide = options.global.provide || {}
|
|
|
|
|
|
|
|
setPropIfNotExists(options.global.provide, DialogBoxKey, DialogBoxStub)
|
|
|
|
setPropIfNotExists(options.global.provide, MessageToasterKey, MessageToasterStub)
|
|
|
|
setPropIfNotExists(options.global.provide, OverlayKey, OverlayStub)
|
|
|
|
setPropIfNotExists(options.global.provide, RouterKey, this.router)
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
2022-05-09 09:59:31 +00:00
|
|
|
}
|