|
| 1 | +import Router from '../../../src/index' |
| 2 | + |
| 3 | +describe('router.onReady', () => { |
| 4 | + it('should work', done => { |
| 5 | + const calls = [] |
| 6 | + |
| 7 | + const router = new Router({ |
| 8 | + mode: 'abstract', |
| 9 | + routes: [ |
| 10 | + { |
| 11 | + path: '/a', |
| 12 | + component: { |
| 13 | + name: 'A', |
| 14 | + beforeRouteEnter: (to, from, next) => { |
| 15 | + setTimeout(() => { |
| 16 | + calls.push(2) |
| 17 | + next() |
| 18 | + }, 1) |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + ] |
| 23 | + }) |
| 24 | + |
| 25 | + router.beforeEach((to, from, next) => { |
| 26 | + setTimeout(() => { |
| 27 | + calls.push(1) |
| 28 | + next() |
| 29 | + }, 1) |
| 30 | + }) |
| 31 | + |
| 32 | + router.onReady(() => { |
| 33 | + expect(calls).toEqual([1, 2]) |
| 34 | + // sync call when already ready |
| 35 | + router.onReady(() => { |
| 36 | + calls.push(3) |
| 37 | + }) |
| 38 | + expect(calls).toEqual([1, 2, 3]) |
| 39 | + done() |
| 40 | + }) |
| 41 | + |
| 42 | + router.push('/a') |
| 43 | + expect(calls).toEqual([]) |
| 44 | + }) |
| 45 | +}) |
| 46 | + |
| 47 | +describe('router.addRoutes', () => { |
| 48 | + it('should work', () => { |
| 49 | + const router = new Router({ |
| 50 | + mode: 'abstract', |
| 51 | + routes: [ |
| 52 | + { path: '/a', component: { name: 'A' }} |
| 53 | + ] |
| 54 | + }) |
| 55 | + |
| 56 | + router.push('/a') |
| 57 | + let components = router.getMatchedComponents() |
| 58 | + expect(components.length).toBe(1) |
| 59 | + expect(components[0].name).toBe('A') |
| 60 | + |
| 61 | + router.push('/b') |
| 62 | + components = router.getMatchedComponents() |
| 63 | + expect(components.length).toBe(0) |
| 64 | + |
| 65 | + router.addRoutes([ |
| 66 | + { path: '/b', component: { name: 'B' }} |
| 67 | + ]) |
| 68 | + components = router.getMatchedComponents() |
| 69 | + expect(components.length).toBe(1) |
| 70 | + expect(components[0].name).toBe('B') |
| 71 | + |
| 72 | + // make sure it preserves previous routes |
| 73 | + router.push('/a') |
| 74 | + components = router.getMatchedComponents() |
| 75 | + expect(components.length).toBe(1) |
| 76 | + expect(components[0].name).toBe('A') |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +describe('router.push/replace callbacks', () => { |
| 81 | + let calls = [] |
| 82 | + let router, spy1, spy2 |
| 83 | + |
| 84 | + const Foo = { |
| 85 | + beforeRouteEnter (to, from, next) { |
| 86 | + calls.push(3) |
| 87 | + setTimeout(() => { |
| 88 | + calls.push(4) |
| 89 | + next() |
| 90 | + }, 1) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + calls = [] |
| 96 | + spy1 = jasmine.createSpy('complete') |
| 97 | + spy2 = jasmine.createSpy('abort') |
| 98 | + |
| 99 | + router = new Router({ |
| 100 | + routes: [ |
| 101 | + { path: '/foo', component: Foo } |
| 102 | + ] |
| 103 | + }) |
| 104 | + |
| 105 | + router.beforeEach((to, from, next) => { |
| 106 | + calls.push(1) |
| 107 | + setTimeout(() => { |
| 108 | + calls.push(2) |
| 109 | + next() |
| 110 | + }, 1) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('push complete', done => { |
| 115 | + router.push('/foo', () => { |
| 116 | + expect(calls).toEqual([1, 2, 3, 4]) |
| 117 | + done() |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + it('push abort', done => { |
| 122 | + router.push('/foo', spy1, spy2) |
| 123 | + router.push('/bar', () => { |
| 124 | + expect(calls).toEqual([1, 1, 2, 2]) |
| 125 | + expect(spy1).not.toHaveBeenCalled() |
| 126 | + expect(spy2).toHaveBeenCalled() |
| 127 | + done() |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + it('replace complete', done => { |
| 132 | + router.replace('/foo', () => { |
| 133 | + expect(calls).toEqual([1, 2, 3, 4]) |
| 134 | + done() |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + it('replace abort', done => { |
| 139 | + router.replace('/foo', spy1, spy2) |
| 140 | + router.replace('/bar', () => { |
| 141 | + expect(calls).toEqual([1, 1, 2, 2]) |
| 142 | + expect(spy1).not.toHaveBeenCalled() |
| 143 | + expect(spy2).toHaveBeenCalled() |
| 144 | + done() |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments