|
| 1 | +var Vue = require('vue') |
| 2 | +var Router = require('../../../src') |
| 3 | +var Emitter = require('events').EventEmitter |
| 4 | + |
| 5 | +/** |
| 6 | + * Setup a router app for testing with two nested routes: |
| 7 | + * |
| 8 | + * - /a/b |
| 9 | + * - /c/d |
| 10 | + * |
| 11 | + * @param {Object} configs - an object that contains the |
| 12 | + * route configs for each component. |
| 13 | + * @param {Function} cb(router, calls, emitter) |
| 14 | + */ |
| 15 | + |
| 16 | +exports.test = function (configs, cb) { |
| 17 | + var emitter = new Emitter() |
| 18 | + var router = new Router({ abstract: true }) |
| 19 | + var el = document.createElement('div') |
| 20 | + var App = Vue.extend({ template: '<div><router-view></router-view></div>' }) |
| 21 | + var calls = [] |
| 22 | + // wrap hooks |
| 23 | + Object.keys(configs).forEach(function (route) { |
| 24 | + var config = configs[route] |
| 25 | + Object.keys(config).forEach(function (hook) { |
| 26 | + var fn = config[hook] |
| 27 | + config[hook] = function (transition) { |
| 28 | + var event = route + '.' + hook |
| 29 | + calls.push(event) |
| 30 | + var res = typeof fn === 'function' |
| 31 | + ? fn(transition) |
| 32 | + : fn |
| 33 | + emitter.emit(event) |
| 34 | + return res |
| 35 | + } |
| 36 | + }) |
| 37 | + }) |
| 38 | + router.map({ |
| 39 | + '/a': { |
| 40 | + component: { |
| 41 | + template: 'A <router-view></router-view>', |
| 42 | + route: configs.a |
| 43 | + }, |
| 44 | + subRoutes: { |
| 45 | + '/b': { |
| 46 | + component: { |
| 47 | + template: 'B', |
| 48 | + route: configs.b |
| 49 | + } |
| 50 | + }, |
| 51 | + '/e': { |
| 52 | + component: { |
| 53 | + template: 'E' |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + '/c': { |
| 59 | + component: { |
| 60 | + template: 'C <router-view></router-view>', |
| 61 | + route: configs.c |
| 62 | + }, |
| 63 | + subRoutes: { |
| 64 | + '/d': { |
| 65 | + component: { |
| 66 | + template: 'D', |
| 67 | + route: configs.d |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + '/data/:msg': { |
| 73 | + component: { |
| 74 | + route: configs.data, |
| 75 | + template: |
| 76 | + '<span v-if="$loadingRouteData">loading...</span>' + |
| 77 | + '<span v-if="!$loadingRouteData">{{msg}}</span>', |
| 78 | + data: function () { |
| 79 | + return { |
| 80 | + msg: 'default' |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + router.start(App, el) |
| 87 | + cb(router, calls, emitter) |
| 88 | +} |
| 89 | + |
| 90 | +exports.assertCalls = function (calls, expects) { |
| 91 | + expects.forEach(function (e, i) { |
| 92 | + expect(calls[i]).toBe(e) |
| 93 | + }) |
| 94 | +} |
0 commit comments