Skip to content

Commit f507480

Browse files
committed
change plugins API from object to array to support arbitrary options
1 parent 078e456 commit f507480

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

docs/API.md

Lines changed: 18 additions & 4 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 {
193-
static install(app: App, options?: any) {
193+
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: Plugin, options: {} }]
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 { plugin, options } of global.plugins) app.use(plugin, options)
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: Plugin; options?: any }[]
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/features/vuex.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('vuex', () => {
3030

3131
const wrapper = mount(Foo, {
3232
global: {
33-
plugins: [{ plugin: store }]
33+
plugins: [store]
3434
}
3535
})
3636

tests/mountingOptions/global.plugins.spec.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('mounting options: plugins', () => {
1919
}
2020
mount(Component, {
2121
global: {
22-
plugins: [{ plugin: Plugin }]
22+
plugins: [Plugin]
2323
}
2424
})
2525

@@ -30,8 +30,8 @@ describe('mounting options: plugins', () => {
3030
const installed = jest.fn()
3131

3232
class Plugin {
33-
static install(_app: App, options: { option1: boolean }) {
34-
installed(options)
33+
static install(_app: App, ...options) {
34+
installed(...options)
3535
}
3636
}
3737

@@ -41,12 +41,46 @@ describe('mounting options: plugins', () => {
4141
}
4242
}
4343
const options = { option1: true }
44+
const testString = 'hello'
4445
mount(Component, {
4546
global: {
46-
plugins: [{ plugin: Plugin, options }]
47+
plugins: [[Plugin, options, testString]]
4748
}
4849
})
4950

50-
expect(installed).toHaveBeenCalledWith(options)
51+
expect(installed).toHaveBeenCalledWith(options, testString)
5152
})
5253
})
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+
)
86+
})

0 commit comments

Comments
 (0)