Skip to content

Commit 3088ce1

Browse files
committed
refactor: remove lodash mergeWith and replace with custom implementation
1 parent 03b4423 commit 3088ce1

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/utils.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import isString from 'lodash/isString'
2-
import mergeWith from 'lodash/mergeWith'
32

43
import { GlobalMountOptions } from './types'
54

5+
// Deep merge function, adapted from from https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
6+
// Merge a `source` object to a `target` recursively
7+
const merge = (target: object, source: object) => {
8+
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
9+
for (const key of Object.keys(source)) {
10+
if (!target[key]) {
11+
target[key] = source[key]
12+
} else {
13+
if (source[key] instanceof Object) {
14+
Object.assign(source[key], merge(target[key], source[key]))
15+
}
16+
}
17+
}
18+
19+
Object.assign(target || {}, source)
20+
}
21+
622
function mergeGlobalProperties(
723
configGlobal: GlobalMountOptions = {},
824
mountGlobal: GlobalMountOptions = {}
925
): GlobalMountOptions {
10-
return mergeWith(
11-
{},
12-
configGlobal,
13-
mountGlobal,
14-
(objValue, srcValue, key: keyof GlobalMountOptions) => {
15-
switch (key) {
16-
case 'mocks':
17-
case 'provide':
18-
case 'components':
19-
case 'directives':
20-
return { ...objValue, ...srcValue }
21-
case 'plugins':
22-
case 'mixins':
23-
return [...(objValue || []), ...(srcValue || [])].filter(Boolean)
24-
}
25-
}
26-
)
26+
merge(configGlobal, mountGlobal)
27+
return configGlobal
2728
}
2829

2930
export { isString, mergeGlobalProperties }

tests/config.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ describe('config', () => {
2020
}
2121

2222
it('allows setting components globally', () => {
23-
config.global.components = { Hello }
24-
const wrapper1 = mount(Component, { props: { msg: 'Wrapper1' } })
25-
const wrapper2 = mount(Component, { props: { msg: 'Wrapper2' } })
26-
expect(wrapper1.text()).toEqual('Wrapper1 Hello world')
27-
expect(wrapper2.text()).toEqual('Wrapper2 Hello world')
23+
const HelloLocal = {
24+
props: ['msg'],
25+
template: '<div>{{ msg }}</div>'
26+
}
27+
config.global.components = { Hello: HelloLocal }
28+
const wrapper1 = mount(Component, {
29+
props: { msg: 'Wrapper1 Overwritten' }
30+
})
31+
const wrapper2 = mount(Component, {
32+
props: { msg: 'Wrapper2 Overwritten' }
33+
})
34+
expect(wrapper1.text()).toEqual('Wrapper1 Overwritten')
35+
expect(wrapper2.text()).toEqual('Wrapper2 Overwritten')
2836
})
2937

3038
it('allows overwriting globally set component config on a per mount instance', () => {

tests/mountingOptions/stubs.global.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import ComponentWithoutName from '../components/ComponentWithoutName.vue'
66
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
77

88
describe('mounting options: stubs', () => {
9+
beforeEach(() => {
10+
config.global.stubs = {}
11+
})
12+
13+
afterEach(() => {
14+
config.global.stubs = {}
15+
})
16+
917
it('handles Array syntax', () => {
1018
const Foo = {
1119
name: 'Foo',

0 commit comments

Comments
 (0)