Skip to content

Commit 3b4c136

Browse files
committed
feat: allow opting out of stubbed transition and transition-group
1 parent 5f375c5 commit 3b4c136

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/stubs.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ const createStub = ({ name, props }: StubOptions): ComponentOptions => {
3636
return defineComponent({ name: name || anonName, render, props })
3737
}
3838

39-
const createTransitionStub = ({ props }: StubOptions): ComponentOptions => {
39+
const createTransitionStub = ({
40+
name,
41+
props
42+
}: StubOptions): ComponentOptions => {
4043
const render = (ctx: ComponentPublicInstance) => {
41-
return h('transition-stub', {}, ctx.$slots)
44+
return h(name, {}, ctx.$slots)
4245
}
4346

44-
return defineComponent({ name: 'transition-stub', render, props })
47+
return defineComponent({ name, render, props })
4548
}
4649

4750
const resolveComponentStubByName = (
@@ -93,11 +96,22 @@ export function stubComponents(
9396
// 2. An object of component options, such as { name: 'foo', render: [Function], props: {...} }
9497
// Depending what it is, we do different things.
9598
if (type === Transition && stubs['transition']) {
96-
return [createTransitionStub({ props: undefined }), undefined, children]
99+
return [
100+
createTransitionStub({ name: 'transition-stub', props: undefined }),
101+
undefined,
102+
children
103+
]
97104
}
98105

99106
if (type === TransitionGroup && stubs['transition-group']) {
100-
return [createTransitionStub({ props: undefined }), undefined, children]
107+
return [
108+
createTransitionStub({
109+
name: 'transition-group-stub',
110+
props: undefined
111+
}),
112+
undefined,
113+
children
114+
]
101115
}
102116

103117
if (

tests/mountingOptions/stubs.global.spec.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import ComponentWithoutName from '../components/ComponentWithoutName.vue'
66
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
77

88
describe('mounting options: stubs', () => {
9+
let configStubsSave = config.global.stubs
910
beforeEach(() => {
10-
config.global.stubs = {}
11+
config.global.stubs = configStubsSave
1112
})
1213

1314
afterEach(() => {
14-
config.global.stubs = {}
15+
config.global.stubs = configStubsSave
1516
})
1617

1718
it('handles Array syntax', () => {
@@ -308,6 +309,59 @@ describe('mounting options: stubs', () => {
308309
expect(wrapper.html()).toBe('<foo-bar-stub></foo-bar-stub>')
309310
})
310311

312+
it('stubs transition by default', () => {
313+
const Comp = {
314+
template: `<transition><div id="content" /></transition>`
315+
}
316+
const wrapper = mount(Comp)
317+
318+
expect(wrapper.html()).toBe(
319+
'<transition-stub><div id="content"></div></transition-stub>'
320+
)
321+
})
322+
323+
it('opts out of stubbing transition by default', () => {
324+
const Comp = {
325+
template: `<transition><div id="content" /></transition>`
326+
}
327+
const wrapper = mount(Comp, {
328+
global: {
329+
stubs: {
330+
transition: false
331+
}
332+
}
333+
})
334+
335+
// Vue removes <transition> at run-time and does it's magic, so <transition> should not
336+
// appear in the html when it isn't stubbed.
337+
expect(wrapper.html()).toBe('<div id="content"></div>')
338+
})
339+
340+
it('opts out of stubbing transition-group by default', () => {
341+
const Comp = {
342+
template: `<transition-group><div key="content" id="content" /></transition-group>`
343+
}
344+
const wrapper = mount(Comp, {
345+
global: {
346+
stubs: {
347+
'transition-group': false
348+
}
349+
}
350+
})
351+
352+
// Vue removes <transition-group> at run-time and does it's magic, so <transition-group> should not
353+
// appear in the html when it isn't stubbed.
354+
expect(wrapper.html()).toBe('<div id="content"></div>')
355+
})
356+
357+
it('stubs transition-group by default', () => {
358+
const Comp = {
359+
template: `<transition-group><div key="a" id="content" /></transition-group>`
360+
}
361+
const wrapper = mount(Comp)
362+
expect(wrapper.find('#content').exists()).toBe(true)
363+
})
364+
311365
describe('stub slots', () => {
312366
const Component = {
313367
name: 'Parent',

0 commit comments

Comments
 (0)