Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,37 @@ export default {}
```

```js
test('installs a plugin via `plugins`', () => {
test('installs plugins via `plugins`', () => {
const installed = jest.fn()
class Plugin {
static install() {
installed()
}
}

const installedWithOptions = jest.fn()
class PluginWithOptions {
static install(_app: App, ...args) {
installedWithOptions(...args)
}
}

const Component = {
render() { return h('div') }
render() {
return h('div')
}
}
mount(Component, {
global: {
plugins: [Plugin]
plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']]
}
})

expect(installed).toHaveBeenCalled()
expect(installedWithOptions).toHaveBeenCalledWith(
'argument 1',
'another argument'
)
})
```

Expand Down
8 changes: 7 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,13 @@ export function mount(

// use and plugins from mounting options
if (global.plugins) {
for (const use of global.plugins) app.use(use)
for (const plugin of global.plugins) {
if (Array.isArray(plugin)) {
app.use(plugin[0], ...plugin.slice(1))
continue
}
app.use(plugin)
}
}

// use any mixins from mounting options
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type FindComponentSelector = RefSelector | NameSelector | string
export type FindAllComponentsSelector = NameSelector | string

export type GlobalMountOptions = {
plugins?: Plugin[]
plugins?: (Plugin | [Plugin, ...any[]])[]
config?: Omit<AppConfig, 'isNativeTag'> // isNativeTag is readonly, so we omit it
mixins?: ComponentOptions[]
mocks?: Record<string, any>
Expand Down
60 changes: 59 additions & 1 deletion tests/mountingOptions/global.plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h } from 'vue'
import { h, App } from 'vue'

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

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

expect(installed).toHaveBeenCalled()
})

it('installs a plugin with options `plugins`', () => {
const installed = jest.fn()

class Plugin {
static install(_app: App, ...options) {
installed(...options)
}
}

const Component = {
render() {
return h('div')
}
}
const options = { option1: true }
const testString = 'hello'
mount(Component, {
global: {
plugins: [[Plugin, options, testString]]
}
})

expect(installed).toHaveBeenCalledWith(options, testString)
})
})

test('installs plugins with and without options', () => {
const installed = jest.fn()
class Plugin {
static install() {
installed()
}
}

const installedWithOptions = jest.fn()
class PluginWithOptions {
static install(_app: App, ...args) {
installedWithOptions(...args)
}
}

const Component = {
render() {
return h('div')
}
}
mount(Component, {
global: {
plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']]
}
})

expect(installed).toHaveBeenCalled()
expect(installedWithOptions).toHaveBeenCalledWith(
'argument 1',
'another argument'
)
})