|
1 | 1 | import { ComponentPublicInstance, nextTick, App } from 'vue'
|
2 | 2 | import { ShapeFlags } from '@vue/shared'
|
3 |
| -import { config } from './config' |
4 | 3 |
|
| 4 | +import { config } from './config' |
5 | 5 | import { DOMWrapper } from './domWrapper'
|
6 |
| -import { FindAllComponentsSelector, FindComponentSelector } from './types' |
| 6 | +import { |
| 7 | + FindAllComponentsSelector, |
| 8 | + FindComponentSelector, |
| 9 | + VueWrapperMeta |
| 10 | +} from './types' |
7 | 11 | import { createWrapperError } from './errorWrapper'
|
8 | 12 | import { TriggerOptions } from './createDomEvent'
|
9 | 13 | import { find } from './utils/find'
|
| 14 | +import { isFunctionalComponent } from './utils' |
10 | 15 |
|
11 | 16 | export class VueWrapper<T extends ComponentPublicInstance> {
|
12 | 17 | private componentVM: T
|
13 | 18 | private rootVM: ComponentPublicInstance
|
14 | 19 | private __app: App | null
|
15 | 20 | private __setProps: ((props: Record<string, any>) => void) | undefined
|
| 21 | + private __isFunctionalComponent: boolean |
16 | 22 |
|
17 | 23 | constructor(
|
18 | 24 | app: App | null,
|
19 | 25 | vm: ComponentPublicInstance,
|
20 |
| - setProps?: (props: Record<string, any>) => void |
| 26 | + setProps?: (props: Record<string, any>) => void, |
| 27 | + meta?: VueWrapperMeta |
21 | 28 | ) {
|
22 | 29 | this.__app = app
|
23 | 30 | this.rootVM = vm.$root!
|
24 | 31 | this.componentVM = vm as T
|
25 | 32 | this.__setProps = setProps
|
| 33 | + this.__isFunctionalComponent = meta.isFunctionalComponent |
26 | 34 | // plugins hook
|
27 | 35 | config.plugins.VueWrapper.extend(this)
|
28 | 36 | }
|
@@ -71,10 +79,17 @@ export class VueWrapper<T extends ComponentPublicInstance> {
|
71 | 79 | emitted<T = unknown>(): Record<string, T[]>
|
72 | 80 | emitted<T = unknown>(eventName?: string): T[]
|
73 | 81 | emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
|
| 82 | + if (this.__isFunctionalComponent) { |
| 83 | + console.warn( |
| 84 | + '[Vue Test Utils]: capture events emitted from functional components is currently not supported.' |
| 85 | + ) |
| 86 | + } |
| 87 | + |
74 | 88 | if (eventName) {
|
75 | 89 | const emitted = (this.vm['__emitted'] as Record<string, T[]>)[eventName]
|
76 | 90 | return emitted
|
77 | 91 | }
|
| 92 | + |
78 | 93 | return this.vm['__emitted'] as Record<string, T[]>
|
79 | 94 | }
|
80 | 95 |
|
@@ -136,13 +151,17 @@ export class VueWrapper<T extends ComponentPublicInstance> {
|
136 | 151 | if (typeof selector === 'object' && 'ref' in selector) {
|
137 | 152 | const result = this.vm.$refs[selector.ref]
|
138 | 153 | if (result) {
|
139 |
| - return createWrapper(null, result as T) |
| 154 | + return createWrapper(null, result as T, { |
| 155 | + isFunctionalComponent: isFunctionalComponent(result) |
| 156 | + }) |
140 | 157 | }
|
141 | 158 | }
|
142 | 159 |
|
143 | 160 | const result = find(this.vm.$.subTree, selector)
|
144 | 161 | if (result.length) {
|
145 |
| - return createWrapper(null, result[0]) |
| 162 | + return createWrapper(null, result[0], { |
| 163 | + isFunctionalComponent: isFunctionalComponent(result) |
| 164 | + }) |
146 | 165 | }
|
147 | 166 |
|
148 | 167 | return createWrapperError('VueWrapper')
|
@@ -178,7 +197,11 @@ export class VueWrapper<T extends ComponentPublicInstance> {
|
178 | 197 | }
|
179 | 198 |
|
180 | 199 | findAllComponents(selector: FindAllComponentsSelector): VueWrapper<T>[] {
|
181 |
| - return find(this.vm.$.subTree, selector).map((c) => createWrapper(null, c)) |
| 200 | + return find(this.vm.$.subTree, selector).map((c) => |
| 201 | + createWrapper(null, c, { |
| 202 | + isFunctionalComponent: isFunctionalComponent(c) |
| 203 | + }) |
| 204 | + ) |
182 | 205 | }
|
183 | 206 |
|
184 | 207 | findAll<K extends keyof HTMLElementTagNameMap>(
|
@@ -228,7 +251,8 @@ export class VueWrapper<T extends ComponentPublicInstance> {
|
228 | 251 | export function createWrapper<T extends ComponentPublicInstance>(
|
229 | 252 | app: App | null,
|
230 | 253 | vm: ComponentPublicInstance,
|
| 254 | + meta: VueWrapperMeta, |
231 | 255 | setProps?: (props: Record<string, any>) => void
|
232 | 256 | ): VueWrapper<T> {
|
233 |
| - return new VueWrapper<T>(app, vm, setProps) |
| 257 | + return new VueWrapper<T>(app, vm, setProps, meta) |
234 | 258 | }
|
0 commit comments