|
| 1 | +import { |
| 2 | + AsyncTestCompleter, |
| 3 | + TestComponentBuilder, |
| 4 | + beforeEach, |
| 5 | + beforeEachBindings, |
| 6 | + ddescribe, |
| 7 | + describe, |
| 8 | + el, |
| 9 | + expect, |
| 10 | + iit, |
| 11 | + inject, |
| 12 | + it, |
| 13 | + xit, |
| 14 | +} from 'angular2/test_lib'; |
| 15 | +import {DebugElement} from 'angular2/src/core/debug/debug_element'; |
| 16 | + |
| 17 | +import {Component, View, ViewMetadata, UrlResolver, bind} from 'angular2/core'; |
| 18 | + |
| 19 | +import {MdButton, MdAnchor} from 'angular2_material/src/components/button/button'; |
| 20 | + |
| 21 | +import {TestUrlResolver} from './test_url_resolver'; |
| 22 | + |
| 23 | +import {XHR} from 'angular2/src/core/render/xhr'; |
| 24 | +import {XHRImpl} from 'angular2/src/core/render/xhr_impl'; |
| 25 | + |
| 26 | + |
| 27 | +export function main() { |
| 28 | + describe('MdButton', () => { |
| 29 | + let builder: TestComponentBuilder; |
| 30 | + |
| 31 | + beforeEachBindings(() => [ |
| 32 | + // Need a custom URL resolver for ng-material template files in order for them to work |
| 33 | + // with both JS and Dart output. |
| 34 | + bind(UrlResolver) |
| 35 | + .toValue(new TestUrlResolver()), |
| 36 | + |
| 37 | + // Need to use the real XHR implementation (instead of the mock) so we can actually request |
| 38 | + // the template files, since Angular 2 doesn't have anything like $templateCache. This should |
| 39 | + // eventually be replaced with a preprocessor that inlines templates. |
| 40 | + bind(XHR).toClass(XHRImpl) |
| 41 | + ]); |
| 42 | + |
| 43 | + beforeEach(inject([TestComponentBuilder], (tcb) => { builder = tcb; })); |
| 44 | + |
| 45 | + describe('button[md-button]', () => { |
| 46 | + it('should handle a click on the button', inject([AsyncTestCompleter], (async) => { |
| 47 | + builder.createAsync(TestApp).then(rootTestComponent => { |
| 48 | + let testComponent = rootTestComponent.debugElement.componentInstance; |
| 49 | + let buttonDebugElement = |
| 50 | + getChildDebugElement(rootTestComponent.debugElement, 'button'); |
| 51 | + |
| 52 | + buttonDebugElement.nativeElement.click(); |
| 53 | + expect(testComponent.clickCount).toBe(1); |
| 54 | + |
| 55 | + async.done(); |
| 56 | + }); |
| 57 | + })); |
| 58 | + |
| 59 | + it('should disable the button', inject([AsyncTestCompleter], (async) => { |
| 60 | + builder.createAsync(TestApp).then(rootTestComponent => { |
| 61 | + let testAppComponent = rootTestComponent.debugElement.componentInstance; |
| 62 | + let buttonDebugElement = |
| 63 | + getChildDebugElement(rootTestComponent.debugElement, 'button'); |
| 64 | + let buttonElement = buttonDebugElement.nativeElement; |
| 65 | + |
| 66 | + // The button should initially be enabled. |
| 67 | + expect(buttonElement.disabled).toBe(false); |
| 68 | + |
| 69 | + // After the disabled binding has been changed. |
| 70 | + testAppComponent.isDisabled = true; |
| 71 | + rootTestComponent.detectChanges(); |
| 72 | + |
| 73 | + // The button should should now be disabled. |
| 74 | + expect(buttonElement.disabled).toBe(true); |
| 75 | + |
| 76 | + // Clicking the button should not invoke the handler. |
| 77 | + buttonElement.click(); |
| 78 | + expect(testAppComponent.clickCount).toBe(0); |
| 79 | + async.done(); |
| 80 | + }); |
| 81 | + })); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('a[md-button]', () => { |
| 85 | + const anchorTemplate = `<a md-button href="http://google.com" [disabled]="isDisabled">Go</a>`; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + builder = builder.overrideView( |
| 89 | + TestApp, new ViewMetadata({template: anchorTemplate, directives: [MdAnchor]})); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should remove disabled anchors from tab order', inject([AsyncTestCompleter], (async) => { |
| 93 | + builder.createAsync(TestApp).then(rootTestComponent => { |
| 94 | + let testAppComponent = rootTestComponent.debugElement.componentInstance; |
| 95 | + let anchorDebugElement = getChildDebugElement(rootTestComponent.debugElement, 'a'); |
| 96 | + let anchorElement = anchorDebugElement.nativeElement; |
| 97 | + |
| 98 | + // The anchor should initially be in the tab order. |
| 99 | + expect(anchorElement.tabIndex).toBe(0); |
| 100 | + |
| 101 | + // After the disabled binding has been changed. |
| 102 | + testAppComponent.isDisabled = true; |
| 103 | + rootTestComponent.detectChanges(); |
| 104 | + |
| 105 | + // The anchor should now be out of the tab order. |
| 106 | + expect(anchorElement.tabIndex).toBe(-1); |
| 107 | + |
| 108 | + async.done(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should preventDefault for disabled anchor clicks', |
| 112 | + inject([AsyncTestCompleter], (async) => { |
| 113 | + // No clear way to test this; see https://github.com/angular/angular/issues/3782 |
| 114 | + async.done(); |
| 115 | + })); |
| 116 | + })); |
| 117 | + }); |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +/** Gets a child DebugElement by tag name. */ |
| 122 | +function getChildDebugElement(parent: DebugElement, tagName: string): DebugElement { |
| 123 | + return parent.query(debugEl => debugEl.nativeElement.tagName.toLowerCase() == tagName); |
| 124 | +} |
| 125 | + |
| 126 | +/** Test component that contains an MdButton. */ |
| 127 | +@Component({selector: 'test-app'}) |
| 128 | +@View({ |
| 129 | + directives: [MdButton], |
| 130 | + template: |
| 131 | + `<button md-button type="button" (click)="increment()" [disabled]="isDisabled">Go</button>` |
| 132 | +}) |
| 133 | +class TestApp { |
| 134 | + clickCount: number = 0; |
| 135 | + isDisabled: boolean = false; |
| 136 | + |
| 137 | + increment() { |
| 138 | + this.clickCount++; |
| 139 | + } |
| 140 | +} |
0 commit comments