Skip to content

Commit

Permalink
change plugins API from object to array to support arbitrary options
Browse files Browse the repository at this point in the history
  • Loading branch information
lzurbriggen committed Aug 14, 2020
1 parent 078e456 commit f507480
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
22 changes: 18 additions & 4 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(app: App, options?: any) {
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: Plugin, options: {} }]
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 { plugin, options } of global.plugins) app.use(plugin, options)
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: Plugin; options?: any }[]
plugins?: (Plugin | [Plugin, ...any[]])[]
config?: Omit<AppConfig, 'isNativeTag'> // isNativeTag is readonly, so we omit it
mixins?: ComponentOptions[]
mocks?: Record<string, any>
Expand Down
2 changes: 1 addition & 1 deletion tests/features/vuex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('vuex', () => {

const wrapper = mount(Foo, {
global: {
plugins: [{ plugin: store }]
plugins: [store]
}
})

Expand Down
44 changes: 39 additions & 5 deletions tests/mountingOptions/global.plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('mounting options: plugins', () => {
}
mount(Component, {
global: {
plugins: [{ plugin: Plugin }]
plugins: [Plugin]
}
})

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

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

Expand All @@ -41,12 +41,46 @@ describe('mounting options: plugins', () => {
}
}
const options = { option1: true }
const testString = 'hello'
mount(Component, {
global: {
plugins: [{ plugin: Plugin, options }]
plugins: [[Plugin, options, testString]]
}
})

expect(installed).toHaveBeenCalledWith(options)
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'
)
})

0 comments on commit f507480

Please sign in to comment.