|
| 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