Skip to content

Commit

Permalink
fix: add stubbed components to ignored elements (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Jun 14, 2018
1 parent dae0b1c commit 5072274
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
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

0 comments on commit 5072274

Please sign in to comment.