|
| 1 | +import { |
| 2 | + AsyncTestCompleter, |
| 3 | + beforeEach, |
| 4 | + ddescribe, |
| 5 | + xdescribe, |
| 6 | + describe, |
| 7 | + dispatchEvent, |
| 8 | + expect, |
| 9 | + iit, |
| 10 | + inject, |
| 11 | + IS_DARTIUM, |
| 12 | + beforeEachBindings, |
| 13 | + it, |
| 14 | + xit, |
| 15 | + TestComponentBuilder, |
| 16 | + proxy, |
| 17 | + SpyObject, |
| 18 | + By |
| 19 | +} from 'angular2/test_lib'; |
| 20 | + |
| 21 | +import {IMPLEMENTS, print} from 'angular2/src/facade/lang'; |
| 22 | + |
| 23 | +import {bind, Component, View} from 'angular2/angular2'; |
| 24 | + |
| 25 | +import {Location, Router, RouterLink} from 'angular2/router'; |
| 26 | + |
| 27 | +import { |
| 28 | + DOM |
| 29 | +} from 'angular2/src/dom/dom_adapter' |
| 30 | + |
| 31 | + |
| 32 | + export function |
| 33 | + main() { |
| 34 | + describe('router-link directive', function() { |
| 35 | + |
| 36 | + beforeEachBindings( |
| 37 | + () => |
| 38 | + [bind(Location).toValue(makeDummyLocation()), bind(Router).toValue(makeDummyRouter())]); |
| 39 | + |
| 40 | + |
| 41 | + it('should update a[href] attribute', |
| 42 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb, async) => { |
| 43 | + |
| 44 | + tcb.createAsync(TestComponent) |
| 45 | + .then((testComponent) => { |
| 46 | + testComponent.detectChanges(); |
| 47 | + let anchorElement = testComponent.query(By.css('a')).nativeElement; |
| 48 | + expect(DOM.getAttribute(anchorElement, 'href')).toEqual('/detail'); |
| 49 | + async.done(); |
| 50 | + }); |
| 51 | + })); |
| 52 | + |
| 53 | + |
| 54 | + it('should call router.navigate when a link is clicked', |
| 55 | + inject([TestComponentBuilder, AsyncTestCompleter, Router], (tcb, async, router) => { |
| 56 | + |
| 57 | + tcb.createAsync(TestComponent) |
| 58 | + .then((testComponent) => { |
| 59 | + testComponent.detectChanges(); |
| 60 | + // TODO: shouldn't this be just 'click' rather than '^click'? |
| 61 | + testComponent.query(By.css('a')).triggerEventHandler('^click', {}); |
| 62 | + expect(router.spy("navigate")).toHaveBeenCalledWith('/detail'); |
| 63 | + async.done(); |
| 64 | + }); |
| 65 | + })); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +@Component({selector: 'test-component'}) |
| 71 | +@View({ |
| 72 | + template: ` |
| 73 | + <div> |
| 74 | + <a [router-link]="['/detail']">detail view</a> |
| 75 | + </div>`, |
| 76 | + directives: [RouterLink] |
| 77 | +}) |
| 78 | +class TestComponent { |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +@proxy |
| 83 | +@IMPLEMENTS(Location) |
| 84 | +class DummyLocation extends SpyObject { |
| 85 | + noSuchMethod(m) { return super.noSuchMethod(m) } |
| 86 | +} |
| 87 | + |
| 88 | +function makeDummyLocation() { |
| 89 | + var dl = new DummyLocation(); |
| 90 | + dl.spy('normalizeAbsolutely').andCallFake((url) => url); |
| 91 | + return dl; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +@proxy |
| 96 | +@IMPLEMENTS(Router) |
| 97 | +class DummyRouter extends SpyObject { |
| 98 | + noSuchMethod(m) { return super.noSuchMethod(m) } |
| 99 | +} |
| 100 | + |
| 101 | +function makeDummyRouter() { |
| 102 | + var dr = new DummyRouter(); |
| 103 | + dr.spy('generate').andCallFake((routeParams) => routeParams.join('=')); |
| 104 | + dr.spy('navigate'); |
| 105 | + return dr; |
| 106 | +} |
0 commit comments