-
Notifications
You must be signed in to change notification settings - Fork 20
docs: vuex #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
docs: vuex #41
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,188 @@ | ||
# Vuex | ||
## Testing Vuex | ||
|
||
:) | ||
Vuex is just an implementation detail; no special treatment is required for testing components using Vuex. That said, there are some techniques that might make your tests easier to read and write. We will look at those here. | ||
|
||
https://github.com/vuejs/vue-test-utils-next-docs/issues/8 | ||
This guide you assumed you are familiar with Vuex. Vuex 4 is the version that works with Vue.js 3. Read the docs [here](https://vuex.vuejs.org/). | ||
|
||
## A Simple Example | ||
|
||
Here is a simple Vuex store, and a component that relies on a Vuex store been present: | ||
|
||
```js | ||
import { createStore } from 'vuex' | ||
|
||
const store = createStore({ | ||
state() { | ||
return { | ||
count: 0 | ||
} | ||
}, | ||
mutations: { | ||
increment(state: any) { | ||
state.count += 1 | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
The store simply stores a count, increasing it when the `increment` mutation is committed. This is the component we will be testing: | ||
|
||
```js | ||
const App = { | ||
template: ` | ||
<div> | ||
<button @click="increment" /> | ||
Count: {{ count }} | ||
</div> | ||
`, | ||
computed: { | ||
count() { | ||
return this.$store.state.count | ||
} | ||
}, | ||
methods: { | ||
increment() { | ||
this.$store.commit('increment') | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Testing with a Real Vuex Store | ||
|
||
To full test this component (and the Vuex store) are working, we will click on the `<button>` and assert the count is increased. In your Vue applications, usually in `main.js`, you install Vuex like this: | ||
|
||
```js | ||
const app = createApp(App) | ||
app.use(store) | ||
``` | ||
|
||
This is because Vuex is a plugin. Plugins are applied by calling `app.use` and passing in the plugin. | ||
|
||
Vue Test Utils allows you to install plugins as well, using the `global.plugins` mounting option. | ||
|
||
```js | ||
import { createStore } from 'vuex' | ||
|
||
const store = createStore({ | ||
state() { | ||
return { | ||
count: 0 | ||
} | ||
}, | ||
mutations: { | ||
increment(state: any) { | ||
state.count += 1 | ||
} | ||
} | ||
}) | ||
|
||
test('vuex', async () => { | ||
const wrapper = mount(App, { | ||
global: { | ||
plugins: [store] | ||
} | ||
}) | ||
|
||
await wrapper.find('button').trigger('click') | ||
|
||
expect(wrapper.html()).toContain('Count: 1') | ||
}) | ||
``` | ||
|
||
After installing the plugin, we use `trigger` to click the button and assert that `count` is increased. This kind of test, that covers the interaction between different systems (in this case, the Component and the store), is known as an integration test. | ||
|
||
## Testing with a Mock Store | ||
|
||
In contrast, a unit test might isolate and test the component and the store separately. This can be useful if you have a very large application with a complex store. For this use case, you can mock the parts of the store you are interested in using `global.mocks`: | ||
|
||
```js | ||
test('vuex using a mock store', async () => { | ||
const $store = { | ||
state: { | ||
count: 25 | ||
}, | ||
commit: jest.fn() | ||
} | ||
|
||
const wrapper = mount(App, { | ||
global: { | ||
mocks: { | ||
$store | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toContain('Count: 25') | ||
await wrapper.find('button').trigger('click') | ||
expect($store.commit).toHaveBeenCalled() | ||
}) | ||
``` | ||
|
||
Instead of using a real Vuex store and installing it via `global.plugins`, we created our own mock store, only implementing the parts of Vuex used in the component (in this case, the `state` and `commit` function). | ||
|
||
While it might seem convenient to test the store in isolation, notice that it won't give you any warning if you break your Vuex store. Consider carefully if you want to mock the Vuex store, or use a real one, and understand the trade-offs. | ||
|
||
## Testing Vuex in Isolation | ||
|
||
You may want to test your Vuex mutations or actions in total isolation, especially if they are complex. You don't need Vue Test Utils for this, since a Vuex store is just regular JavaScript. Here's how you might test `increment` mutation without Vue Test Utils: | ||
|
||
```js | ||
test('increment mutation', () => { | ||
const store = createStore({ | ||
state: { | ||
count: 0 | ||
}, | ||
mutations: { | ||
increment(state) { | ||
state.count += 1 | ||
} | ||
} | ||
}) | ||
|
||
store.commit('increment') | ||
|
||
expect(store.state.count).toBe(1) | ||
}) | ||
``` | ||
|
||
## Presetting the Vuex State | ||
|
||
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. | ||
|
||
```js | ||
const createVuexStore = (initialState) => createStore({ | ||
state: { | ||
count: 0, | ||
...initialState | ||
}, | ||
mutations: { | ||
increment(state, value) { | ||
state.count += value | ||
} | ||
} | ||
}) | ||
|
||
test('increment mutation without passing a value', () => { | ||
const store = createVuexStore({ count: 20 }) | ||
store.commit('increment') | ||
expect(store.state.count).toBe(21) | ||
}) | ||
|
||
test('increment mutation with a value', () => { | ||
const store = createVuexStore({ count: -10 }) | ||
store.commit('increment', 15) | ||
expect(store.state.count).toBe(5) | ||
}) | ||
``` | ||
|
||
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. | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to plug my own guide lol. I will have it up to date with VTU v2 soon :D |
||
|
||
## Conclusion | ||
|
||
- Use `global.plugins` to install Vuex as a plugin | ||
- Use `global.mocks` to mock a global object, such as Vuex, for advanced use cases | ||
- Consider testing complex Vuex mutations and actions in isolation | ||
- Wrap `createStore` with a function that takes an argument to set up specific test scenarios |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add the imports here, to show that we're importing and using the real store, as the component does 👌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually just writing them all in one file - I'll make this more clear.