Skip to content

fix: add stubbed components to ignored elements #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/shared/stub-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ function createStubFromString (
}

function createBlankStub (originalComponent: Component) {
const name = `${originalComponent.name}-stub`

// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(name)
}

return {
...getCoreProperties(originalComponent),
render (h) {
return h(`${originalComponent.name}-stub`)
return h(name)
}
}
}
Expand Down Expand Up @@ -131,10 +138,6 @@ export function createComponentStubs (
}
}
}
// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(`${stub}-stub`)
}
})
}
return components
Expand All @@ -148,11 +151,6 @@ function stubComponents (components: Object, stubbedComponents: Object) {
components[component].name = component
}
stubbedComponents[component] = createBlankStub(components[component])

// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(`${components[component].name}-stub`)
}
})
}

Expand All @@ -163,6 +161,8 @@ export function createComponentStubsForAll (component: Component): Object {
stubComponents(component.components, stubbedComponents)
}

stubbedComponents[component.name] = createBlankStub(component)

let extended = component.extends

// Loop through extended component chains to stub all child components
Expand Down
58 changes: 49 additions & 9 deletions test/specs/shallow-mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import ComponentWithoutName from '~resources/components/component-without-name.v
import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue'
import RecursiveComponent from '~resources/components/recursive-component.vue'
import { vueVersion } from '~resources/utils'
import { describeRunIf } from 'conditional-specs'
import { describeRunIf, itDoNotRunIf } from 'conditional-specs'

describeRunIf(process.env.TEST_ENV !== 'node',
'shallowMount', () => {
let info

beforeEach(() => {
info = sinon.stub(console, 'info')
sinon.stub(console, 'info')
sinon.stub(console, 'error')
})

afterEach(() => {
info.restore()
console.info.restore()
console.error.restore()
})

it('returns new VueWrapper of Vue localVue if no options are passed', () => {
Expand Down Expand Up @@ -61,7 +61,7 @@ describeRunIf(process.env.TEST_ENV !== 'node',
localVue.component('registered-component', ComponentWithLifecycleHooks)
mount(TestComponent, { localVue })

expect(info.callCount).to.equal(4)
expect(console.info.callCount).to.equal(4)
})

it('stubs globally registered components', () => {
Expand All @@ -72,12 +72,52 @@ describeRunIf(process.env.TEST_ENV !== 'node',
shallowMount(Component)
mount(Component)

expect(info.callCount).to.equal(4)
})
expect(console.info.callCount).to.equal(4)
})

itDoNotRunIf(
vueVersion < 2.1,
'adds stubbed components to ignored elements', () => {
const TestComponent = {
template: `
<div>
<router-link>
</router-link>
<custom-component />
</div>
`,
components: {
'router-link': {
template: '<div/>'
},
'custom-component': {
template: '<div/>'
}
}
}
shallowMount(TestComponent)
expect(console.error).not.called
})

itDoNotRunIf(
vueVersion < 2.1,
'handles recursive components', () => {
const TestComponent = {
template: `
<div>
<test-component />
</div>
`,
name: 'test-component'
}
const wrapper = shallowMount(TestComponent)
expect(wrapper.html()).to.contain('<test-component-stub>')
expect(console.error).not.called
})

it('does not call stubbed children lifecycle hooks', () => {
shallowMount(ComponentWithNestedChildren)
expect(info.called).to.equal(false)
expect(console.info.called).to.equal(false)
})

it('stubs extended components', () => {
Expand Down