Skip to content

Commit

Permalink
fix: make find work for unnamed components in shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Dec 28, 2017
1 parent 1482c2b commit 60dde1e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export function createComponentStubsForAll (component: Component): Object {
Object.keys(component.components).forEach(c => {
// Remove cached constructor
delete component.components[c]._Ctor
if (!component.components[c].name) {
component.components[c].name = c
}
components[c] = createBlankStub(component.components[c])

// ignoreElements does not exist in Vue 2.0.x
Expand Down
16 changes: 16 additions & 0 deletions test/unit/specs/mount/Wrapper/contains.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Component from '~resources/components/component.vue'
import FunctionalComponent from '~resources/components/functional-component.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import { functionalSFCsSupported } from '~resources/test-utils'
import ComponentWithoutName from '~resources/components/component-without-name.vue'

describe('contains', () => {
it('returns true if wrapper contains element', () => {
Expand Down Expand Up @@ -78,6 +79,21 @@ describe('contains', () => {
expect(wrapper.contains('doesntexist')).to.equal(false)
})

it('returns true if wrapper root Component matches selector', () => {
const TestComponent = {
template: `
<div>
<component-without-name />
</div>
`,
components: {
ComponentWithoutName
}
}
const wrapper = mount(TestComponent)
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
})

it('returns true if wrapper root Component matches selector', () => {
const wrapper = mount(Component)
expect(wrapper.contains(Component)).to.equal(true)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/specs/shallow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Component from '~resources/components/component.vue'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue'
import ComponentWithoutName from '~resources/components/component-without-name.vue'

describe('shallow', () => {
let info
Expand Down Expand Up @@ -73,6 +74,23 @@ describe('shallow', () => {
expect(info.called).to.equal(false)
})

it('works correctly with find, contains, findAll, and is', () => {
const TestComponent = {
template: `
<div>
<component-without-name />
</div>
`,
components: {
ComponentWithoutName
}
}
const wrapper = shallow(TestComponent)
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
expect(wrapper.find(ComponentWithoutName).is(ComponentWithoutName)).to.equal(true)
expect(wrapper.findAll(ComponentWithoutName).length).to.equal(1)
})

it('throws an error when the component fails to mount', () => {
expect(() => shallow({
template: '<div></div>',
Expand Down

0 comments on commit 60dde1e

Please sign in to comment.