Skip to content

Commit bc2c66f

Browse files
Improve router testing. Fix navigation issues
1 parent e422827 commit bc2c66f

File tree

5 files changed

+64
-32
lines changed

5 files changed

+64
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
dist/
55
node_modules/
6+
coverage/
67

78
# ignore log files
89
*.log

jest.config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
2-
testEnvironment: "node",
3-
moduleFileExtensions: [ "js", "vue" ],
2+
testURL: 'http://localhost/',
3+
moduleFileExtensions: ['js', 'vue'],
44
transform: {
5-
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
6-
".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor"
7-
}
5+
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
6+
'.*\\.(vue)$': '<rootDir>/node_modules/jest-vue-preprocessor',
7+
},
88
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"prod": "NODE_ENV=production rollup -c ./build/rollup.config.js --configProd",
4242
"release": "bash ./build/release.sh",
4343
"lint": "eslint src/**/* build/*.js test/**/*",
44-
"test": "jest"
44+
"test": "jest --coverage",
45+
"test:node": "jest --coverage --env=node"
4546
},
4647
"devDependencies": {
4748
"babel-jest": "^23.4.2",
@@ -62,7 +63,7 @@
6263
"rollup-plugin-vue": "^4.3.0",
6364
"vue-test-utils": "^1.0.0-beta.11"
6465
},
65-
"dependencies": {
66+
"peerDependencies": {
6667
"vue": "^2.5.16",
6768
"vue-template-compiler": "^2.5.16",
6869
"vue-router": "^3.0.1"

src/router.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export default class Router extends globalVueRouter {
2020
constructor(...args) {
2121
super(...args)
2222
this.direction = args.direction || 1
23-
this.viewCount = args.viewCount || 1
24-
this.prevRouteStack = [this.history.current]
23+
this.viewCount = args.viewCount || 0
24+
this.prevRouteStack = []
2525
this.extendHistory()
2626
}
2727
extendHistory() {
@@ -33,35 +33,37 @@ export default class Router extends globalVueRouter {
3333
}
3434
}
3535
canGoBack() {
36-
return this.viewCount > 0 && this.currentRoute.fullPath.length > 1
36+
return this.viewCount > 1 && this.currentRoute.fullPath.length > 1
3737
}
3838
guessDirection(nextRoute) {
39-
// Nowhere to go but forward
40-
if (this.prevRouteStack.length === 0) {
41-
return 1
42-
}
43-
44-
const prevRoute = this.prevRouteStack[this.prevRouteStack.length - 1]
39+
if (this.prevRouteStack.length !== 0) {
40+
const prevRoute = this.prevRouteStack[this.prevRouteStack.length - 1]
4541

46-
// Last route is the same as the next one - go back
47-
// If we're going to / reset the stack otherwise pop a route
48-
if (prevRoute.fullPath === nextRoute.fullPath) {
49-
if (prevRoute.fullPath.length === 1) {
50-
this.prevRouteStack = [nextRoute]
51-
} else {
52-
this.prevRouteStack.pop()
42+
// Last route is the same as the next one - go back
43+
// If we're going to / reset the stack otherwise pop a route
44+
if (prevRoute.fullPath === nextRoute.fullPath) {
45+
if (prevRoute.fullPath.length === 1) {
46+
this.prevRouteStack = []
47+
} else {
48+
this.prevRouteStack.pop()
49+
}
50+
return -1
5351
}
54-
return -1
5552
}
5653

5754
// Forward movement, push next route to stack
58-
this.prevRouteStack.push(this.history.current)
55+
if (this.history.current.fullPath !== nextRoute.fullPath) {
56+
this.prevRouteStack.push(this.history.current)
57+
}
5958
return 1
6059
}
6160
}
6261

6362
Router.install = function(Vue) {
64-
if (Router.install.installed) return
63+
if (Router.install.installed) {
64+
return
65+
}
66+
6567
Router.install.installed = true
6668

6769
globalVueRouter.install(Vue)

test/router.spec.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1+
import Vue from 'vue'
12
import Router from '../src/router.js'
23

34
describe('Router', () => {
4-
it('Counts views', () => {
5-
let count = 1
6-
const r = new Router()
5+
it('Installs correctly', () => {
6+
Vue.use(Router)
7+
8+
const app = new Vue({
9+
router: new Router(),
10+
})
11+
12+
expect(typeof app.$router).toBe('object')
13+
expect(typeof app.$options.components.IonVueRouter).toBe('function')
14+
expect(Router.install()).toBeFalsy()
15+
})
16+
17+
it('Navigates correctly', () => {
18+
const r = new Router({ mode: 'abstract' })
19+
20+
r.push('/')
21+
expect(r.viewCount).toBe(1)
22+
expect(r.direction).toBe(1)
23+
expect(r.canGoBack()).toBeFalsy()
724

825
r.push('/foo')
9-
expect(r.viewCount).toBe(++count)
26+
expect(r.viewCount).toBe(2)
27+
expect(r.direction).toBe(1)
28+
expect(r.canGoBack()).toBeTruthy()
1029

1130
r.push('/bar')
12-
expect(r.viewCount).toBe(++count)
31+
expect(r.viewCount).toBe(3)
32+
expect(r.direction).toBe(1)
33+
expect(r.canGoBack()).toBeTruthy()
34+
35+
r.go(-1)
36+
expect(r.viewCount).toBe(2)
37+
expect(r.direction).toBe(-1)
38+
expect(r.canGoBack()).toBeTruthy()
1339

1440
r.go(-1)
15-
expect(r.viewCount).toBe(--count)
41+
expect(r.viewCount).toBe(1)
42+
expect(r.direction).toBe(-1)
43+
expect(r.canGoBack()).toBeFalsy()
1644
})
1745
})

0 commit comments

Comments
 (0)