From f804697854a0ba7d4ebb104daf75b41b8a27984c Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Sat, 26 Sep 2020 18:53:37 +1000 Subject: [PATCH 1/2] fix: allow finding route component --- src/utils/find.ts | 9 +++++++-- src/vueWrapper.ts | 12 +++++++++++- tests/findComponent.spec.ts | 8 +++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/utils/find.ts b/src/utils/find.ts index af56f6b94..e2eec4982 100644 --- a/src/utils/find.ts +++ b/src/utils/find.ts @@ -13,7 +13,10 @@ import { matchName } from './matchName' * @param selector * @return {boolean | ((value: any) => boolean)} */ -function matches(node: VNode, selector: FindAllComponentsSelector): boolean { +export function matches( + node: VNode, + selector: FindAllComponentsSelector +): boolean { // do not return none Vue components if (!node.component) return false @@ -54,7 +57,9 @@ function matches(node: VNode, selector: FindAllComponentsSelector): boolean { } } // we may have one or both missing names - return matchName(selectorName, componentName) + if (selectorName && componentName) { + return matchName(selectorName, componentName) + } } } diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 74f00d8d9..7ff19b026 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -10,7 +10,7 @@ import { } from './types' import { createWrapperError } from './errorWrapper' import { TriggerOptions } from './createDomEvent' -import { find } from './utils/find' +import { find, matches } from './utils/find' import { isFunctionalComponent } from './utils' export class VueWrapper { @@ -164,6 +164,16 @@ export class VueWrapper { }) } + // https://github.com/vuejs/vue-test-utils-next/issues/211 + // VTU v1 supported finding the component mounted itself. + // eg: mount(Comp).findComponent(Comp) + // this is the same as doing `wrapper.vm`, but we keep this behavior for back compat. + if (matches(this.vm.$.vnode, selector)) { + return createWrapper(null, this.vm.$.vnode.component.proxy, { + isFunctionalComponent: false + }) + } + return createWrapperError('VueWrapper') } diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index 618051e4c..0a4e4ddef 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -4,7 +4,7 @@ import Hello from './components/Hello.vue' import ComponentWithoutName from './components/ComponentWithoutName.vue' const compC = defineComponent({ - name: 'ComponentC', + // name: 'ComponentC', template: '
C
' }) @@ -28,6 +28,7 @@ const compB = defineComponent({ }) const compA = defineComponent({ + name: 'A', template: `
@@ -82,6 +83,11 @@ describe('findComponent', () => { expect(wrapper.findComponent({ name: 'component-c' }).exists()).toBeTruthy() }) + it('finds root component', () => { + const wrapper = mount(compA) + expect(wrapper.findComponent(compA).exists()).toBe(true) + }) + it('finds component without a name by using its object definition', () => { const Component = { template: '
', From 910e3f43f7e21757066b214be2dd17cf43396cc0 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Sat, 26 Sep 2020 19:03:33 +1000 Subject: [PATCH 2/2] tests: improve test --- tests/findComponent.spec.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index 0a4e4ddef..d56b1fffa 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -4,7 +4,7 @@ import Hello from './components/Hello.vue' import ComponentWithoutName from './components/ComponentWithoutName.vue' const compC = defineComponent({ - // name: 'ComponentC', + name: 'ComponentC', template: '
C
' }) @@ -83,9 +83,21 @@ describe('findComponent', () => { expect(wrapper.findComponent({ name: 'component-c' }).exists()).toBeTruthy() }) - it('finds root component', () => { - const wrapper = mount(compA) - expect(wrapper.findComponent(compA).exists()).toBe(true) + it('finds root component', async () => { + const Comp = defineComponent({ + name: 'C', + template: ` + + {{ msg }} + `, + data() { + return { msg: 'foo' } + } + }) + const wrapper = mount(Comp) + expect(wrapper.findComponent(Comp).exists()).toBe(true) + await wrapper.find('input').setValue('bar') + expect(wrapper.html()).toContain('bar') }) it('finds component without a name by using its object definition', () => {