You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
test('increment mutation without passing a value', () => {
152
167
conststore=createVuexStore({ count:20 })
153
168
store.commit('increment')
154
-
155
169
expect(store.state.count).toBe(21)
156
170
})
171
+
172
+
test('increment mutation with a value', () => {
173
+
conststore=createVuexStore({ count:-10 })
174
+
store.commit('increment', 15)
175
+
expect(store.state.count).toBe(5)
176
+
})
157
177
```
158
178
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
+
159
181
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.
0 commit comments