| OLD | NEW |
| 1 library routing_spec; | 1 library routing_spec; |
| 2 | 2 |
| 3 import '../_specs.dart'; | 3 import '../_specs.dart'; |
| 4 import 'package:angular/routing/module.dart'; |
| 4 import 'package:angular/mock/module.dart'; | 5 import 'package:angular/mock/module.dart'; |
| 5 import 'dart:async'; | |
| 6 | 6 |
| 7 main() { | 7 main() { |
| 8 describe('routing', () { | 8 describe('routing', () { |
| 9 TestBed _; | 9 TestBed _; |
| 10 TestRouteInitializer initer; |
| 10 Router router; | 11 Router router; |
| 11 | 12 |
| 12 beforeEach(module((Module m) { | 13 beforeEach(module((Module m) { |
| 13 _initRoutesCalls = 0; | |
| 14 _router = null; | |
| 15 router = new Router(useFragment: false, windowImpl: new MockWindow()); | 14 router = new Router(useFragment: false, windowImpl: new MockWindow()); |
| 16 m | 15 m |
| 17 ..install(new AngularMockModule()) | 16 ..install(new AngularMockModule()) |
| 18 ..factory(RouteInitializerFn, (_) => initRoutes) | 17 ..type(RouteInitializer, implementedBy: TestRouteInitializer) |
| 19 ..value(Router, router); | 18 ..value(Router, router); |
| 20 })); | 19 })); |
| 21 | 20 |
| 22 beforeEach(inject((TestBed tb) { | 21 beforeEach(inject((TestBed tb, RouteInitializer _initer) { |
| 23 _ = tb; | 22 _ = tb; |
| 23 initer = _initer; |
| 24 })); | 24 })); |
| 25 | 25 |
| 26 |
| 26 it('should call init of the RouteInitializer once', async(() { | 27 it('should call init of the RouteInitializer once', async(() { |
| 27 expect(_initRoutesCalls).toEqual(0); | 28 expect(initer.calledInit).toEqual(0); |
| 28 | 29 |
| 29 // Force the routing system to initialize. | 30 // Force the routing system to initialize. |
| 30 _.compile('<ng-view></ng-view>'); | 31 _.compile('<ng-view></ng-view>'); |
| 31 | 32 |
| 32 expect(_initRoutesCalls).toEqual(1); | 33 expect(initer.calledInit).toEqual(1); |
| 33 expect(_router).toBe(router); | 34 expect(initer.router).toBe(router); |
| 34 })); | |
| 35 | |
| 36 }); | |
| 37 | |
| 38 describe('routing DSL', () { | |
| 39 Router router; | |
| 40 TestBed _; | |
| 41 | |
| 42 afterEach(() { | |
| 43 router = _ = null; | |
| 44 }); | |
| 45 | |
| 46 initRouter(initializer) { | |
| 47 var module = new Module() | |
| 48 ..value(RouteInitializerFn, initializer); | |
| 49 var injector = new DynamicInjector( | |
| 50 modules: [new AngularModule(), new AngularMockModule(), module]); | |
| 51 injector.get(NgRoutingHelper); // force routing initialization | |
| 52 router = injector.get(Router); | |
| 53 _ = injector.get(TestBed); | |
| 54 } | |
| 55 | |
| 56 it('should configure route hierarchy from provided config', async(() { | |
| 57 var counters = { | |
| 58 'foo': 0, | |
| 59 'bar': 0, | |
| 60 'baz': 0, | |
| 61 'aux': 0, | |
| 62 }; | |
| 63 initRouter((Router router, ViewFactory views) { | |
| 64 views.configure({ | |
| 65 'foo': ngRoute( | |
| 66 path: '/foo', | |
| 67 enter: (_) => counters['foo']++, | |
| 68 mount: { | |
| 69 'bar': ngRoute( | |
| 70 path: '/bar', | |
| 71 enter: (_) => counters['bar']++ | |
| 72 ), | |
| 73 'baz': ngRoute( | |
| 74 path: '/baz', | |
| 75 enter: (_) => counters['baz']++ | |
| 76 ) | |
| 77 } | |
| 78 ), | |
| 79 'aux': ngRoute( | |
| 80 path: '/aux', | |
| 81 enter: (_) => counters['aux']++ | |
| 82 ) | |
| 83 }); | |
| 84 }); | |
| 85 | |
| 86 expect(router.root.getRoute('foo').name).toEqual('foo'); | |
| 87 expect(router.root.getRoute('foo.bar').name).toEqual('bar'); | |
| 88 expect(router.root.getRoute('foo.baz').name).toEqual('baz'); | |
| 89 expect(router.root.getRoute('aux').name).toEqual('aux'); | |
| 90 | |
| 91 router.route('/foo'); | |
| 92 microLeap(); | |
| 93 expect(counters, equals({ | |
| 94 'foo': 1, | |
| 95 'bar': 0, | |
| 96 'baz': 0, | |
| 97 'aux': 0, | |
| 98 })); | |
| 99 | |
| 100 router.route('/foo/bar'); | |
| 101 microLeap(); | |
| 102 expect(counters, equals({ | |
| 103 'foo': 1, | |
| 104 'bar': 1, | |
| 105 'baz': 0, | |
| 106 'aux': 0, | |
| 107 })); | |
| 108 | |
| 109 router.route('/foo/baz'); | |
| 110 microLeap(); | |
| 111 expect(counters, equals({ | |
| 112 'foo': 1, | |
| 113 'bar': 1, | |
| 114 'baz': 1, | |
| 115 'aux': 0, | |
| 116 })); | |
| 117 | |
| 118 router.route('/aux'); | |
| 119 microLeap(); | |
| 120 expect(counters, equals({ | |
| 121 'foo': 1, | |
| 122 'bar': 1, | |
| 123 'baz': 1, | |
| 124 'aux': 1, | |
| 125 })); | |
| 126 })); | |
| 127 | |
| 128 | |
| 129 it('should set the default route', async(() { | |
| 130 int enterCount = 0; | |
| 131 initRouter((Router router, ViewFactory views) { | |
| 132 views.configure({ | |
| 133 'foo': ngRoute(path: '/foo'), | |
| 134 'bar': ngRoute(path: '/bar', defaultRoute: true), | |
| 135 'baz': ngRoute(path: '/baz'), | |
| 136 }); | |
| 137 }); | |
| 138 | |
| 139 router.route('/invalidRoute'); | |
| 140 microLeap(); | |
| 141 | |
| 142 expect(router.activePath.length).toBe(1); | |
| 143 expect(router.activePath.first.name).toBe('bar'); | |
| 144 })); | |
| 145 | |
| 146 | |
| 147 it('should call enter callback and show the view when routed', async(() { | |
| 148 int enterCount = 0; | |
| 149 initRouter((Router router, ViewFactory views) { | |
| 150 views.configure({ | |
| 151 'foo': ngRoute( | |
| 152 path: '/foo', | |
| 153 enter: (_) => enterCount++, | |
| 154 view: 'foo.html' | |
| 155 ), | |
| 156 }); | |
| 157 }); | |
| 158 _.injector.get(TemplateCache) | |
| 159 .put('foo.html', new HttpResponse(200, '<h1>Foo</h1>')); | |
| 160 | |
| 161 Element root = _.compile('<ng-view></ng-view>'); | |
| 162 expect(root.text).toEqual(''); | |
| 163 | |
| 164 router.route('/foo'); | |
| 165 microLeap(); | |
| 166 | |
| 167 expect(enterCount).toBe(1); | |
| 168 expect(root.text).toEqual('Foo'); | |
| 169 })); | |
| 170 | |
| 171 | |
| 172 it('should call preEnter callback and load modules', async(() { | |
| 173 int preEnterCount = 0; | |
| 174 int modulesCount = 0; | |
| 175 initRouter((Router router, ViewFactory views) { | |
| 176 views.configure({ | |
| 177 'foo': ngRoute( | |
| 178 path: '/foo', | |
| 179 preEnter: (_) => preEnterCount++, | |
| 180 modules: () { | |
| 181 modulesCount++; | |
| 182 return new Future.value(); | |
| 183 } | |
| 184 ), | |
| 185 'bar': ngRoute( | |
| 186 path: '/bar' | |
| 187 ) | |
| 188 }); | |
| 189 }); | |
| 190 | |
| 191 router.route('/foo'); | |
| 192 microLeap(); | |
| 193 | |
| 194 expect(preEnterCount).toBe(1); | |
| 195 expect(modulesCount).toBe(1); | |
| 196 | |
| 197 router.route('/foo'); | |
| 198 microLeap(); | |
| 199 | |
| 200 expect(preEnterCount).toBe(1); | |
| 201 expect(modulesCount).toBe(1); | |
| 202 | |
| 203 router.route('/bar'); | |
| 204 microLeap(); | |
| 205 | |
| 206 expect(preEnterCount).toBe(1); | |
| 207 expect(modulesCount).toBe(1); | |
| 208 | |
| 209 router.route('/foo'); | |
| 210 microLeap(); | |
| 211 | |
| 212 expect(preEnterCount).toBe(2); | |
| 213 expect(modulesCount).toBe(1); | |
| 214 })); | |
| 215 | |
| 216 | |
| 217 it('should clear view on leave an call leave callback', async(() { | |
| 218 int leaveCount = 0; | |
| 219 initRouter((Router router, ViewFactory views) { | |
| 220 views.configure({ | |
| 221 'foo': ngRoute( | |
| 222 path: '/foo', | |
| 223 leave: (_) => leaveCount++, | |
| 224 view: 'foo.html' | |
| 225 ), | |
| 226 'bar': ngRoute( | |
| 227 path: '/bar' | |
| 228 ), | |
| 229 }); | |
| 230 }); | |
| 231 _.injector.get(TemplateCache) | |
| 232 .put('foo.html', new HttpResponse(200, '<h1>Foo</h1>')); | |
| 233 | |
| 234 Element root = _.compile('<ng-view></ng-view>'); | |
| 235 expect(root.text).toEqual(''); | |
| 236 | |
| 237 router.route('/foo'); | |
| 238 microLeap(); | |
| 239 | |
| 240 expect(root.text).toEqual('Foo'); | |
| 241 expect(leaveCount).toBe(0); | |
| 242 | |
| 243 router.route('/bar'); | |
| 244 microLeap(); | |
| 245 | |
| 246 expect(root.text).toEqual(''); | |
| 247 expect(leaveCount).toBe(1); | |
| 248 })); | |
| 249 | |
| 250 | |
| 251 it('should synchronously load new directives from modules ', async(() { | |
| 252 initRouter((Router router, ViewFactory views) { | |
| 253 views.configure({ | |
| 254 'foo': ngRoute( | |
| 255 path: '/foo', | |
| 256 modules: () => [ | |
| 257 new Module()..type(NewDirective) | |
| 258 ], | |
| 259 view: 'foo.html' | |
| 260 ), | |
| 261 }); | |
| 262 }); | |
| 263 _.injector.get(TemplateCache) | |
| 264 .put('foo.html', new HttpResponse(200, '<div make-it-new>Old!</div>'))
; | |
| 265 | |
| 266 Element root = _.compile('<ng-view></ng-view>'); | |
| 267 expect(root.text).toEqual(''); | |
| 268 | |
| 269 router.route('/foo'); | |
| 270 microLeap(); | |
| 271 | |
| 272 expect(root.text).toEqual('New!'); | |
| 273 })); | |
| 274 | |
| 275 | |
| 276 it('should asynchronously load new directives from modules ', async(() { | |
| 277 initRouter((Router router, ViewFactory views) { | |
| 278 views.configure({ | |
| 279 'foo': ngRoute( | |
| 280 path: '/foo', | |
| 281 modules: () => new Future.value([ | |
| 282 new Module()..type(NewDirective) | |
| 283 ]), | |
| 284 view: 'foo.html' | |
| 285 ), | |
| 286 }); | |
| 287 }); | |
| 288 _.injector.get(TemplateCache) | |
| 289 .put('foo.html', new HttpResponse(200, '<div make-it-new>Old!</div>'))
; | |
| 290 | |
| 291 Element root = _.compile('<ng-view></ng-view>'); | |
| 292 expect(root.text).toEqual(''); | |
| 293 | |
| 294 router.route('/foo'); | |
| 295 microLeap(); | |
| 296 | |
| 297 expect(root.text).toEqual('New!'); | |
| 298 })); | |
| 299 | |
| 300 | |
| 301 it('should synchronously load new filters from modules ', async(() { | |
| 302 initRouter((Router router, ViewFactory views) { | |
| 303 views.configure({ | |
| 304 'foo': ngRoute( | |
| 305 path: '/foo', | |
| 306 modules: () => [ | |
| 307 new Module()..type(HelloFilter) | |
| 308 ], | |
| 309 view: 'foo.html' | |
| 310 ), | |
| 311 }); | |
| 312 }); | |
| 313 _.injector.get(TemplateCache) | |
| 314 .put('foo.html', new HttpResponse(200, '<div>{{\'World\' | hello}}</di
v>')); | |
| 315 | |
| 316 Element root = _.compile('<ng-view></ng-view>'); | |
| 317 expect(root.text).toEqual(''); | |
| 318 | |
| 319 router.route('/foo'); | |
| 320 microLeap(); | |
| 321 _.rootScope.apply(); | |
| 322 | |
| 323 expect(root.text).toEqual('Hello, World!'); | |
| 324 })); | |
| 325 | |
| 326 | |
| 327 it('should asynchronously load new filters from modules ', async(() { | |
| 328 initRouter((Router router, ViewFactory views) { | |
| 329 views.configure({ | |
| 330 'foo': ngRoute( | |
| 331 path: '/foo', | |
| 332 modules: () => new Future.value([ | |
| 333 new Module()..type(HelloFilter) | |
| 334 ]), | |
| 335 view: 'foo.html' | |
| 336 ), | |
| 337 }); | |
| 338 }); | |
| 339 _.injector.get(TemplateCache) | |
| 340 .put('foo.html', new HttpResponse(200, '<div>{{\'World\' | hello}}</di
v>')); | |
| 341 | |
| 342 Element root = _.compile('<ng-view></ng-view>'); | |
| 343 expect(root.text).toEqual(''); | |
| 344 | |
| 345 router.route('/foo'); | |
| 346 microLeap(); | |
| 347 _.rootScope.apply(); | |
| 348 | |
| 349 expect(root.text).toEqual('Hello, World!'); | |
| 350 })); | 35 })); |
| 351 | 36 |
| 352 }); | 37 }); |
| 353 } | 38 } |
| 354 | 39 |
| 355 var _router; | 40 class TestRouteInitializer implements RouteInitializer { |
| 356 var _initRoutesCalls = 0; | 41 int calledInit = 0; |
| 42 Router router; |
| 357 | 43 |
| 358 void initRoutes(Router router, ViewFactory view) { | 44 void init(Router router, ViewFactory view) { |
| 359 _initRoutesCalls++; | 45 calledInit++; |
| 360 _router = router; | 46 this.router = router; |
| 361 } | |
| 362 | |
| 363 @NgDirective(selector: '[make-it-new]') | |
| 364 class NewDirective { | |
| 365 NewDirective(Element element) { | |
| 366 element.innerHtml = 'New!'; | |
| 367 } | 47 } |
| 368 } | 48 } |
| 369 | |
| 370 @NgFilter(name:'hello') | |
| 371 class HelloFilter { | |
| 372 call(String str) { | |
| 373 return 'Hello, $str!'; | |
| 374 } | |
| 375 } | |
| 376 | |
| OLD | NEW |