10000 [doc] Add more descriptions of getters (#340) · padcom/vuejs.org@9fddf0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fddf0b

Browse files
ktsnyyx990803
authored andcommitted
[doc] Add more descriptions of getters (vuejs#340)
* add description of the 2nd and 3rd arguments of getters * add example test code of getters * tweak description of module getters * fix small grammatical error
1 parent fe75f42 commit 9fddf0b

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

docs/en/getters.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ computed: {
1313

1414
If more than one component needs to make use of this, we have to either duplicate the function, or extract it into a shared helper and import it in multiple places - both are less than ideal.
1515

16-
Vuex allows us to define "getters" in the store (think of them as computed properties for stores):
16+
Vuex allows us to define "getters" in the store (think of them as computed properties for stores). Getters will receive the state as their 1st argument:
1717

1818
``` js
1919
const store = new Vuex.Store({
@@ -24,15 +24,30 @@ const store = new Vuex.Store({
2424
]
2525
},
2626
getters: {
27-
doneTodosCount: state => {
28-
return state.todos.filter(todo => todo.done).length
27+
doneTodos: state => {
28+
return state.todos.filter(todo => todo.done)
2929
}
3030
}
3131
})
3232
```
3333

3434
The getters will be exposed on the `store.getters` object:
3535

36+
``` js
37+
store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]
38+
```
39+
40+
Getters will also receive other getters as the 2nd argument:
41+
42+
``` js
43+
getters: {
44+
// ...
45+
doneTodosCount: (state, getters) => {
46+
return getters.doneTodos.length
47+
}
48+
}
49+
```
50+
3651
``` js
3752
store.getters.doneTodosCount // -> 1
3853
```

docs/en/modules.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ const moduleA = {
6969
}
7070
```
7171

72+
Also, inside module getters, the root state will be exposed as their 3rd argument:
73+
74+
``` js
75+
const moduleA = {
76+
// ...
77+
getters: {
78+
sumWithRootCount (state, getters, rootState) {
79+
return state.count + rootState.count
80+
}
81+
}
82+
}
83+
```
84+
7285
### Namespacing
7386

7487
Note that actions, mutations and getters inside modules are still registered under the **global namespace** - this allows multiple modules to react to the same mutation/action type. You can namespace the module assets yourself to avoid name clashing by prefixing or suffixing their names. And you probably should if you are writing a reusable Vuex module that will be used in unknown environments. For example, we want to create a `todos` module:

docs/en/testing.md

Lines changed: 47 additions & 0 deletions
< 10000 /div>
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,53 @@ describe('actions', () => {
122122
})
123123
```
124124

125+
### Testing Getters
126+
127+
If your getters have complicated computation, it is worth testing them. Getters are also very straightforward to test as same reason as mutations.
128+
129+
Example testing a getter:
130+
131+
``` js
132+
// getters.js
133+
export const getters = {
134+
filteredProducts (state, { filterCategory }) {
135+
return state.products.filter(product => {
136+
return product.category === filterCategory
137+
})
138+
}
139+
}
140+
```
141+
142+
``` js
143+
// getters.spec.js
144+
import { expect } from 'chai'
145+
import { getters } from './getters'
146+
147+
describe('getters', () => {
148+
it('filteredProducts', () => {
149+
// mock state
150+
const state = {
151+
products: [
152+
{ id: 1, title: 'Apple', category: 'fruit' },
153+
{ id: 2, title: 'Orange', category: 'fruit' },
154+
{ id: 3, title: 'Carrot', category: 'vegetable' }
155+
]
156+
}
157+
// mock getter
158+
const filterCategory = 'fruit'
159+
160+
// get the result from the getter
161+
const result = getters.filteredProducts(state, { filterCategory })
162+
163+
// assert the result
164+
expect(result).to.deep.equal([
165+
{ id: 1, title: 'Apple', category: 'fruit' },
166+
{ id: 2, title: 'Orange', category: 'fruit' }
167+
])
168+
})
169+
})
170+
```
171+
125172
### Running Tests
126173

127174
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.

0 commit comments

Comments
 (0)
0