Skip to content

Commit 086f8ae

Browse files
committed
refactor: refactor options.intercept route to remove reference to globals
1 parent 0ff0f63 commit 086f8ae

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/lib/add-globals.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/lib/create-instance.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Vue from 'vue'
44
import addSlots from './add-slots'
5-
import addGlobals from './add-globals'
5+
import createInterceptPlugin from './create-intercept-plugin'
66
import addProvide from './add-provide'
77
import { stubComponents } from './stub-components'
88
import { throwError } from './util'
@@ -38,8 +38,10 @@ export default function createConstructor (component: Component, options: Option
3838
const Constructor = vue.extend(component)
3939

4040
if (options.intercept) {
41-
const globals = addGlobals(options.intercept)
42-
Constructor.use(globals)
41+
// creates a plugin that adds properties, and then install on local Constructor
42+
// this does not affect the base Vue class
43+
const interceptPlugin = createInterceptPlugin(options.intercept)
44+
Constructor.use(interceptPlugin)
4345
}
4446

4547
const vm = new Constructor(options)

src/lib/create-intercept-plugin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
3+
export default function createInterceptPlugin (interceptedProperties: Object) {
4+
return {
5+
install: function (Vue: Object) {
6+
Object.keys(interceptedProperties).forEach((key) => {
7+
Vue.prototype[key] = interceptedProperties[key]
8+
})
9+
}
10+
}
11+
}

test/integration/specs/mount/options/intercept.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import mount from '~src/mount'
22
import Component from '~resources/components/component.vue'
33

44
describe('mount.intercept', () => {
5-
it('injects global variables when passed as intercept object', () => {
5+
it('adds variables to vm when passed as intercept object', () => {
66
const $store = { store: true }
77
const $route = { path: 'http://test.com' }
88
const wrapper = mount(Component, {
@@ -14,4 +14,16 @@ describe('mount.intercept', () => {
1414
expect(wrapper.vm.$store).to.equal($store)
1515
expect(wrapper.vm.$route).to.equal($route)
1616
})
17+
18+
it('does not affect global vue class when passed as intercept object', () => {
19+
const $store = { store: true }
20+
const wrapper = mount(Component, {
21+
intercept: {
22+
$store
23+
}
24+
})
25+
expect(wrapper.vm.$store).to.equal($store)
26+
const freshWrapper = mount(Component)
27+
expect(typeof freshWrapper.vm.$store).to.equal('undefined')
28+
})
1729
})

0 commit comments

Comments
 (0)