|
1 | | -var Vue = require('vue') |
2 | | -var Router = require('../../../src') |
3 | | -var Emitter = require('events').EventEmitter |
| 1 | +var util = require('../../../src/util') |
4 | 2 |
|
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 | | - */ |
| 3 | +describe('Util', function () { |
15 | 4 |
|
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 | | - }) |
| 5 | + it('resolvePath', function () { |
| 6 | + expect(util.resolvePath('/a', 'b')).toBe('/b') |
| 7 | + expect(util.resolvePath('/a/', 'b')).toBe('/a/b') |
| 8 | + expect(util.resolvePath('/a', '/b')).toBe('/b') |
| 9 | + expect(util.resolvePath('/a/', '/b')).toBe('/a/b') |
| 10 | + // append mode |
| 11 | + expect(util.resolvePath('/a', 'b', true)).toBe('/a/b') |
| 12 | + expect(util.resolvePath('/a/', 'b', true)).toBe('/a/b') |
| 13 | + expect(util.resolvePath('/a', '/b', true)).toBe('/a/b') |
| 14 | + expect(util.resolvePath('/a/', '/b', true)).toBe('/a/b') |
| 15 | + // relative query |
| 16 | + expect(util.resolvePath('/a', '?b=1')).toBe('/a?b=1') |
| 17 | + expect(util.resolvePath('/a/', '?b=1')).toBe('/a/?b=1') |
37 | 18 | }) |
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 | | - } |
| 19 | + |
| 20 | + it('mapParams', function () { |
| 21 | + expect(util.mapParams('/a/:id', { id: 'b' })).toBe('/a/b') |
| 22 | + expect(util.mapParams('/a/:id/', { id: 'b' })).toBe('/a/b/') |
| 23 | + expect(util.mapParams('/a/:id/c/:d', { id: 'b', d: 'd' })).toBe('/a/b/c/d') |
| 24 | + expect(util.mapParams('/a/:id/c/:d', { id: 'b', d: 'd' }, { e: 123 })).toBe('/a/b/c/d?e=123') |
| 25 | + }) |
| 26 | + |
| 27 | + it('resolveAsyncComponent', function (done) { |
| 28 | + var handler = { |
| 29 | + component: function (resolve) { |
| 30 | + setTimeout(function () { |
| 31 | + resolve({ |
| 32 | + template: 'hi' |
| 33 | + }) |
| 34 | + }, 0) |
83 | 35 | } |
84 | 36 | } |
| 37 | + util.resolveAsyncComponent(handler, function (Component) { |
| 38 | + expect(Component.options.template).toBe('hi') |
| 39 | + done() |
| 40 | + }) |
85 | 41 | }) |
86 | | - router.start(App, el) |
87 | | - cb(router, calls, emitter) |
88 | | -} |
89 | 42 |
|
90 | | -exports.assertCalls = function (calls, expects) { |
91 | | - expects.forEach(function (e, i) { |
92 | | - expect(calls[i]).toBe(e) |
| 43 | + it('getRouteConfig', function () { |
| 44 | + expect(util.getRouteConfig({}, 'data')).toBeUndefined() |
| 45 | + expect(util.getRouteConfig({ options: { route: {}}}, 'data')).toBeUndefined() |
| 46 | + expect(util.getRouteConfig({ options: { route: { data: 1 }}}, 'data')).toBe(1) |
| 47 | + expect(util.getRouteConfig({ $options: { route: {}}}, 'data')).toBeUndefined() |
| 48 | + expect(util.getRouteConfig({ $options: { route: { data: 1 }}}, 'data')).toBe(1) |
93 | 49 | }) |
94 | | -} |
| 50 | + |
| 51 | +}) |
0 commit comments