File tree Expand file tree Collapse file tree 6 files changed +116
-2
lines changed Expand file tree Collapse file tree 6 files changed +116
-2
lines changed Original file line number Diff line number Diff line change 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
11091112Also 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 )
Original file line number Diff line number Diff line change 1- const base = require ( './webback .base.config.js' )
1+ const base = require ( './webpack .base.config.js' )
22
33let config = Object . assign ( { } , base , { } )
44
Original file line number Diff line number Diff line change 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" : " " ,
Original file line number Diff line number Diff line change 1+ {
2+ "env": {
3+ "mocha": true
4+ },
5+ "globals": {
6+ "expect": true,
7+ "sinon": true
8+ }
9+ }
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments