(vm, setProps)
}
diff --git a/tests/find.spec.ts b/tests/find.spec.ts
index 6caab72a1..a4b51f147 100644
--- a/tests/find.spec.ts
+++ b/tests/find.spec.ts
@@ -2,6 +2,7 @@ import { defineComponent, h } from 'vue'
import { mount } from '../src'
import SuspenseComponent from './components/Suspense.vue'
+import Hello from './components/Hello.vue'
describe('find', () => {
it('find using single root node', () => {
diff --git a/tests/findAllComponents.spec.ts b/tests/findAllComponents.spec.ts
new file mode 100644
index 000000000..b13e65460
--- /dev/null
+++ b/tests/findAllComponents.spec.ts
@@ -0,0 +1,28 @@
+import { mount } from '../src'
+import Hello from './components/Hello.vue'
+import { defineComponent } from 'vue'
+
+const compC = defineComponent({
+ name: 'ComponentC',
+ template: 'C
'
+})
+const compB = defineComponent({
+ template: 'TextBeforeTextAfter
',
+ components: { compC }
+})
+const compA = defineComponent({
+ template: '
',
+ components: { compB, Hello }
+})
+
+describe('findAllComponents', () => {
+ it('finds all deeply nested vue components', () => {
+ const wrapper = mount(compA)
+ // find by DOM selector
+ expect(wrapper.findAllComponents('.C')).toHaveLength(2)
+ expect(wrapper.findAllComponents({ name: 'Hello' })[0].text()).toBe(
+ 'Hello world'
+ )
+ expect(wrapper.findAllComponents(Hello)[0].text()).toBe('Hello world')
+ })
+})
diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts
new file mode 100644
index 000000000..499ea1abf
--- /dev/null
+++ b/tests/findComponent.spec.ts
@@ -0,0 +1,89 @@
+import { defineComponent } from 'vue'
+import { mount } from '../src'
+import Hello from './components/Hello.vue'
+
+const compC = defineComponent({
+ name: 'ComponentC',
+ template: 'C
'
+})
+
+const compD = defineComponent({
+ name: 'ComponentD',
+ template: '',
+ components: { compC }
+})
+
+const compB = defineComponent({
+ name: 'component-b',
+ template: `
+
+ TextBefore
+
+ TextAfter
+
+
+
`,
+ components: { compC, compD }
+})
+
+const compA = defineComponent({
+ template: `
+ `,
+ components: { compB, Hello }
+})
+
+describe('findComponent', () => {
+ it('does not find plain dom elements', () => {
+ const wrapper = mount(compA)
+ expect(wrapper.findComponent('.domElement').exists()).toBeFalsy()
+ })
+
+ it('finds component by ref', () => {
+ const wrapper = mount(compA)
+ // find by ref
+ expect(wrapper.findComponent({ ref: 'b' })).toBeTruthy()
+ })
+
+ it('finds component by dom selector', () => {
+ const wrapper = mount(compA)
+ // find by DOM selector
+ expect(wrapper.findComponent('.C').vm).toHaveProperty(
+ '$options.name',
+ 'ComponentC'
+ )
+ })
+
+ it('does allows using complicated DOM selector query', () => {
+ const wrapper = mount(compA)
+ expect(wrapper.findComponent('.B > .C').vm).toHaveProperty(
+ '$options.name',
+ 'ComponentC'
+ )
+ })
+
+ it('finds a component when root of mounted component', async () => {
+ const wrapper = mount(compD)
+ // make sure it finds the component, not its root
+ expect(wrapper.findComponent('.c-as-root-on-d').vm).toHaveProperty(
+ '$options.name',
+ 'ComponentC'
+ )
+ })
+
+ it('finds component by name', () => {
+ const wrapper = mount(compA)
+ expect(wrapper.findComponent({ name: 'Hello' }).text()).toBe('Hello world')
+ expect(wrapper.findComponent({ name: 'ComponentB' }).exists()).toBeTruthy()
+ expect(wrapper.findComponent({ name: 'component-c' }).exists()).toBeTruthy()
+ })
+
+ it('finds component by imported SFC file', () => {
+ const wrapper = mount(compA)
+ expect(wrapper.findComponent(Hello).text()).toBe('Hello world')
+ expect(wrapper.findComponent(compC).text()).toBe('C')
+ })
+})
diff --git a/tests/setProps.spec.ts b/tests/setProps.spec.ts
index c1ffb81fc..cc6241cdf 100644
--- a/tests/setProps.spec.ts
+++ b/tests/setProps.spec.ts
@@ -10,10 +10,10 @@ describe('setProps', () => {
}
const wrapper = mount(Foo, {
props: {
- foo: 'foo'
+ foo: 'bar'
}
})
- expect(wrapper.html()).toContain('foo')
+ expect(wrapper.html()).toContain('bar')
await wrapper.setProps({ foo: 'qux' })
expect(wrapper.html()).toContain('qux')
@@ -44,7 +44,8 @@ describe('setProps', () => {
it('sets component props, and updates DOM when props were not initially passed', async () => {
const Foo = {
props: ['foo'],
- template: `{{ foo }}
`
+ template: `
+ {{ foo }}
`
}
const wrapper = mount(Foo)
expect(wrapper.html()).not.toContain('foo')
@@ -67,7 +68,8 @@ describe('setProps', () => {
this.bar = val
}
},
- template: `{{ bar }}
`
+ template: `
+ {{ bar }}
`
}
const wrapper = mount(Foo)
expect(wrapper.html()).toContain('original-bar')
@@ -118,4 +120,27 @@ describe('setProps', () => {
expect(wrapper.attributes()).toEqual(nonExistentProp)
expect(wrapper.html()).toBe('foo
')
})
+
+ it('allows using only on mounted component', async () => {
+ const Foo = {
+ name: 'Foo',
+ props: ['foo'],
+ template: '{{ foo }}
'
+ }
+ const Baz = {
+ props: ['baz'],
+ template: '
',
+ components: { Foo }
+ }
+
+ const wrapper = mount(Baz, {
+ props: {
+ baz: 'baz'
+ }
+ })
+ const FooResult = wrapper.findComponent({ name: 'Foo' })
+ expect(() => FooResult.setProps({ baz: 'bin' })).toThrowError(
+ 'You can only use setProps on your mounted component'
+ )
+ })
})
diff --git a/tests/vm.spec.ts b/tests/vm.spec.ts
index 2613b3f0b..6f2a3b31d 100644
--- a/tests/vm.spec.ts
+++ b/tests/vm.spec.ts
@@ -5,6 +5,7 @@ import { mount } from '../src'
describe('vm', () => {
it('returns the component vm', () => {
const Component = defineComponent({
+ name: 'VTUComponent',
template: '{{ msg }}
',
setup() {
const msg = 'hello'
diff --git a/yarn.lock b/yarn.lock
index 221472f4c..c8ebd7738 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -268,10 +268,10 @@
dependencies:
"@babel/types" "^7.8.3"
-"@babel/helper-validator-identifier@^7.9.0":
- version "7.9.0"
- resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.0.tgz#ad53562a7fc29b3b9a91bbf7d10397fd146346ed"
- integrity sha512-6G8bQKjOh+of4PV/ThDm/rRqlU7+IGoJuofpagU5GlEl29Vv0RGqqt86ZGRV8ZuSOY3o+8yXl5y782SMcG7SHw==
+"@babel/helper-validator-identifier@^7.9.0", "@babel/helper-validator-identifier@^7.9.5":
+ version "7.9.5"
+ resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.5.tgz#90977a8e6fbf6b431a7dc31752eee233bf052d80"
+ integrity sha512-/8arLKUFq882w4tWGj9JYzRpAlZgiWUJ+dtteNTDqrRBz9Iguck9Rn3ykuBDoUwh2TO4tSAJlrxDUOXWklJe4g==
"@babel/helper-wrap-function@^7.8.3":
version "7.8.3"
@@ -830,7 +830,16 @@
lodash "^4.17.13"
to-fast-properties "^2.0.0"
-"@babel/types@^7.8.6", "@babel/types@^7.9.0":
+"@babel/types@^7.8.6":
+ version "7.9.5"
+ resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.9.5.tgz#89231f82915a8a566a703b3b20133f73da6b9444"
+ integrity sha512-XjnvNqenk818r5zMaba+sLQjnbda31UfUURv3ei0qPQw4u+j2jMyJ5b11y8ZHYTRSI3NnInQkkkRT4fLqqPdHg==
+ dependencies:
+ "@babel/helper-validator-identifier" "^7.9.5"
+ lodash "^4.17.13"
+ to-fast-properties "^2.0.0"
+
+"@babel/types@^7.9.0":
version "7.9.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.9.0.tgz#00b064c3df83ad32b2dbf5ff07312b15c7f1efb5"
integrity sha512-BS9JKfXkzzJl8RluW4JGknzpiUV7ZrvTayM6yfqLTVBEnFtyowVIOu6rqxRd5cVO6yGoWf4T8u8dgK9oB+GCng==
@@ -2097,9 +2106,9 @@ cssstyle@^2.0.0:
cssom "~0.3.6"
csstype@^2.6.8:
- version "2.6.9"
- resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.9.tgz#05141d0cd557a56b8891394c1911c40c8a98d098"
- integrity sha512-xz39Sb4+OaTsULgUERcCk+TJj8ylkL4aSVDQiX/ksxbELSqwkgt4d4RD7fovIdgJGSuNYqwZEiVjYY5l0ask+Q==
+ version "2.6.10"
+ resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.10.tgz#e63af50e66d7c266edb6b32909cfd0aabe03928b"
+ integrity sha512-D34BqZU4cIlMCY93rZHbrq9pjTAQJ3U8S8rfBqjwHxkGPThWFjzZDQpgMJY0QViLxth6ZKYiwFBo14RdN44U/w==
currently-unhandled@^0.4.1:
version "0.4.1"