Skip to content

Commit 5f99904

Browse files
committed
[docs] testing & hot-reload
1 parent 1d906fd commit 5f99904

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

docs/en/forms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ vuex: {
2020
message: state => state.obj.message
2121
},
2222
actions: {
23-
updateMessage: (store, e) => {
24-
store.dispatch('UPDATE_MESSAGE', e.target.value)
23+
updateMessage: ({ dispatch }, e) => {
24+
dispatch('UPDATE_MESSAGE', e.target.value)
2525
}
2626
}
2727
}

docs/en/hot-reload.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
# Hot Reloading
22

3-
Vuex supports hot-reloading actions and mutations during development, using Webpack's [Hot Module Replacement API](https://webpack.github.io/docs/hot-module-replacement.html). You can also use it in Browserify with the [browserify-hmr](https://github.com/AgentME/browserify-hmr/) plugin.
3+
Vuex supports hot-reloading mutations, modules, actions and getters during development, using Webpack's [Hot Module Replacement API](https://webpack.github.io/docs/hot-module-replacement.html). You can also use it in Browserify with the [browserify-hmr](https://github.com/AgentME/browserify-hmr/) plugin.
44

5-
It's as simple as calling `store.hotUpdate()` with the new actions and mutations:
5+
For mutations and modules, you need to use the `store.hotUpdate()` API method:
66

77
``` js
8-
// ...
8+
// store.js
9+
import Vue from 'vue'
10+
import Vuex from 'vuex'
11+
import mutations from './mutations'
12+
import moduleA from './modules/a'
13+
14+
Vue.use(Vuex)
15+
16+
const state = { ... }
17+
918
const store = new Vuex.Store({
1019
state,
11-
actions,
12-
mutations
20+
mutations,
21+
modules: {
22+
a: moduleA
23+
}
1324
})
1425

1526
if (module.hot) {
1627
// accept actions and mutations as hot modules
17-
module.hot.accept(['./actions', './mutations'], () => {
28+
module.hot.accept(['./mutations', './modules/a'], () => {
1829
// require the updated modules
1930
// have to add .default here due to babel 6 module output
20-
const newActions = require('./actions').default
2131
const newMutations = require('./mutations').default
32+
const newModuleA = require('./modules/a').default
2233
// swap in the new actions and mutations
2334
store.hotUpdate({
24-
actions: newActions,
25-
mutations: newMutations
35+
mutations: newMutations,
36+
modules: {
37+
a: newModuleA
38+
}
2639
})
2740
})
2841
}
2942
```
43+
44+
You don't need to do anything specific for actions and getters. Webpack's hot module replacement system will "bubble" changes up the dependency chain - and changes in actions and getters will bubble up to the Vue components that imported them. Because Vue components loaded via `vue-loader` are automatically hot-reloadable, these affected components will hot-reload themselves and use the updated actions and getters.

docs/en/testing.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
# Testing
22

3-
Mutations are very straightforward to test, because they are just functions that completely rely on their arguments. Actions can be a bit more tricky because they may call out to external APIs. When testing actions, we usually need to do some level of mocking - for example, we can abstract the API calls into a service and mock that service inside our tests. In order to easily mock dependencies, we can use Webpack and [inject-loader](https://github.com/plasticine/inject-loader) to bundle our test files.
3+
The main parts we want to unit test in Vuex are mutations and actions.
44

5-
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
5+
### Testing Mutations
6+
7+
Mutations are very straightforward to test, because they are just functions that completely rely on their arguments. One trick is that if you are using ES2015 modules and put your mutations inside your `store.js` file, in addition to the default export, you can also export the mutations as a named export:
8+
9+
``` js
10+
const state = { ... }
11+
12+
// export mutations as a named export
13+
export const mutations = { ... }
14+
15+
export default new Vuex.Store({
16+
state,
17+
mutations
18+
})
19+
```
620

721
Example testing a mutation using Mocha + Chai (you can use any framework/assertion libraries you like):
822

@@ -14,7 +28,10 @@ export const INCREMENT = state => state.count++
1428
``` js
1529
// mutations.spec.js
1630
import { expect } from 'chai'
17-
import { INCREMENT } from './mutations'
31+
import { mutations } from './store'
32+
33+
// destructure assign mutations
34+
const { INCREMENT } = mutations
1835

1936
describe('mutations', () => {
2037
it('INCREMENT', () => {
@@ -28,6 +45,10 @@ describe('mutations', () => {
2845
})
2946
```
3047

48+
### Testing Actions
49+
50+
Actions can be a bit more tricky because they may call out to external APIs. When testing actions, we usually need to do some level of mocking - for example, we can abstract the API calls into a service and mock that service inside our tests. In order to easily mock dependencies, we can use Webpack and [inject-loader](https://github.com/plasticine/inject-loader) to bundle our test files.
51+
3152
Example testing an async action:
3253

3354
``` js
@@ -97,7 +118,11 @@ describe('actions', () => {
97118
})
98119
```
99120

100-
### Running in Node
121+
### Running Tests
122+
123+
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
124+
125+
#### Running in Node
101126

102127
Create the following webpack config:
103128

@@ -130,9 +155,13 @@ webpack
130155
mocha test-bundle.js
131156
```
132157

133-
### Running in Browser
158+
#### Running in Browser
134159

135160
1. Install `mocha-loader`
136-
2. Change the `entry` from the Webpack config above to `'mocha!babel!./test.js'`.
161+
2. Change the `entry` from the Webpack config above to `'mocha!babel!./test.js'`.
137162
3. Start `webpack-dev-server` using the config
138163
4. Go to `localhost:8080/webpack-dev-server/test-bundle`.
164+
165+
#### Running in Browser with Karma + karma-webpack
166+
167+
Consult the setup in [vue-loader documentation](http://vuejs.github.io/vue-loader/workflow/testing.html).

0 commit comments

Comments
 (0)