forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.spec.ts
154 lines (124 loc) · 5.4 KB
/
button.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {it, describe, expect, beforeEach, inject} from '@angular/core/testing';
import {TestComponentBuilder} from '@angular/compiler/testing';
import {Component} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdButton, MdAnchor} from './button';
describe('MdButton', () => {
let builder: TestComponentBuilder;
beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
builder = tcb;
}));
// General button tests
it('should apply class based on color attribute', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('button'));
let aDebugElement = fixture.debugElement.query(By.css('a'));
testComponent.buttonColor = 'primary';
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
expect(aDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
testComponent.buttonColor = 'accent';
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
expect(aDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
done();
});
});
it('should should not clear previous defined classes', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('button'));
buttonDebugElement.nativeElement.classList.add('custom-class');
testComponent.buttonColor = 'primary';
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
testComponent.buttonColor = 'accent';
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(false);
expect(buttonDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
done();
});
});
// Regular button tests
describe('button[md-button]', () => {
it('should handle a click on the button', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('button'));
buttonDebugElement.nativeElement.click();
expect(testComponent.clickCount).toBe(1);
done();
});
});
it('should not increment if disabled', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('button'));
testComponent.isDisabled = true;
fixture.detectChanges();
buttonDebugElement.nativeElement.click();
expect(testComponent.clickCount).toBe(0);
done();
});
});
});
// Anchor button tests
describe('a[md-button]', () => {
it('should not redirect if disabled', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('a'));
testComponent.isDisabled = true;
fixture.detectChanges();
buttonDebugElement.nativeElement.click();
// will error if page reloads
done();
});
});
it('should remove tabindex if disabled', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('a'));
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe(null);
testComponent.isDisabled = true;
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe('-1');
done();
});
});
it('should add aria-disabled attribute if disabled', (done: () => void) => {
return builder.createAsync(TestApp).then(fixture => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('a'));
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.getAttribute('aria-disabled')).toBe('false');
testComponent.isDisabled = true;
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.getAttribute('aria-disabled')).toBe('true');
done();
});
});
});
});
/** Test component that contains an MdButton. */
@Component({
selector: 'test-app',
template: `
<button md-button type="button" (click)="increment()"
[disabled]="isDisabled" [color]="buttonColor">
Go
</button>
<a href="http://www.google.com" md-button [disabled]="isDisabled" [color]="buttonColor">Link</a>
`,
directives: [MdButton, MdAnchor]
})
class TestApp {
clickCount: number = 0;
isDisabled: boolean = false;
increment() {
this.clickCount++;
}
}