Skip to content

Commit 8762517

Browse files
committed
add test
1 parent 2891953 commit 8762517

File tree

3 files changed

+2086
-2
lines changed

3 files changed

+2086
-2
lines changed

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "4.1.3",
44
"description": "Effortlessly keep vue-router and vuex store in sync.",
55
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
69
"repository": {
710
"type": "git",
811
"url": "git+https://github.com/vuejs/vuex-router-sync.git"
@@ -18,8 +21,14 @@
1821
"url": "https://github.com/vuejs/vuex-router-sync/issues"
1922
},
2023
"homepage": "https://github.com/vuejs/vuex-router-sync#readme",
24+
"devDependencies": {
25+
"jest": "^20.0.4",
26+
"vue": "^2.3.3",
27+
"vue-router": "^2.0.0",
28+
"vuex": "^2.1.0"
29+
},
2130
"peerDependencies": {
22-
"vuex": "^2.0.0",
23-
"vue-router": "^2.0.0"
31+
"vue-router": "^2.0.0",
32+
"vuex": "^2.1.0"
2433
}
2534
}

test/test.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const Vue = require('vue')
2+
const Vuex = require('vuex')
3+
const VueRouter = require('vue-router')
4+
const { sync } = require('../index')
5+
const { mapState } = Vuex
6+
7+
Vue.use(Vuex)
8+
Vue.use(VueRouter)
9+
10+
const run = (originalModuleName, done) => {
11+
const moduleName = originalModuleName || 'route'
12+
13+
const store = new Vuex.Store({
14+
state: { msg: 'foo' }
15+
})
16+
17+
const Home = {
18+
computed: mapState(moduleName, {
19+
path: state => state.fullPath,
20+
foo: state => state.params.foo,
21+
bar: state => state.params.bar
22+
}),
23+
render (h) {
24+
return h('div', [this.path, ' ', this.foo, ' ', this.bar])
25+
}
26+
}
27+
28+
const router = new VueRouter({
29+
mode: 'abstract',
30+
routes: [
31+
{ path: '/:foo/:bar', component: Home }
32+
]
33+
})
34+
35+
sync(store, router, {
36+
moduleName: originalModuleName
37+
})
38+
39+
router.push('/a/b')
40+
expect(store.state[moduleName].fullPath).toBe('/a/b')
41+
expect(store.state[moduleName].params).toEqual({ foo: 'a', bar: 'b' })
42+
43+
const app = new Vue({
44+
store,
45+
router,
46+
render: h => h('router-view')
47+
}).$mount()
48+
49+
expect(app.$el.textContent).toBe('/a/b a b')
50+
51+
router.push('/c/d?n=1#hello')
52+
expect(store.state[moduleName].fullPath).toBe('/c/d?n=1#hello')
53+
expect(store.state[moduleName].params).toEqual({ foo: 'c', bar: 'd' })
54+
expect(store.state[moduleName].query).toEqual({ n: '1' })
55+
expect(store.state[moduleName].hash).toEqual('#hello')
56+
57+
Vue.nextTick(() => {
58+
expect(app.$el.textContent).toBe('/c/d?n=1#hello c d')
59+
done()
60+
})
61+
}
62+
63+
test('default usage', done => {
64+
run(null, done)
65+
})
66+
67+
test('with custom moduleName', done => {
68+
run('moduleName', done)
69+
})

0 commit comments

Comments
 (0)