Skip to content

Commit 35cba34

Browse files
authored
Add an article about reducing boilerplate (#138)
1 parent 5e3cca4 commit 35cba34

File tree

6 files changed

+475
-0
lines changed

6 files changed

+475
-0
lines changed

demo-app/src/components/Posts.vue

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<template>
2+
<div>
3+
<div id="message" v-if="message">{{ message }}</div>
4+
5+
<div v-if="authenticated">
6+
<router-link
7+
class="new-post"
8+
to="/posts/new"
9+
>
10+
New Post
11+
</router-link>
12+
</div>
13+
14+
<h1>Posts</h1>
15+
<div
16+
v-for="post in posts"
17+
:key="post.id"
18+
class="post"
19+
>
20+
<router-link :to="postLink(post.id)">
21+
{{ post.title }}
22+
</router-link>
23+
</div>
24+
</div>
25+
</template>
26+
27+
<script>
28+
export default {
29+
name: 'Posts',
30+
props: {
31+
message: String,
32+
},
33+
34+
computed: {
35+
authenticated() {
36+
return this.$store.state.authenticated
37+
},
38+
39+
posts() {
40+
return this.$store.state.posts
41+
}
42+
},
43+
44+
methods: {
45+
postLink(id) {
46+
return `/posts/${id}`
47+
}
48+
}
49+
}
50+
</script>

demo-app/src/createRouter.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const createRouter = () => {
7+
return new VueRouter({
8+
mode: 'history',
9+
base: process.env.BASE_URL,
10+
routes: []
11+
})
12+
}
13+
14+
export { createRouter }
15+

demo-app/src/createStore.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Vue from 'vue'
2+
import Vuex from 'vuex'
3+
4+
Vue.use(Vuex)
5+
6+
const createStore = (initialState = {}) => new Vuex.Store({
7+
state: {
8+
authenticated: false,
9+
posts: [],
10+
...initialState,
11+
},
12+
mutations: {
13+
ADD_POSTS(state, posts) {
14+
state.posts = [...state.posts, ...posts]
15+
},
16+
17+
SET_AUTH(state, authenticated) {
18+
state.authenticated = authenticated
19+
},
20+
}
21+
})
22+
23+
export { createStore }
24+
25+

demo-app/tests/unit/Posts.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Vuex from 'vuex'
2+
import VueRouter from 'vue-router'
3+
import { mount, createLocalVue } from '@vue/test-utils'
4+
5+
import Posts from '@/components/Posts.vue'
6+
import { createRouter } from '@/createRouter'
7+
import { createStore } from '@/createStore'
8+
9+
const createWrapper = (component, options = {}, storeState = {}) => {
10+
const localVue = createLocalVue()
11+
localVue.use(VueRouter)
12+
localVue.use(Vuex)
13+
const store = createStore(storeState)
14+
const router = createRouter()
15+
16+
return mount(component, {
17+
store,
18+
router,
19+
localVue,
20+
...options
21+
})
22+
}
23+
24+
describe('Posts.vue', () => {
25+
it('renders a message if passed', () => {
26+
const message = 'New content coming soon!'
27+
const wrapper = createWrapper(Posts, {
28+
propsData: { message },
29+
})
30+
31+
expect(wrapper.find("#message").text()).toBe('New content coming soon!')
32+
})
33+
34+
it('renders posts', async () => {
35+
const wrapper = createWrapper(Posts, {}, {
36+
posts: [{ id: 1, title: 'Post' }]
37+
})
38+
39+
expect(wrapper.findAll('.post').length).toBe(1)
40+
})
41+
42+
it('renders new post link if authenticated', async () => {
43+
const wrapper = createWrapper(Posts, {}, {
44+
authenticated: true
45+
})
46+
47+
expect(wrapper.find('.new-post').exists()).toBe(true)
48+
})
49+
50+
it('does not render new post link if not authenticated', async () => {
51+
const wrapper = createWrapper(Posts, {}, {
52+
authenticated: false
53+
})
54+
55+
expect(wrapper.find('.new-post').exists()).toBe(false)
56+
})
57+
})

src/.vuepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module.exports = {
6363
['/vuex-in-components-mutations-and-actions', 'Vuex in components - mutations and actions'],
6464
['/vue-router', 'Vue Router'],
6565
['/composition-api', 'Composition API'],
66+
['/reducing-boilerplate-in-tests', 'Reducing Boilerplate'],
6667
['/jest-mocking-modules', 'Jest - mocking modules'],
6768
]
6869
},

0 commit comments

Comments
 (0)