Skip to content

Commit 74424ad

Browse files
authored
Add Vuex in components guide (#25)
* Add getters guide * Upate
1 parent f633cc8 commit 74424ad

12 files changed

+333
-17
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"no-debugger": 2,
1818
"no-proto": 0,
1919
"max-len": 2,
20-
"indent": ["error", 2]
20+
"indent": ["error", 2],
21+
"no-multi-spaces": "error"
2122
},
2223
extends: [
2324
'eslint:recommended',

demo-app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"@vue/test-utils": "^1.0.0-beta.16",
2020
"babel-core": "7.0.0-bridge.0",
2121
"babel-jest": "^23.0.1",
22-
"vue-template-compiler": "^2.5.16"
22+
"vue-template-compiler": "^2.5.16",
23+
"vuex": "^3.0.1"
2324
},
2425
"browserslist": [
2526
"> 1%",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<div class="fullname">
3+
{{ fullname }}
4+
</div>
5+
</template>
6+
7+
<script>
8+
// import { mapGetters } from "vuex"
9+
10+
export default {
11+
name: "ComponentWithGetters",
12+
13+
computed: {
14+
fullname() {
15+
return this.$store.getters.fullname
16+
}
17+
18+
/**
19+
* mapGetters helper can be used
20+
* ...mapGetters([
21+
* 'fullname'
22+
* ])
23+
*/
24+
}
25+
}
26+
</script>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<div class="username">
4+
{{ username }}
5+
</div>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: "ComponentWithVuex",
12+
13+
data() {
14+
return {
15+
username: this.$store.state.username
16+
}
17+
}
18+
}
19+
</script>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Vuex from "vuex"
2+
import { shallowMount, createLocalVue } from "@vue/test-utils"
3+
import ComponentWithVuex from "@/components/ComponentWithVuex.vue"
4+
import ComponentWithGetters from "@/components/ComponentWithGetters.vue"
5+
6+
const localVue = createLocalVue()
7+
localVue.use(Vuex)
8+
9+
const store = new Vuex.Store({
10+
state: {
11+
username: "alice",
12+
firstName: "Alice",
13+
lastName: "Doe"
14+
},
15+
16+
getters: {
17+
fullname: (state) => state.firstName + " " + state.lastName
18+
}
19+
})
20+
21+
describe("ComponentWithVuex", () => {
22+
it("renders a username using a real Vuex store", () => {
23+
const wrapper = shallowMount(ComponentWithVuex, { store, localVue })
24+
25+
expect(wrapper.find(".username").text()).toBe("alice")
26+
})
27+
28+
it("renders a username using a mock store", () => {
29+
const wrapper = shallowMount(ComponentWithVuex, {
30+
mocks: {
31+
$store: {
32+
state: { username: "alice" }
33+
}
34+
}
35+
})
36+
37+
expect(wrapper.find(".username").text()).toBe("alice")
38+
})
39+
})
40+
41+
describe("ComponentWithGetters", () => {
42+
it("renders a username using a real Vuex getter", () => {
43+
const wrapper = shallowMount(ComponentWithGetters, { store, localVue })
44+
45+
expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
46+
})
47+
48+
it("renders a username using computed mounting options", () => {
49+
const wrapper = shallowMount(ComponentWithGetters, {
50+
mocks: {
51+
$store: {
52+
getters: {
53+
fullname: "Alice Doe"
54+
}
55+
}
56+
}
57+
})
58+
59+
expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
60+
})
61+
62+
it("renders a username using computed mounting options", () => {
63+
const wrapper = shallowMount(ComponentWithGetters, {
64+
computed: {
65+
fullname: () => "Alice Doe"
66+
}
67+
})
68+
69+
expect(wrapper.find(".fullname").text()).toBe("Alice Doe")
70+
})
71+
})

demo-app/yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7331,6 +7331,10 @@ vue@^2.5.16:
73317331
version "2.5.16"
73327332
resolved "https://registry.yarnpkg.com/vue/-/vue-2.5.16.tgz#07edb75e8412aaeed871ebafa99f4672584a0085"
73337333

7334+
vuex@^3.0.1:
7335+
version "3.0.1"
7336+
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.0.1.tgz#e761352ebe0af537d4bb755a9b9dc4be3df7efd2"
7337+
73347338
w3c-hr-time@^1.0.1:
73357339
version "1.0.1"
73367340
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045"

docs/vuex-in-components-mutations-and-getters.md

Whitespace-only changes.

src/.vuepress/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = {
2929
['/vuex-mutations', 'Vuex - Mutations'],
3030
['/vuex-actions', 'Vuex - Actions'],
3131
['/vuex-getters', 'Vuex - Getters'],
32-
['/vuex-in-components', 'Mocking Vuex in components'],
32+
['/vuex-in-components', 'Vuex in components - $state and getters'],
33+
['/vuex-in-components-mutations-and-getters', 'Vuex in components - mutations and actions'],
3334
['/jest-mocking-modules', 'Jest - mocking modules'],
3435
['/vue-router', 'Vue Router'],
3536
]
@@ -51,6 +52,7 @@ module.exports = {
5152
['/ja/vuex-actions', 'Vuex - アクション'],
5253
['/ja/vuex-getters', 'Vuex - ゲッター'],
5354
['/ja/vuex-in-components', 'コンポーネントの中でVuexのテスト'],
55+
['/ja/vuex-in-components-mutations-and-getters.md', 'Vuex in components - mutations and actions'],
5456
['/ja/jest-mocking-modules', 'Jestでモジュールをモック'],
5557
['/ja/vue-router', 'Vueルーター'],
5658
]

src/ja/vuex-in-components-mutations-and-getters.md

Whitespace-only changes.

src/testing-vuex.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
### Testing Vuex
22

3-
Intro page to testing vuex. Brief intro. Talk about two different ways to test Vuex and their benefits.
3+
The next few guides discuss testing Vuex.
44

5-
1. testing the store separately
5+
## Two Sides of Testing Vuex
66

7-
2. testing in a component (either mocking vuex or using a real store with `localVue`
7+
Generally components will interact with Vuex by
88

9+
1. committing a mutation
10+
2. dispatching an action
11+
3. acesss the state via `$store.state` or getters
12+
13+
These tests are to assert that the component behaves correctly based on the current state of the Vuex store. They do not need to know about the implmentation of the mutators, actions or getters.
14+
15+
Any logic performed by the store, such as mutations and getters, can be tested in isolation. Since Vuex stores are comprised of regular JavaScript functions, they are easily unit tested.
16+
17+
The next guide introduces some techniques to test components that use a Vuex store, and ensure they behave correctly based on the store's state. Later guides discuss testing Vuex in isolation.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Mutations and Actions
2+
3+
Talk about using `jest.fn` to mock commit and dispatch. We want to assert the correct mutation/action handler and payload.
4+
5+
Also introduce the tests working with `mapMutations` and `mapActions`?

0 commit comments

Comments
 (0)