Skip to content

Commit a5f1d60

Browse files
authored
Merge pull request #31 from vuejs/fix/refactor-global-mount-api
Move global app properties, fix #28
2 parents bdd82c6 + 006e31e commit a5f1d60

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

docs/API.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ mount(Component, {
137137
}
138138
})
139139
```
140-
141-
### `mixins`
140+
### Global
141+
You can provide properties to the App instance using the properties under the `global` mount property
142+
143+
### `global.mixins`
142144

143145
Applies mixins via `app.mixin(...)`.
144146

@@ -161,14 +163,16 @@ test('adds a lifecycle mixin', () => {
161163
}
162164

163165
const wrapper = mount(Component, {
164-
mixins: [mixin]
166+
global: {
167+
mixins: [mixin]
168+
}
165169
})
166170

167171
// 'Component was created!' will be logged
168172
})
169173
```
170174

171-
### `plugins`
175+
### `global.plugins`
172176

173177
Installs plugins on the component.
174178

@@ -194,7 +198,9 @@ test('installs a plugin via `plugins`', () => {
194198
render() { return h('div') }
195199
}
196200
mount(Component, {
197-
plugins: [Plugin]
201+
global: {
202+
plugins: [Plugin]
203+
}
198204
})
199205

200206
expect(installed).toHaveBeenCalled()

src/mount.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { h, createApp, VNode, defineComponent } from 'vue'
1+
import {
2+
h,
3+
createApp,
4+
VNode,
5+
defineComponent,
6+
Plugin,
7+
ComponentOptions
8+
} from 'vue'
29

310
import { VueWrapper, createWrapper } from './vue-wrapper'
411
import { createEmitMixin } from './emitMixin'
@@ -13,8 +20,10 @@ interface MountingOptions<Props> {
1320
default?: Slot
1421
[key: string]: Slot
1522
}
16-
plugins?: any[]
17-
mixins?: any[]
23+
global?: {
24+
plugins?: Plugin[]
25+
mixins?: ComponentOptions[]
26+
}
1827
provides?: Record<any, any>
1928
stubs?: Record<string, any>
2029
}
@@ -59,13 +68,13 @@ export function mount<P>(
5968
const vm = createApp(Parent(options && options.props))
6069

6170
// use and plugins from mounting options
62-
if (options?.plugins) {
63-
for (const use of options.plugins) vm.use(use)
71+
if (options?.global?.plugins) {
72+
for (const use of options?.global?.plugins) vm.use(use)
6473
}
6574

6675
// use any mixins from mounting options
67-
if (options?.mixins) {
68-
for (const mixin of options.mixins) vm.mixin(mixin)
76+
if (options?.global?.mixins) {
77+
for (const mixin of options?.global?.mixins) vm.mixin(mixin)
6978
}
7079

7180
// provide any values passed via provides mounting option

tests/mountingOptions/mixins.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ describe('mounting options: mixins', () => {
1111
}
1212
}
1313
const Component = {
14-
render() { return h('div') }
14+
render() {
15+
return h('div')
16+
}
1517
}
1618

1719
mount(Component, {
18-
mixins: [mixin]
20+
global: {
21+
mixins: [mixin]
22+
}
1923
})
2024

2125
expect(createdHook).toHaveBeenCalled()
2226
})
23-
})
27+
})

tests/mountingOptions/plugins.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ import { mount } from '../../src'
55
describe('mounting options: plugins', () => {
66
it('installs a plugin via `plugins`', () => {
77
const installed = jest.fn()
8+
89
class Plugin {
910
static install() {
1011
installed()
1112
}
1213
}
14+
1315
const Component = {
14-
render() { return h('div') }
16+
render() {
17+
return h('div')
18+
}
1519
}
1620
mount(Component, {
17-
plugins: [Plugin]
21+
global: {
22+
plugins: [Plugin]
23+
}
1824
})
1925

2026
expect(installed).toHaveBeenCalled()
2127
})
22-
})
28+
})

0 commit comments

Comments
 (0)