Skip to content

Commit 60dde1e

Browse files
committed
fix: make find work for unnamed components in shallow
1 parent 1482c2b commit 60dde1e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/lib/stub-components.js

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export function createComponentStubsForAll (component: Component): Object {
120120
Object.keys(component.components).forEach(c => {
121121
// Remove cached constructor
122122
delete component.components[c]._Ctor
123+
if (!component.components[c].name) {
124+
component.components[c].name = c
125+
}
123126
components[c] = createBlankStub(component.components[c])
124127

125128
// ignoreElements does not exist in Vue 2.0.x

test/unit/specs/mount/Wrapper/contains.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Component from '~resources/components/component.vue'
55
import FunctionalComponent from '~resources/components/functional-component.vue'
66
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
77
import { functionalSFCsSupported } from '~resources/test-utils'
8+
import ComponentWithoutName from '~resources/components/component-without-name.vue'
89

910
describe('contains', () => {
1011
it('returns true if wrapper contains element', () => {
@@ -78,6 +79,21 @@ describe('contains', () => {
7879
expect(wrapper.contains('doesntexist')).to.equal(false)
7980
})
8081

82+
it('returns true if wrapper root Component matches selector', () => {
83+
const TestComponent = {
84+
template: `
85+
<div>
86+
<component-without-name />
87+
</div>
88+
`,
89+
components: {
90+
ComponentWithoutName
91+
}
92+
}
93+
const wrapper = mount(TestComponent)
94+
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
95+
})
96+
8197
it('returns true if wrapper root Component matches selector', () => {
8298
const wrapper = mount(Component)
8399
expect(wrapper.contains(Component)).to.equal(true)

test/unit/specs/shallow.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Component from '~resources/components/component.vue'
55
import ComponentWithChild from '~resources/components/component-with-child.vue'
66
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
77
import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue'
8+
import ComponentWithoutName from '~resources/components/component-without-name.vue'
89

910
describe('shallow', () => {
1011
let info
@@ -73,6 +74,23 @@ describe('shallow', () => {
7374
expect(info.called).to.equal(false)
7475
})
7576

77+
it('works correctly with find, contains, findAll, and is', () => {
78+
const TestComponent = {
79+
template: `
80+
<div>
81+
<component-without-name />
82+
</div>
83+
`,
84+
components: {
85+
ComponentWithoutName
86+
}
87+
}
88+
const wrapper = shallow(TestComponent)
89+
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
90+
expect(wrapper.find(ComponentWithoutName).is(ComponentWithoutName)).to.equal(true)
91+
expect(wrapper.findAll(ComponentWithoutName).length).to.equal(1)
92+
})
93+
7694
it('throws an error when the component fails to mount', () => {
7795
expect(() => shallow({
7896
template: '<div></div>',

0 commit comments

Comments
 (0)