Skip to content

Commit 80b0f07

Browse files
committed
test: improve vuex test case
1 parent 45f1ca7 commit 80b0f07

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/create-local-vue.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function createLocalVue (): Component {
99
// clone global APIs
1010
Object.keys(Vue).forEach(key => {
1111
if (!instance.hasOwnProperty(key)) {
12-
instance[key] = cloneDeep(Vue[key])
12+
const original = Vue[key]
13+
instance[key] = typeof original === 'object'
14+
? cloneDeep(original)
15+
: original
1316
}
1417
})
1518

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div @click="inc">{{ count }} {{ bar }}</div>
3+
</template>
4+
5+
<script>
6+
export default {
7+
computed: {
8+
count () {
9+
return this.$store.state.count
10+
},
11+
bar () {
12+
return this.$store.state.foo.bar
13+
}
14+
},
15+
methods: {
16+
inc () {
17+
this.$store.commit('increment')
18+
}
19+
}
20+
}
21+
</script>

test/unit/specs/create-local-vue.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Vuex from 'vuex'
33
import VueRouter from 'vue-router'
44
import mount from '~src/mount'
55
import Component from '~resources/components/component.vue'
6+
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
67

78
describe('createLocalVue', () => {
89
it('installs Vuex without polluting global Vue', () => {
@@ -22,6 +23,31 @@ describe('createLocalVue', () => {
2223
expect(typeof freshWrapper.vm.$store).to.equal('undefined')
2324
})
2425

26+
it('Vuex should work properly with local Vue', () => {
27+
const localVue = createLocalVue()
28+
localVue.use(Vuex)
29+
const store = new Vuex.Store({
30+
state: {
31+
count: 0
32+
},
33+
mutations: {
34+
increment (state) {
35+
state.count++
36+
}
37+
},
38+
modules: {
39+
foo: {
40+
state: () => ({ bar: 1 })
41+
}
42+
}
43+
})
44+
const wrapper = mount(ComponentWithVuex, { localVue, store })
45+
expect(wrapper.vm.$store).to.be.an('object')
46+
expect(wrapper.text()).to.equal('0 1')
47+
wrapper.trigger('click')
48+
expect(wrapper.text()).to.equal('1 1')
49+
})
50+
2551
it('installs Router without polluting global Vue', () => {
2652
const localVue = createLocalVue()
2753
localVue.use(VueRouter)

0 commit comments

Comments
 (0)