Skip to content

Commit b740217

Browse files
refactor: cleanup merge logic and fix failing tests
1 parent bcb7667 commit b740217

File tree

2 files changed

+30
-49
lines changed

2 files changed

+30
-49
lines changed

src/utils.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@ import { GlobalMountOptions } from './types'
22

33
const isString = (val: unknown): val is string => typeof val === 'string'
44

5+
function mergeStubs(target, source) {
6+
if (source.stubs) {
7+
if (Array.isArray(source.stubs)) {
8+
source.stubs.forEach((x) => (target[x] = true))
9+
} else {
10+
for (const [k, v] of Object.entries(source.stubs)) {
11+
target[k] = v
12+
}
13+
}
14+
}
15+
}
16+
517
function mergeGlobalProperties(
618
configGlobal: GlobalMountOptions = {},
719
mountGlobal: GlobalMountOptions = {}
820
): GlobalMountOptions {
921
const stubs: Record<string, any> = {}
10-
if (configGlobal.stubs) {
11-
if (Array.isArray(configGlobal.stubs)) {
12-
configGlobal.stubs.forEach((x) => (stubs[x] = true))
13-
} else {
14-
for (const [k, v] of Object.entries(configGlobal.stubs)) {
15-
stubs[k] = v
16-
}
17-
}
18-
}
1922

20-
if (mountGlobal.stubs) {
21-
if (mountGlobal.stubs && Array.isArray(mountGlobal.stubs)) {
22-
mountGlobal.stubs.forEach((x) => (stubs[x] = true))
23-
} else {
24-
for (const [k, v] of Object.entries(mountGlobal.stubs)) {
25-
stubs[k] = v
26-
}
27-
}
28-
}
23+
mergeStubs(stubs, configGlobal)
24+
mergeStubs(stubs, mountGlobal)
2925

3026
return {
3127
mixins: [...(configGlobal.mixins || []), ...(mountGlobal.mixins || [])],

tests/config.spec.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,22 @@ describe('config', () => {
109109
})
110110

111111
describe('provide', () => {
112-
it('sets a provide everywhere', () => {
113-
config.global.provide = {
114-
theme: 'dark'
115-
}
116-
const Comp = {
117-
setup() {
118-
const theme = inject('theme')
119-
return () => h('div', theme)
120-
}
112+
config.global.provide = {
113+
theme: 'dark'
114+
}
115+
const Comp = {
116+
setup() {
117+
const theme = inject('theme')
118+
return () => h('div', theme)
121119
}
120+
}
122121

122+
it('sets a provide everywhere', () => {
123123
const wrapper = mount(Comp)
124124
expect(wrapper.html()).toContain('dark')
125125
})
126126

127127
it('overrides with a local provide', () => {
128-
config.global.provide = {
129-
theme: 'dark'
130-
}
131-
const Comp = {
132-
setup() {
133-
const theme = inject('theme')
134-
return () => h('div', theme)
135-
}
136-
}
137-
138128
const wrapper = mount(Comp, {
139129
global: {
140130
provide: {
@@ -202,30 +192,25 @@ describe('config', () => {
202192
}
203193
}
204194

205-
it('sets a stub globally', () => {
195+
beforeEach(() => {
206196
config.global.stubs = {
207197
Foo: {
198+
name: 'Foo',
208199
render() {
209-
return h('div', 'foo stub')
200+
return h('div', 'config foo stub')
210201
}
211202
}
212203
}
204+
})
213205

206+
it('sets a stub globally', () => {
214207
const wrapper = mount(Component)
215208

216209
// once on root, once in the mounted component
217-
expect(wrapper.html()).toContain('foo stub')
210+
expect(wrapper.html()).toContain('config foo stub')
218211
})
219212

220-
xit('overrides config stub with locally defined stub', () => {
221-
config.global.stubs = {
222-
Foo: {
223-
render() {
224-
return h('div', 'config foo stub')
225-
}
226-
}
227-
}
228-
213+
it('overrides config stub with locally defined stub', () => {
229214
const wrapper = mount(Component, {
230215
global: {
231216
stubs: {

0 commit comments

Comments
 (0)