Skip to content

Commit 9f3a471

Browse files
committed
testing
1 parent 1820a2e commit 9f3a471

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
- [Testing](#testing)
5656
- [Testing Options](#testing-options)
5757
- [Testing Setup](#testing-setup)
58+
- [Testing Component](#testing-component)
59+
- [Testing Component Changes](#testing-component-changes)
60+
- [Testing with Router and State](#testing-with-router-and-state)
5861

5962
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
6063

@@ -1109,3 +1112,34 @@ Create a `test` folder at same level as `src`. Place [karma.config.js](test/unit
11091112
Also create [webpack.test.config.js](build/webpack.test.config.js).
11101113

11111114
[index.js](test/unit/index.js) contains all files that would like to test.
1115+
1116+
Add extra lint rules for tests (describe, it, expect) [.eslintrc](test/unit/.eslintrc)
1117+
### Testing Component
1118+
1119+
[Post.spec.js](test/unit/specs/Post.spec.js)
1120+
1121+
`Post` component has a `link` property and displays the title section in the title slot and content section in content slot.
1122+
1123+
Test will verify that link is generated correctly.
1124+
1125+
To run tests, add to `package.json`:
1126+
1127+
```
1128+
"test": "karma start test/unit/karma.config.js --single-run"
1129+
```
1130+
1131+
Then run at console:
1132+
1133+
```shell
1134+
npm test
1135+
```
1136+
1137+
### Testing Component Changes
1138+
1139+
Also need to test that when properties of a component change, it re-renders with appropriate changes.
1140+
1141+
To make property change take effect in test, use [nextTick](https://vuejs.org/v2/api/#Vue-nextTick). This method triggers callback after DOM update cycle is finished. Use Mocha's `done` callback to wait.
1142+
1143+
### Testing with Router and State
1144+
1145+
[Category.spec.js](test/unit/specs/Category.spec.js)

build/webpack.test.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const base = require('./webback.base.config.js')
1+
const base = require('./webpack.base.config.js')
22

33
let config = Object.assign({}, base, {})
44

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "> Notes from Pluralsight [course](https://app.pluralsight.com/library/courses/vue-js-single-page-applications/table-of-contents)",
55
"scripts": {
66
"start": "node server.js",
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "karma start test/unit/karma.config.js --single-run"
88
},
99
"keywords": [],
1010
"author": "",

test/unit/.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
},
5+
"globals": {
6+
"expect": true,
7+
"sinon": true
8+
}
9+
}

test/unit/specs/Category.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'es6-promise/auto'
2+
import Vue from 'vue'
3+
import store from '../../../src/vuex/index.js'
4+
import VueRouter from 'vue-router'
5+
import Category from '../../../src/theme/Category.vue'
6+
7+
describe('Category.vue', () => {
8+
it('loads front-end links', done => {
9+
Vue.use(VueRouter)
10+
// dummy router instance
11+
const router = new VueRouter({
12+
routes: [
13+
{path: '/', component: Category}
14+
]
15+
})
16+
17+
// Category component uses router heavily as a dependency
18+
const vm = new Vue({
19+
el: document.createElement('div'),
20+
router,
21+
store,
22+
// i.e. <div><router-view></router-view></div>
23+
render: h => h('router-view')
24+
})
25+
26+
// flaky: depending on real ajax request - homework: mock this
27+
store.watch(
28+
(state) => {
29+
return state.postsModule.posts
30+
},
31+
function () {
32+
console.log(vm.$el)
33+
expect(vm.$el.querySelectorAll('.column').length).to.equal(6)
34+
done()
35+
}
36+
)
37+
// console.log(vm.$el) // with no api hooked up: <div class="columns"></div>
38+
// done()
39+
})
40+
})

test/unit/specs/Post.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Vue from 'vue'
2+
import Post from '../../../src/theme/Post.vue'
3+
4+
describe('Post.vue', () => {
5+
// re-usable function to create component for testing
6+
const createComponent = () => {
7+
const PostConstructor = Vue.extend(Post)
8+
const comp = new PostConstructor({
9+
propsData: {
10+
link: 'http://www.pluralsight.com'
11+
}
12+
}).$mount()
13+
return comp
14+
}
15+
it('renders link', () => {
16+
const comp = createComponent()
17+
expect(comp.$el.querySelector('.card-footer-item').getAttribute('href')).to.equal('http://www.pluralsight.com')
18+
})
19+
20+
it('updates href when property link changes', (done) => {
21+
const comp = createComponent()
22+
expect(comp.$el.querySelector('.card-footer-item').getAttribute('href')).to.equal('http://www.pluralsight.com')
23+
// change link
24+
comp.link = 'http://fullstackweekly.com'
25+
// wait for dom cycle to complete rendering after link updated
26+
Vue.nextTick(() => {
27+
expect(comp.$el.querySelector('.card-footer-item').getAttribute('href')).to.equal('http://fullstackweekly.com')
28+
done()
29+
})
30+
})
31+
})

0 commit comments

Comments
 (0)