forked from vuejs/test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapperFactory.ts
42 lines (37 loc) · 1.18 KB
/
wrapperFactory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { ComponentPublicInstance, App } from 'vue'
import type { DOMWrapper as DOMWrapperType } from './domWrapper'
import type { VueWrapper as VueWrapperType } from './vueWrapper'
export enum WrapperType {
DOMWrapper,
VueWrapper
}
type DOMWrapperFactory = <T extends Node>(
element: T | null | undefined
) => DOMWrapperType<T>
type VueWrapperFactory = <T extends ComponentPublicInstance>(
app: App | null,
vm: T,
setProps?: (props: Record<string, unknown>) => Promise<void>
) => VueWrapperType<T>
const factories: {
[WrapperType.DOMWrapper]?: DOMWrapperFactory
[WrapperType.VueWrapper]?: VueWrapperFactory
} = {}
export function registerFactory(
type: WrapperType.DOMWrapper,
fn: DOMWrapperFactory
): void
export function registerFactory(
type: WrapperType.VueWrapper,
fn: VueWrapperFactory
): void
export function registerFactory(
type: WrapperType.DOMWrapper | WrapperType.VueWrapper,
fn: any
): void {
factories[type] = fn
}
export const createDOMWrapper: DOMWrapperFactory = (element) =>
factories[WrapperType.DOMWrapper]!(element)
export const createVueWrapper: VueWrapperFactory = (app, vm, setProps) =>
factories[WrapperType.VueWrapper]!(app, vm, setProps)