-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow plugin to be installed in localVue (#497)
* feat: allow plugin to be installed in localVue * chore: fix tests * chore: cr fixes * chore: fix tests and copy version * Update test/use.spec.ts Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> * Update test/use.spec.ts Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> * chore: fix ts Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
4 changed files
with
81 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Vue, { VueConstructor } from 'vue' | ||
|
||
// based on https://github.com/vuejs/vue-test-utils/blob/dev/packages/test-utils/src/create-local-vue.js | ||
|
||
export function createLocalVue(_Vue: VueConstructor = Vue) { | ||
const instance = _Vue.extend() | ||
|
||
Object.keys(_Vue).forEach((key) => { | ||
// @ts-ignore | ||
instance[key] = _Vue[key] | ||
}) | ||
|
||
// @ts-ignore | ||
if (instance._installedPlugins && instance._installedPlugins.length) { | ||
// @ts-ignore | ||
instance._installedPlugins.length = 0 | ||
} | ||
|
||
instance.config = _Vue.config | ||
|
||
const use = instance.use | ||
//@ts-ignore | ||
instance.use = (plugin, ...rest) => { | ||
if (plugin.installed === true) { | ||
plugin.installed = false | ||
} | ||
if (plugin.install && plugin.install.installed === true) { | ||
plugin.install.installed = false | ||
} | ||
use.call(instance, plugin, ...rest) | ||
} | ||
return instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import CompositionApi from '../src' | ||
import { createLocalVue } from './helpers/create-local-vue' | ||
import { mockWarn } from './helpers' | ||
|
||
describe('use', () => { | ||
mockWarn(true) | ||
|
||
it('should allow install in multiple vue', () => { | ||
const localVueOne = createLocalVue() | ||
localVueOne.use(CompositionApi) | ||
|
||
const localVueTwo = createLocalVue() | ||
localVueTwo.use(CompositionApi) | ||
|
||
expect('Another instance of vue installed').toHaveBeenWarned() | ||
}) | ||
|
||
it('should warn installing multiple times', () => { | ||
const localVueOne = createLocalVue() | ||
localVueOne.use(CompositionApi) | ||
|
||
expect(() => { | ||
// vue prevents the same plugin of being installed, this will create a new plugin instance | ||
localVueOne.use({ | ||
install(v) { | ||
CompositionApi.install(v) | ||
}, | ||
}) | ||
}).toThrowError( | ||
'already installed. Vue.use(VueCompositionAPI) should be called only once.' | ||
) | ||
|
||
expect('Another instance of vue installed').toHaveBeenWarned() | ||
}) | ||
}) |