Skip to content

Commit 06eb3df

Browse files
authored
[docs][zh-cn] synced updates (#1212)
* [docs][zh-cn] synced updates * Update api.md * Update installation.md * Update testing.md
1 parent 57f3cb2 commit 06eb3df

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

docs/zh-cn/api.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ const store = new Vuex.Store({ ...options })
144144

145145
 替换 store 的根状态,仅用状态合并或时光旅行调试。
146146

147-
- **`watch(getter: Function, cb: Function, options?: Object)`**
147+
- **`watch(fn: Function, callback: Function, options?: Object): Function`**
148148

149-
 响应式地监测一个 getter 方法的返回值,当值改变时调用回调函数。Getter 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 `vm.$watch` 方法的参数。
149+
 响应式地侦听 `fn` 的返回值,当值改变时调用回调函数。`fn` 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 `vm.$watch` 方法的参数。
150150

151-
要停止监测,直接调用返回的处理函数
151+
 要停止侦听,调用此方法返回的函数即可停止侦听
152152

153-
- **`subscribe(handler: Function)`**
153+
- **`subscribe(handler: Function): Function`**
154154

155-
注册监听 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
155+
订阅 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
156156

157157
``` js
158158
store.subscribe((mutation, state) => {
@@ -161,9 +161,11 @@ const store = new Vuex.Store({ ...options })
161161
})
162162
```
163163

164+
 要停止订阅,调用此方法返回的函数即可停止订阅。
165+
164166
通常用于插件。[详细介绍](plugins.md)
165167

166-
- **`subscribeAction(handler: Function)`**
168+
- **`subscribeAction(handler: Function): Function`**
167169

168170
> 2.5.0 新增
169171

@@ -176,6 +178,8 @@ const store = new Vuex.Store({ ...options })
176178
})
177179
```
178180

181+
要停止订阅,调用此方法返回的函数即可停止订阅。
182+
179183
该功能常用于插件。[详细介绍](plugins.md)
180184

181185
- **`registerModule(path: string | Array<string>, module: Module, options?: Object)`**

docs/zh-cn/installation.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ Vue.use(Vuex)
3838

3939
当使用全局 script 标签引用 Vuex 时,不需要以上安装过程。
4040

41+
### Promise
42+
43+
Vuex 依赖 [Promise](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Using_promises)。如果你支持的浏览器并没有实现 Promise (比如 IE),那么你可以使用一个 polyfill 的库,例如 [es6-promise](https://github.com/stefanpenner/es6-promise)
44+
45+
你可以通过 CDN 将其引入:
46+
47+
``` html
48+
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
49+
```
50+
51+
然后 `window.Promise` 会自动可用。
52+
53+
如果你喜欢使用诸如 npm 或 Yarn 等包管理器,可以按照下列方式执行安装:
54+
55+
``` bash
56+
npm install es6-promise --save # npm
57+
yarn add es6-promise # Yarn
58+
```
59+
60+
或者更进一步,将下列代码添加到你使用 Vuex 之前的一个地方:
61+
62+
``` js
63+
import 'es6-promise/auto'
64+
```
65+
4166
### 自己构建
4267

4368
如果需要使用 dev 分支下的最新版本,您可以直接从 GitHub 上克隆代码并自己构建。

docs/zh-cn/testing.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('mutations', () => {
4949

5050
### 测试 Action
5151

52-
Action 应对起来略微棘手,因为它们可能需要调用外部的 API。当测试 action 的时候,我们需要增加一个 mocking 服务层——例如,我们可以把 API 调用抽象成服务,然后在测试文件中用 mock 服务回应 API 调用。为了便于解决 mock 依赖,可以用 webpack 和 [inject-loader](https://github.com/plasticine/inject-loader) 打包测试文件。
52+
Action 应对起来略微棘手,因为它们可能需要调用外部的 API。当测试 action 的时候,我们需要增加一个 mocking 服务层——例如,我们可以把 API 调用抽象成服务,然后在测试文件中用 mock 服务回应 API 调用。为了便于解决 mock 依赖,可以用 webpack 和 [inject-loader](https://github.com/plasticine/inject-loader) 打包测试文件。
5353

5454
下面是一个测试异步 action 的例子:
5555

@@ -93,9 +93,9 @@ const testAction = (action, args, state, expectedMutations, done) => {
9393
const mutation = expectedMutations[count]
9494

9595
try {
96-
expect(type).to.equal(mutation.type)
96+
expect(mutation.type).to.equal(type)
9797
if (payload) {
98-
expect(payload).to.deep.equal(mutation.payload)
98+
expect(mutation.payload).to.deep.equal(payload)
9999
}
100100
} catch (error) {
101101
done(error)
@@ -127,6 +127,24 @@ describe('actions', () => {
127127
})
128128
```
129129

130+
如果在测试环境下有可用的 spy (比如通过 [Sinon.JS](http://sinonjs.org/)),你可以使用它们替换辅助函数 `testAction`
131+
132+
``` js
133+
describe('actions', () => {
134+
it('getAllProducts', () => {
135+
const commit = sinon.spy()
136+
const state = {}
137+
138+
actions.getAllProducts({ commit, state })
139+
140+
expect(commit.args).to.deep.equal([
141+
['REQUEST_PRODUCTS'],
142+
['RECEIVE_PRODUCTS', { /* mocked response */ }]
143+
])
144+
})
145+
})
146+
```
147+
130148
### 测试 Getter
131149

132150
如果你的 getter 包含很复杂的计算过程,很有必要测试它们。Getter 的测试与 mutation 一样直截了当。

0 commit comments

Comments
 (0)