Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: third_party/pkg/route_hierarchical/test/client_test.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 import 'dart:async';
2 import 'dart:html';
3
4 import 'package:unittest/unittest.dart';
5 import 'package:unittest/mock.dart';
6 import 'package:unittest/html_enhanced_config.dart';
7 import 'package:route_hierarchical/client.dart';
8 import 'package:route_hierarchical/url_pattern.dart';
9
10 import 'mocks.dart';
11
12 main() {
13 useHtmlEnhancedConfiguration();
14
15 test('paths are routed to routes added with addRoute', () {
16 Router router = new Router();
17 router.root.addRoute(
18 name: 'foo',
19 path: '/foo',
20 enter: expectAsync1((RouteEvent e) {
21 expect(e.path, '/foo');
22 }));
23 router.route('/foo');
24 });
25
26 group('hierarchical routing', () {
27
28 _testParentChild(
29 Pattern parentPath,
30 Pattern childPath,
31 String expectedParentPath,
32 String expectedChildPath,
33 String testPath) {
34 Router router = new Router();
35 router.root.addRoute(
36 name: 'parent',
37 path: parentPath,
38 enter: expectAsync1((RouteEvent e) {
39 expect(e.path, expectedParentPath);
40 }),
41 mount: (Route child) {
42 child.addRoute(
43 name: 'child',
44 path: childPath,
45 enter: expectAsync1((RouteEvent e) {
46 expect(e.path, expectedChildPath);
47 }));
48 });
49 router.route(testPath);
50 }
51
52 test('child router with UrlPattern', () {
53 _testParentChild(
54 new UrlPattern(r'/foo/(\w+)'),
55 new UrlPattern(r'/bar'),
56 '/foo/abc',
57 '/bar',
58 '/foo/abc/bar');
59 });
60
61 test('child router with Strings', () {
62 _testParentChild(
63 '/foo',
64 '/bar',
65 '/foo',
66 '/bar',
67 '/foo/bar');
68 });
69
70 });
71
72 group('leave', () {
73
74 test('should leave previous route and enter new', () {
75 Map<String, int> counters = <String, int>{
76 'fooEnter': 0,
77 'fooLeave': 0,
78 'barEnter': 0,
79 'barLeave': 0,
80 'bazEnter': 0,
81 'bazLeave': 0
82 };
83 Router router = new Router();
84 router.root
85 ..addRoute(path: '/foo',
86 name: 'foo',
87 enter: (RouteEvent e) => counters['fooEnter']++,
88 leave: (RouteEvent e) => counters['fooLeave']++,
89 mount: (Route route) => route
90 ..addRoute(path: '/bar',
91 name: 'bar',
92 enter: (RouteEvent e) => counters['barEnter']++,
93 leave: (RouteEvent e) => counters['barLeave']++)
94 ..addRoute(path: '/baz',
95 name: 'baz',
96 enter: (RouteEvent e) => counters['bazEnter']++,
97 leave: (RouteEvent e) => counters['bazLeave']++));
98
99 expect(counters, {
100 'fooEnter': 0,
101 'fooLeave': 0,
102 'barEnter': 0,
103 'barLeave': 0,
104 'bazEnter': 0,
105 'bazLeave': 0
106 });
107
108 router.route('/foo/bar').then(expectAsync1((_) {
109 expect(counters, {
110 'fooEnter': 1,
111 'fooLeave': 0,
112 'barEnter': 1,
113 'barLeave': 0,
114 'bazEnter': 0,
115 'bazLeave': 0
116 });
117
118 router.route('/foo/baz').then(expectAsync1((_) {
119 expect(counters, {
120 'fooEnter': 1,
121 'fooLeave': 0,
122 'barEnter': 1,
123 'barLeave': 1,
124 'bazEnter': 1,
125 'bazLeave': 0
126 });
127 }));
128 }));
129 });
130
131 _testAllowLeave(bool allowLeave) {
132 Completer<bool> completer = new Completer<bool>();
133 bool barEntered = false;
134 bool bazEntered = false;
135
136 Router router = new Router();
137 router.root
138 ..addRoute(name: 'foo', path: '/foo',
139 mount: (Route child) => child
140 ..addRoute(name: 'bar', path: '/bar',
141 enter: (RouteEvent e) => barEntered = true,
142 leave: (RouteEvent e) => e.allowLeave(completer.future))
143 ..addRoute(name: 'baz', path: '/baz',
144 enter: (RouteEvent e) => bazEntered = true));
145
146 router.route('/foo/bar').then(expectAsync1((_) {
147 expect(barEntered, true);
148 expect(bazEntered, false);
149 router.route('/foo/baz').then(expectAsync1((_) {
150 expect(bazEntered, allowLeave);
151 }));
152 completer.complete(allowLeave);
153 }));
154 }
155
156 test('should allow navigation', () {
157 _testAllowLeave(true);
158 });
159
160 test('should veto navigation', () {
161 _testAllowLeave(false);
162 });
163 });
164
165 group('Default route', () {
166
167 _testHeadTail(String path, String expectFoo, String expectBar) {
168 Router router = new Router();
169 router.root
170 ..addRoute(
171 name: 'foo',
172 path: '/foo',
173 defaultRoute: true,
174 enter: expectAsync1((RouteEvent e) {
175 expect(e.path, expectFoo);
176 }),
177 mount: (child) => child
178 ..addRoute(
179 name: 'bar',
180 path: '/bar',
181 defaultRoute: true,
182 enter: expectAsync1((RouteEvent e) =>
183 expect(e.path, expectBar))));
184
185 router.route(path);
186 }
187
188 test('should calculate head/tail of empty route', () {
189 _testHeadTail('', '', '');
190 });
191
192 test('should calculate head/tail of partial route', () {
193 _testHeadTail('/foo', '/foo', '');
194 });
195
196 test('should calculate head/tail of a route', () {
197 _testHeadTail('/foo/bar', '/foo', '/bar');
198 });
199
200 test('should calculate head/tail of an invalid parent route', () {
201 _testHeadTail('/garbage/bar', '', '');
202 });
203
204 test('should calculate head/tail of an invalid child route', () {
205 _testHeadTail('/foo/garbage', '/foo', '');
206 });
207
208 test('should follow default routes', () {
209 Map<String, int> counters = <String, int>{
210 'list_entered': 0,
211 'article_123_entered': 0,
212 'article_123_view_entered': 0,
213 'article_123_edit_entered': 0
214 };
215
216 Router router = new Router();
217 router.root
218 ..addRoute(
219 name: 'articles',
220 path: '/articles',
221 defaultRoute: true,
222 enter: (_) => counters['list_entered']++)
223 ..addRoute(
224 name: 'article',
225 path: '/article/123',
226 enter: (_) => counters['article_123_entered']++,
227 mount: (Route child) => child
228 ..addRoute(
229 name: 'viewArticles',
230 path: '/view',
231 defaultRoute: true,
232 enter: (_) => counters['article_123_view_entered']++)
233 ..addRoute(
234 name: 'editArticles',
235 path: '/edit',
236 enter: (_) => counters['article_123_edit_entered']++));
237
238 router.route('').then((_) {
239 expect(counters, {
240 'list_entered': 1, // default to list
241 'article_123_entered': 0,
242 'article_123_view_entered': 0,
243 'article_123_edit_entered': 0
244 });
245 router.route('/articles').then((_) {
246 expect(counters, {
247 'list_entered': 2,
248 'article_123_entered': 0,
249 'article_123_view_entered': 0,
250 'article_123_edit_entered': 0
251 });
252 router.route('/article/123').then((_) {
253 expect(counters, {
254 'list_entered': 2,
255 'article_123_entered': 1,
256 'article_123_view_entered': 1, // default to view
257 'article_123_edit_entered': 0
258 });
259 router.route('/article/123/view').then((_) {
260 expect(counters, {
261 'list_entered': 2,
262 'article_123_entered': 1,
263 'article_123_view_entered': 2,
264 'article_123_edit_entered': 0
265 });
266 router.route('/article/123/edit').then((_) {
267 expect(counters, {
268 'list_entered': 2,
269 'article_123_entered': 1,
270 'article_123_view_entered': 2,
271 'article_123_edit_entered': 1
272 });
273 });
274 });
275 });
276 });
277 });
278 });
279
280 });
281
282 group('go', () {
283
284 test('shoud location.assign/replace when useFragment=true', () {
285 MockWindow mockWindow = new MockWindow();
286 Router router = new Router(useFragment: true, windowImpl: mockWindow);
287 router.root
288 ..addRoute(
289 name: 'articles',
290 path: '/articles');
291
292 router.go('articles', {}).then(expectAsync1((_) {
293 var mockLocation = mockWindow.location;
294
295 mockLocation.getLogs(callsTo('assign', anything))
296 .verify(happenedExactly(1));
297 expect(mockLocation.getLogs(callsTo('assign', anything)).last.args,
298 ['#/articles']);
299 mockLocation.getLogs(callsTo('replace', anything))
300 .verify(happenedExactly(0));
301
302 router.go('articles', {}, replace: true).then(expectAsync1((_) {
303 mockLocation.getLogs(callsTo('replace', anything))
304 .verify(happenedExactly(1));
305 expect(mockLocation.getLogs(callsTo('replace', anything)).last.args,
306 ['#/articles']);
307 mockLocation.getLogs(callsTo('assign', anything))
308 .verify(happenedExactly(1));
309 }));
310 }));
311 });
312
313 test('shoud history.push/replaceState when useFragment=false', () {
314 MockWindow mockWindow = new MockWindow();
315 Router router = new Router(useFragment: false, windowImpl: mockWindow);
316 router.root
317 ..addRoute(
318 name: 'articles',
319 path: '/articles');
320
321 router.go('articles', {}).then(expectAsync1((_) {
322 var mockHistory = mockWindow.history;
323
324 mockHistory.getLogs(callsTo('pushState', anything))
325 .verify(happenedExactly(1));
326 expect(mockHistory.getLogs(callsTo('pushState', anything)).last.args,
327 [null, '', '/articles']);
328 mockHistory.getLogs(callsTo('replaceState', anything))
329 .verify(happenedExactly(0));
330
331 router.go('articles', {}, replace: true).then(expectAsync1((_) {
332 mockHistory.getLogs(callsTo('replaceState', anything))
333 .verify(happenedExactly(1));
334 expect(mockHistory.getLogs(callsTo('replaceState', anything)).last.arg s,
335 [null, '', '/articles']);
336 mockHistory.getLogs(callsTo('pushState', anything))
337 .verify(happenedExactly(1));
338 }));
339 }));
340 });
341
342 test('should work with hierarchical go', () {
343 MockWindow mockWindow = new MockWindow();
344 Router router = new Router(windowImpl: mockWindow);
345 router.root
346 ..addRoute(
347 name: 'a',
348 path: '/:foo',
349 mount: (child) => child
350 ..addRoute(
351 name: 'b',
352 path: '/:bar'));
353
354 var routeA = router.root.getRoute('a');
355
356 router.go('a.b', {}).then(expectAsync1((_) {
357 var mockHistory = mockWindow.history;
358
359 mockHistory.getLogs(callsTo('pushState', anything))
360 .verify(happenedExactly(1));
361 expect(mockHistory.getLogs(callsTo('pushState', anything)).last.args,
362 [null, '', '/null/null']);
363
364 router.go('a.b', {'foo': 'aaaa', 'bar': 'bbbb'}).then(expectAsync1((_) {
365 mockHistory.getLogs(callsTo('pushState', anything))
366 .verify(happenedExactly(2));
367 expect(mockHistory.getLogs(callsTo('pushState', anything)).last.args,
368 [null, '', '/aaaa/bbbb']);
369
370 router.go('b', {'bar': 'bbbb'}, startingFrom: routeA)
371 .then(expectAsync1((_) {
372 mockHistory.getLogs(callsTo('pushState', anything))
373 .verify(happenedExactly(3));
374 expect(
375 mockHistory.getLogs(callsTo('pushState')).last.args,
376 [null, '', '/aaaa/bbbb']);
377 }));
378
379 }));
380 }));
381
382 });
383
384 test('should attempt to reverse default routes', () {
385 Map<String, int> counters = <String, int>{
386 'aEnter': 0,
387 'bEnter': 0
388 };
389
390 MockWindow mockWindow = new MockWindow();
391 Router router = new Router(windowImpl: mockWindow);
392 router.root
393 ..addRoute(
394 name: 'a',
395 defaultRoute: true,
396 path: '/:foo',
397 enter: (_) => counters['aEnter']++,
398 mount: (child) => child
399 ..addRoute(
400 name: 'b',
401 defaultRoute: true,
402 path: '/:bar',
403 enter: (_) => counters['bEnter']++));
404
405 expect(counters, {
406 'aEnter': 0,
407 'bEnter': 0
408 });
409
410 router.route('').then((_) {
411 expect(counters, {
412 'aEnter': 1,
413 'bEnter': 1
414 });
415
416 var routeA = router.root.getRoute('a');
417 router.go('b', {'bar': 'bbb'}, startingFrom: routeA).then((_) {
418 var mockHistory = mockWindow.history;
419
420 mockHistory.getLogs(callsTo('pushState', anything))
421 .verify(happenedExactly(1));
422 expect(mockHistory.getLogs(callsTo('pushState', anything)).last.args,
423 [null, '', '/null/bbb']);
424 });
425 });
426 });
427
428 });
429
430 group('url', () {
431
432 test('should reconstruct url', () {
433 MockWindow mockWindow = new MockWindow();
434 Router router = new Router(windowImpl: mockWindow);
435 router.root
436 ..addRoute(
437 name: 'a',
438 defaultRoute: true,
439 path: '/:foo',
440 mount: (child) => child
441 ..addRoute(
442 name: 'b',
443 defaultRoute: true,
444 path: '/:bar'));
445
446 var routeA = router.root.getRoute('a');
447
448 router.route('').then((_) {
449 expect(router.url('a.b'), '/null/null');
450 expect(router.url('a.b', parameters: {'foo': 'aaa'}), '/aaa/null');
451 expect(router.url('b', parameters: {'bar': 'bbb'},
452 startingFrom: routeA), '/null/bbb');
453
454 router.route('/foo/bar').then((_) {
455 expect(router.url('a.b'), '/foo/bar');
456 expect(router.url('a.b', parameters: {'foo': 'aaa'}), '/aaa/bar');
457 expect(router.url('b', parameters: {'bar': 'bbb'},
458 startingFrom: routeA), '/foo/bbb');
459 expect(router.url('b', parameters: {'foo': 'aaa', 'bar': 'bbb'},
460 startingFrom: routeA), '/foo/bbb');
461
462 expect(router.url('b', parameters: {'bar': 'bbb', 'b.param1': 'val1'},
463 startingFrom: routeA), '/foo/bbb?b.param1=val1');
464
465 });
466 });
467 });
468
469 });
470
471 group('getRoute', () {
472
473 test('should return correct routes', () {
474 Route routeFoo, routeBar, routeBaz, routeQux, routeAux;
475
476 Router router = new Router();
477 router.root
478 ..addRoute(
479 name: 'foo',
480 path: '/:foo',
481 mount: (child) => routeFoo = child
482 ..addRoute(
483 name: 'bar',
484 path: '/:bar',
485 mount: (child) => routeBar = child
486 ..addRoute(
487 name: 'baz',
488 path: '/:baz',
489 mount: (child) => routeBaz = child))
490 ..addRoute(
491 name: 'qux',
492 path: '/:qux',
493 mount: (child) => routeQux = child
494 ..addRoute(
495 name: 'aux',
496 path: '/:aux',
497 mount: (child) => routeAux = child)));
498
499 expect(router.root.getRoute('foo'), same(routeFoo));
500 expect(router.root.getRoute('foo.bar'), same(routeBar));
501 expect(routeFoo.getRoute('bar'), same(routeBar));
502 expect(router.root.getRoute('foo.bar.baz'), same(routeBaz));
503 expect(router.root.getRoute('foo.qux'), same(routeQux));
504 expect(router.root.getRoute('foo.qux.aux'), same(routeAux));
505 expect(routeQux.getRoute('aux'), same(routeAux));
506 expect(routeFoo.getRoute('qux.aux'), same(routeAux));
507
508 expect(router.root.getRoute('baz'), isNull);
509 expect(router.root.getRoute('foo.baz'), isNull);
510 });
511
512 });
513
514 group('route', () {
515
516 test('should parse query', () {
517 Router router = new Router();
518 router.root
519 ..addRoute(
520 name: 'foo',
521 path: '/:foo',
522 enter: expectAsync1((RouteEvent e) {
523 expect(e.parameters, {
524 'foo': '123',
525 'a': 'b',
526 'b': '',
527 'c': 'foo bar'
528 });
529 }));
530
531 router.route('/123?foo.a=b&foo.b=&foo.c=foo%20bar&foo.=ignore');
532 });
533
534 group('isActive', () {
535
536 test('should currectly identify active/inactive routes', () {
537 Router router = new Router();
538 router.root
539 ..addRoute(
540 name: 'foo',
541 path: '/foo',
542 mount: (child) => child
543 ..addRoute(
544 name: 'bar',
545 path: '/bar',
546 mount: (child) => child
547 ..addRoute(
548 name: 'baz',
549 path: '/baz',
550 mount: (child) => child))
551 ..addRoute(
552 name: 'qux',
553 path: '/qux',
554 mount: (child) => child
555 ..addRoute(
556 name: 'aux',
557 path: '/aux',
558 mount: (child) => child)));
559
560 expect(r(router, 'foo').isActive, false);
561 expect(r(router, 'foo.bar').isActive, false);
562 expect(r(router, 'foo.bar.baz').isActive, false);
563 expect(r(router, 'foo.qux').isActive, false);
564
565 return router.route('/foo').then((_) {
566 expect(r(router, 'foo').isActive, true);
567 expect(r(router, 'foo.bar').isActive, false);
568 expect(r(router, 'foo.bar.baz').isActive, false);
569 expect(r(router, 'foo.qux').isActive, false);
570
571 return router.route('/foo/qux').then((_) {
572 expect(r(router, 'foo').isActive, true);
573 expect(r(router, 'foo.bar').isActive, false);
574 expect(r(router, 'foo.bar.baz').isActive, false);
575 expect(r(router, 'foo.qux').isActive, true);
576
577 return router.route('/foo/bar/baz').then((_) {
578 expect(r(router, 'foo').isActive, true);
579 expect(r(router, 'foo.bar').isActive, true);
580 expect(r(router, 'foo.bar.baz').isActive, true);
581 expect(r(router, 'foo.qux').isActive, false);
582 });
583 });
584 });
585 });
586
587 });
588
589 group('parameters', () {
590
591 test('should return path parameters for routes', () {
592 Router router = new Router();
593 router.root
594 ..addRoute(
595 name: 'foo',
596 path: '/:foo',
597 mount: (child) => child
598 ..addRoute(
599 name: 'bar',
600 path: '/:bar',
601 mount: (child) => child
602 ..addRoute(
603 name: 'baz',
604 path: '/:baz',
605 mount: (child) => child)));
606
607 expect(r(router, 'foo').parameters, isNull);
608 expect(r(router, 'foo.bar').parameters, isNull);
609 expect(r(router, 'foo.bar.baz').parameters, isNull);
610
611 return router.route('/aaa').then((_) {
612 expect(r(router, 'foo').parameters, {'foo': 'aaa'});
613 expect(r(router, 'foo.bar').parameters, isNull);
614 expect(r(router, 'foo.bar.baz').parameters, isNull);
615
616 return router.route('/aaa/bbb').then((_) {
617 expect(r(router, 'foo').parameters, {'foo': 'aaa'});
618 expect(r(router, 'foo.bar').parameters, {'bar': 'bbb'});
619 expect(r(router, 'foo.bar.baz').parameters, isNull);
620
621 return router.route('/aaa/bbb/ccc').then((_) {
622 expect(r(router, 'foo').parameters, {'foo': 'aaa'});
623 expect(r(router, 'foo.bar').parameters, {'bar': 'bbb'});
624 expect(r(router, 'foo.bar.baz').parameters, {'baz': 'ccc'});
625 });
626 });
627 });
628 });
629
630 });
631
632 });
633
634 group('activePath', () {
635
636 test('should currectly identify active path', () {
637 Router router = new Router();
638 router.root
639 ..addRoute(
640 name: 'foo',
641 path: '/foo',
642 mount: (child) => child
643 ..addRoute(
644 name: 'bar',
645 path: '/bar',
646 mount: (child) => child
647 ..addRoute(
648 name: 'baz',
649 path: '/baz',
650 mount: (child) => child))
651 ..addRoute(
652 name: 'qux',
653 path: '/qux',
654 mount: (child) => child
655 ..addRoute(
656 name: 'aux',
657 path: '/aux',
658 mount: (child) => child)));
659
660 var strPath = (List<Route> path) =>
661 path.map((Route r) => r.name).join('.');
662
663 expect(strPath(router.activePath), '');
664
665 return router.route('/foo').then((_) {
666 expect(strPath(router.activePath), 'foo');
667
668 return router.route('/foo/qux').then((_) {
669 expect(strPath(router.activePath), 'foo.qux');
670
671 return router.route('/foo/bar/baz').then((_) {
672 expect(strPath(router.activePath), 'foo.bar.baz');
673 });
674 });
675 });
676 });
677
678 });
679
680 }
681
682 /// An alias for Router.root.getRoute(path)
683 r(Router router, String path) => router.root.getRoute(path);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698