Skip to content

Commit e5aee88

Browse files
authored
Merge pull request #178 from lzurbriggen/feature/plugin-options
Feature/plugin options
2 parents 4139219 + f507480 commit e5aee88

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

docs/API.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,37 @@ export default {}
187187
```
188188

189189
```js
190-
test('installs a plugin via `plugins`', () => {
190+
test('installs plugins via `plugins`', () => {
191191
const installed = jest.fn()
192192
class Plugin {
193193
static install() {
194194
installed()
195195
}
196196
}
197+
198+
const installedWithOptions = jest.fn()
199+
class PluginWithOptions {
200+
static install(_app: App, ...args) {
201+
installedWithOptions(...args)
202+
}
203+
}
204+
197205
const Component = {
198-
render() { return h('div') }
206+
render() {
207+
return h('div')
208+
}
199209
}
200210
mount(Component, {
201211
global: {
202-
plugins: [Plugin]
212+
plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']]
203213
}
204214
})
205215

206216
expect(installed).toHaveBeenCalled()
217+
expect(installedWithOptions).toHaveBeenCalledWith(
218+
'argument 1',
219+
'another argument'
220+
)
207221
})
208222
```
209223

src/mount.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,13 @@ export function mount(
312312

313313
// use and plugins from mounting options
314314
if (global.plugins) {
315-
for (const use of global.plugins) app.use(use)
315+
for (const plugin of global.plugins) {
316+
if (Array.isArray(plugin)) {
317+
app.use(plugin[0], ...plugin.slice(1))
318+
continue
319+
}
320+
app.use(plugin)
321+
}
316322
}
317323

318324
// use any mixins from mounting options

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type FindComponentSelector = RefSelector | NameSelector | string
2020
export type FindAllComponentsSelector = NameSelector | string
2121

2222
export type GlobalMountOptions = {
23-
plugins?: Plugin[]
23+
plugins?: (Plugin | [Plugin, ...any[]])[]
2424
config?: Omit<AppConfig, 'isNativeTag'> // isNativeTag is readonly, so we omit it
2525
mixins?: ComponentOptions[]
2626
mocks?: Record<string, any>

tests/mountingOptions/global.plugins.spec.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { h } from 'vue'
1+
import { h, App } from 'vue'
22

33
import { mount } from '../../src'
44

@@ -25,4 +25,62 @@ describe('mounting options: plugins', () => {
2525

2626
expect(installed).toHaveBeenCalled()
2727
})
28+
29+
it('installs a plugin with options `plugins`', () => {
30+
const installed = jest.fn()
31+
32+
class Plugin {
33+
static install(_app: App, ...options) {
34+
installed(...options)
35+
}
36+
}
37+
38+
const Component = {
39+
render() {
40+
return h('div')
41+
}
42+
}
43+
const options = { option1: true }
44+
const testString = 'hello'
45+
mount(Component, {
46+
global: {
47+
plugins: [[Plugin, options, testString]]
48+
}
49+
})
50+
51+
expect(installed).toHaveBeenCalledWith(options, testString)
52+
})
53+
})
54+
55+
test('installs plugins with and without options', () => {
56+
const installed = jest.fn()
57+
class Plugin {
58+
static install() {
59+
installed()
60+
}
61+
}
62+
63+
const installedWithOptions = jest.fn()
64+
class PluginWithOptions {
65+
static install(_app: App, ...args) {
66+
installedWithOptions(...args)
67+
}
68+
}
69+
70+
const Component = {
71+
render() {
72+
return h('div')
73+
}
74+
}
75+
mount(Component, {
76+
global: {
77+
plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']]
78+
}
79+
})
80+
81+
expect(installed).toHaveBeenCalled()
82+
expect(installedWithOptions).toHaveBeenCalledWith(
83+
'argument 1',
84+
'another argument'
85+
)
2886
})

0 commit comments

Comments
 (0)