1
0
Эх сурвалжийг харах

[docs][zh-cn] synced updates (#1212)

* [docs][zh-cn] synced updates

* Update api.md

* Update installation.md

* Update testing.md
勾三股四 7 жил өмнө
parent
commit
06eb3df66f

+ 10 - 6
docs/zh-cn/api.md

@@ -144,15 +144,15 @@ const store = new Vuex.Store({ ...options })
 
   替换 store 的根状态,仅用状态合并或时光旅行调试。
 
-- **`watch(getter: Function, cb: Function, options?: Object)`**
+- **`watch(fn: Function, callback: Function, options?: Object): Function`**
 
-  响应式地监测一个 getter 方法的返回值,当值改变时调用回调函数。Getter 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 `vm.$watch` 方法的参数。
+  响应式地侦听 `fn` 的返回值,当值改变时调用回调函数。`fn` 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 `vm.$watch` 方法的参数。
 
-  要停止监测,直接调用返回的处理函数
+  要停止侦听,调用此方法返回的函数即可停止侦听
 
-- **`subscribe(handler: Function)`**
+- **`subscribe(handler: Function): Function`**
 
-  注册监听 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
+  订阅 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
 
   ``` js
   store.subscribe((mutation, state) => {
@@ -161,9 +161,11 @@ const store = new Vuex.Store({ ...options })
   })
   ```
 
+  要停止订阅,调用此方法返回的函数即可停止订阅。
+
   通常用于插件。[详细介绍](plugins.md)
 
-- **`subscribeAction(handler: Function)`**
+- **`subscribeAction(handler: Function): Function`**
 
   > 2.5.0 新增
 
@@ -176,6 +178,8 @@ const store = new Vuex.Store({ ...options })
   })
   ```
 
+  要停止订阅,调用此方法返回的函数即可停止订阅。
+
   该功能常用于插件。[详细介绍](plugins.md)
 
 - **`registerModule(path: string | Array<string>, module: Module, options?: Object)`**

+ 25 - 0
docs/zh-cn/installation.md

@@ -38,6 +38,31 @@ Vue.use(Vuex)
 
 当使用全局 script 标签引用 Vuex 时,不需要以上安装过程。
 
+### Promise
+
+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)。
+
+你可以通过 CDN 将其引入:
+
+``` html
+<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
+```
+
+然后 `window.Promise` 会自动可用。
+
+如果你喜欢使用诸如 npm 或 Yarn 等包管理器,可以按照下列方式执行安装:
+
+``` bash
+npm install es6-promise --save # npm
+yarn add es6-promise # Yarn
+```
+
+或者更进一步,将下列代码添加到你使用 Vuex 之前的一个地方:
+
+``` js
+import 'es6-promise/auto'
+```
+
 ### 自己构建
 
 如果需要使用 dev 分支下的最新版本,您可以直接从 GitHub 上克隆代码并自己构建。

+ 21 - 3
docs/zh-cn/testing.md

@@ -49,7 +49,7 @@ describe('mutations', () => {
 
 ### 测试 Action
 
-Action 应对起来略微棘手,因为它们可能需要调用外部的 API。当测试 action 的时候,我们需要增加一个 mocking 服务层——例如,我们可以把 API 调用抽象成服务,然后在测试文件中用 mock 服务回应 API 调用。为了便于解决 mock 依赖,可以用 webpack 和  [inject-loader](https://github.com/plasticine/inject-loader) 打包测试文件。
+Action 应对起来略微棘手,因为它们可能需要调用外部的 API。当测试 action 的时候,我们需要增加一个 mocking 服务层——例如,我们可以把 API 调用抽象成服务,然后在测试文件中用 mock 服务回应 API 调用。为了便于解决 mock 依赖,可以用 webpack 和 [inject-loader](https://github.com/plasticine/inject-loader) 打包测试文件。
 
 下面是一个测试异步 action 的例子:
 
@@ -93,9 +93,9 @@ const testAction = (action, args, state, expectedMutations, done) => {
     const mutation = expectedMutations[count]
 
     try {
-      expect(type).to.equal(mutation.type)
+      expect(mutation.type).to.equal(type)
       if (payload) {
-        expect(payload).to.deep.equal(mutation.payload)
+        expect(mutation.payload).to.deep.equal(payload)
       }
     } catch (error) {
       done(error)
@@ -127,6 +127,24 @@ describe('actions', () => {
 })
 ```
 
+如果在测试环境下有可用的 spy (比如通过 [Sinon.JS](http://sinonjs.org/)),你可以使用它们替换辅助函数 `testAction`:
+
+``` js
+describe('actions', () => {
+  it('getAllProducts', () => {
+    const commit = sinon.spy()
+    const state = {}
+    
+    actions.getAllProducts({ commit, state })
+    
+    expect(commit.args).to.deep.equal([
+      ['REQUEST_PRODUCTS'],
+      ['RECEIVE_PRODUCTS', { /* mocked response */ }]
+    ])
+  })
+})
+```
+
 ### 测试 Getter
 
 如果你的 getter 包含很复杂的计算过程,很有必要测试它们。Getter 的测试与 mutation 一样直截了当。