Skip to content

Commit 441e889

Browse files
committed
docs: improve vuex article
1 parent 6a1f570 commit 441e889

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/guide/vuex.md

+37-15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ This is because Vuex is a plugin. Plugins are applied by calling `app.use` and p
6262
Vue Test Utils allows you to install plugins as well, using the `global.plugins` mounting option.
6363

6464
```js
65+
import { createStore } from 'vuex'
66+
67+
const store = createStore({
68+
state() {
69+
return {
70+
count: 0
71+
}
72+
},
73+
mutations: {
74+
increment(state: any) {
75+
state.count += 1
76+
}
77+
}
78+
})
79+
6580
test('vuex', async () => {
6681
const wrapper = mount(App, {
6782
global: {
@@ -85,7 +100,7 @@ In contrast, a unit test might isolate and test the component and the store sepa
85100
test('vuex using a mock store', async () => {
86101
const $store = {
87102
state: {
88-
count: 1
103+
count: 25
89104
},
90105
commit: jest.fn()
91106
}
@@ -98,7 +113,7 @@ test('vuex using a mock store', async () => {
98113
}
99114
})
100115

101-
expect(wrapper.html()).toContain('Count: 1')
116+
expect(wrapper.html()).toContain('Count: 25')
102117
await wrapper.find('button').trigger('click')
103118
expect($store.commit).toHaveBeenCalled()
104119
})
@@ -133,29 +148,36 @@ test('increment mutation', () => {
133148

134149
## Presetting the Vuex State
135150

136-
Sometimes it can be useful to have the Vuex store in a specific state for a test. One useful technique you can use, other that `global.mocks`, is to create a function that wraps `createStore` and takes an argument to seed the initial state:
151+
Sometimes it can be useful to have the Vuex store in a specific state for a test. One useful technique you can use, other that `global.mocks`, is to create a function that wraps `createStore` and takes an argument to seed the initial state. In this example we extend `increment` to take an additional argument, which will be added on to the `state.count`. If that is not provided, we just increment `state.count` by 1.
137152

138153
```js
139-
test('increment mutation', () => {
140-
const createVuexStore = (initialState) => createStore({
141-
state: {
142-
count: 0,
143-
...initialState
144-
},
145-
mutations: {
146-
increment(state) {
147-
state.count += 1
148-
}
154+
const createVuexStore = (initialState) => createStore({
155+
state: {
156+
count: 0,
157+
...initialState
158+
},
159+
mutations: {
160+
increment(state, value) {
161+
state.count += value
149162
}
150-
})
163+
}
164+
})
151165

166+
test('increment mutation without passing a value', () => {
152167
const store = createVuexStore({ count: 20 })
153168
store.commit('increment')
154-
155169
expect(store.state.count).toBe(21)
156170
})
171+
172+
test('increment mutation with a value', () => {
173+
const store = createVuexStore({ count: -10 })
174+
store.commit('increment', 15)
175+
expect(store.state.count).toBe(5)
176+
})
157177
```
158178

179+
By creating a `createVuexStore` function that takes an initial state, we can easily set the initial state. This allows us to test all the edge cases, while simplifying our tests.
180+
159181
The [Vue Testing Handbook](https://lmiller1990.github.io/vue-testing-handbook/testing-vuex.html) has more examples for testing Vuex. Note: the examples pertain to Vue.js 2 and Vue Test Utils v1. The ideas and concepts are the same, and the Vue Testing Handbook will be updated for Vue.js 3 and Vue Test Utils 2 in the near future.
160182

161183
## Conclusion

0 commit comments

Comments
 (0)