Skip to content

Commit ff92cb3

Browse files
committed
feat(mocks): add property as reactive when mocked
1 parent 3c563d1 commit ff92cb3

File tree

4 files changed

+138
-64
lines changed

4 files changed

+138
-64
lines changed

package-lock.json

Lines changed: 107 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"vue": "^2.5.2",
8787
"vue-loader": "^13.0.5",
8888
"vue-router": "^2.7.0",
89-
"vue-template-compiler": "^2.4.4",
89+
"vue-template-compiler": "^2.5.2",
9090
"vuex": "^2.4.1",
9191
"webpack": "^3.6.0",
9292
"webpack-node-externals": "^1.6.0"

src/lib/add-mocks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
2+
import $$Vue from 'vue'
23

34
export default function addMocks (mockedProperties: Object, Vue: Component) {
45
Object.keys(mockedProperties).forEach((key) => {
56
Vue.prototype[key] = mockedProperties[key]
7+
$$Vue.util.defineReactive(Vue, key, mockedProperties[key])
68
})
79
}

test/unit/specs/mount/options/mocks.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ describe('mount.mocks', () => {
1515
expect(wrapper.vm.$route).to.equal($route)
1616
})
1717

18+
it('adds variables to vm when passed as mocks object', () => {
19+
const stub = sinon.stub()
20+
const $reactiveMock = { value: 'value' }
21+
const wrapper = mount({
22+
template: `
23+
<div>
24+
{{value}}
25+
</div>
26+
`,
27+
computed: {
28+
value () {
29+
return this.$reactiveMock.value
30+
}
31+
},
32+
watch: {
33+
value () {
34+
stub()
35+
}
36+
}
37+
}, {
38+
mocks: { $reactiveMock }
39+
})
40+
expect(wrapper.text()).to.contain('value')
41+
$reactiveMock.value = 'changed value'
42+
wrapper.update()
43+
expect(wrapper.text()).to.contain('changed value')
44+
})
45+
1846
it('does not affect global vue class when passed as mocks object', () => {
1947
const $store = { store: true }
2048
const wrapper = mount(Component, {

0 commit comments

Comments
 (0)