Skip to content

Commit 6766b04

Browse files
posvacexbrayat
authored andcommitted
fix: allow global components to be stubbed
1 parent 5da353f commit 6766b04

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/mount.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,12 @@ export function mount(
386386
}
387387

388388
if (global.components) {
389-
for (const key of Object.keys(global.components))
390-
app.component(key, global.components[key])
389+
for (const key of Object.keys(global.components)) {
390+
// avoid registering components that are stubbed twice
391+
if (!global.stubs[key]) {
392+
app.component(key, global.components[key])
393+
}
394+
}
391395
}
392396

393397
if (global.directives) {

tests/mountingOptions/global.components.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,54 @@ describe('global.components', () => {
1818

1919
expect(wrapper.text()).toBe('Global')
2020
})
21+
22+
it('allows global stubs to take precedence without warning', () => {
23+
const GlobalComponent = {
24+
template: '<div>Global</div>'
25+
}
26+
const spy = jest.spyOn(console, 'warn')
27+
const wrapper = mount(
28+
{
29+
template: '<div><global-component/></div>'
30+
},
31+
{
32+
global: {
33+
components: {
34+
GlobalComponent
35+
},
36+
stubs: { GlobalComponent: true }
37+
}
38+
}
39+
)
40+
41+
expect(spy).not.toHaveBeenCalled()
42+
spy.mockRestore()
43+
expect(wrapper.html()).toBe(
44+
`<div><global-component-stub></global-component-stub></div>`
45+
)
46+
})
47+
48+
it('allows global stubs to be deactivated without warning', () => {
49+
const GlobalComponent = {
50+
template: '<div>Global</div>'
51+
}
52+
const spy = jest.spyOn(console, 'warn')
53+
const wrapper = mount(
54+
{
55+
template: '<div><global-component/></div>'
56+
},
57+
{
58+
global: {
59+
components: {
60+
GlobalComponent
61+
},
62+
stubs: { GlobalComponent: false }
63+
}
64+
}
65+
)
66+
67+
expect(spy).not.toHaveBeenCalled()
68+
spy.mockRestore()
69+
expect(wrapper.text()).toBe('Global')
70+
})
2171
})

0 commit comments

Comments
 (0)