|
1 | | -import {ComponentFixture, TestBed, async} from '@angular/core/testing'; |
| 1 | +import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing'; |
2 | 2 | import {Component, DebugElement} from '@angular/core'; |
3 | 3 | import {By} from '@angular/platform-browser'; |
4 | 4 | import {MatBadge, MatBadgeModule} from './index'; |
5 | 5 | import {ThemePalette} from '@angular/material/core'; |
6 | 6 |
|
7 | 7 | describe('MatBadge', () => { |
8 | 8 | let fixture: ComponentFixture<any>; |
9 | | - let testComponent: BadgeWithTextContent; |
10 | | - let badgeNativeElement; |
| 9 | + let testComponent: BadgeTestApp; |
| 10 | + let badgeNativeElement: HTMLElement; |
| 11 | + let badgeDebugElement: DebugElement; |
11 | 12 |
|
12 | | - beforeEach(async(() => { |
| 13 | + beforeEach(fakeAsync(() => { |
13 | 14 | TestBed.configureTestingModule({ |
14 | 15 | imports: [MatBadgeModule], |
15 | | - declarations: [BadgeWithTextContent], |
| 16 | + declarations: [BadgeTestApp], |
16 | 17 | }).compileComponents(); |
17 | | - })); |
18 | 18 |
|
19 | | - beforeEach(() => { |
20 | | - fixture = TestBed.createComponent(BadgeWithTextContent); |
| 19 | + fixture = TestBed.createComponent(BadgeTestApp); |
21 | 20 | testComponent = fixture.debugElement.componentInstance; |
22 | 21 | fixture.detectChanges(); |
| 22 | + |
| 23 | + badgeDebugElement = fixture.debugElement.query(By.directive(MatBadge)); |
| 24 | + badgeNativeElement = badgeDebugElement.nativeElement; |
| 25 | + })); |
| 26 | + |
| 27 | + it('should update the badge based on attribute', () => { |
| 28 | + let badgeContentDebugElement = badgeNativeElement.querySelector('.mat-badge-content')!; |
| 29 | + |
| 30 | + expect(badgeContentDebugElement.textContent).toContain('1'); |
| 31 | + |
| 32 | + testComponent.badgeContent = '22'; |
| 33 | + fixture.detectChanges(); |
| 34 | + |
| 35 | + badgeContentDebugElement = badgeNativeElement.querySelector('.mat-badge-content')!; |
| 36 | + expect(badgeContentDebugElement.textContent).toContain('22'); |
23 | 37 | }); |
24 | 38 |
|
25 | | - describe('MatBadge Text', () => { |
26 | | - let badgeDebugElement: DebugElement; |
| 39 | + it('should apply class based on color attribute', () => { |
| 40 | + testComponent.badgeColor = 'primary'; |
| 41 | + fixture.detectChanges(); |
| 42 | + expect(badgeNativeElement.classList.contains('mat-badge-primary')).toBe(true); |
27 | 43 |
|
28 | | - beforeEach(() => { |
29 | | - badgeDebugElement = fixture.debugElement.query(By.directive(MatBadge)); |
30 | | - badgeNativeElement = badgeDebugElement.nativeElement; |
31 | | - fixture.detectChanges(); |
32 | | - }); |
| 44 | + testComponent.badgeColor = 'accent'; |
| 45 | + fixture.detectChanges(); |
| 46 | + expect(badgeNativeElement.classList.contains('mat-badge-accent')).toBe(true); |
33 | 47 |
|
34 | | - it('should update the badge based on attribute', () => { |
35 | | - let badgeContentDebugElement = badgeNativeElement.querySelector('.mat-badge-content'); |
| 48 | + testComponent.badgeColor = 'warn'; |
| 49 | + fixture.detectChanges(); |
| 50 | + expect(badgeNativeElement.classList.contains('mat-badge-warn')).toBe(true); |
36 | 51 |
|
37 | | - expect(badgeContentDebugElement.textContent).toContain('1'); |
| 52 | + testComponent.badgeColor = undefined; |
| 53 | + fixture.detectChanges(); |
38 | 54 |
|
39 | | - testComponent.badgeContent = '22'; |
40 | | - fixture.detectChanges(); |
| 55 | + expect(badgeNativeElement.classList).not.toContain('mat-badge-accent'); |
| 56 | + }); |
41 | 57 |
|
42 | | - badgeContentDebugElement = badgeNativeElement.querySelector('.mat-badge-content'); |
43 | | - expect(badgeContentDebugElement.textContent).toContain('22'); |
44 | | - }); |
| 58 | + it('should update the badge position on direction change', () => { |
| 59 | + expect(badgeNativeElement.classList.contains('mat-badge-above')).toBe(true); |
| 60 | + expect(badgeNativeElement.classList.contains('mat-badge-after')).toBe(true); |
45 | 61 |
|
46 | | - it('should apply class based on color attribute', () => { |
47 | | - testComponent.badgeColor = 'primary'; |
48 | | - fixture.detectChanges(); |
49 | | - expect(badgeNativeElement.classList.contains('mat-badge-primary')).toBe(true); |
| 62 | + testComponent.badgeDirection = 'below before'; |
| 63 | + fixture.detectChanges(); |
50 | 64 |
|
51 | | - testComponent.badgeColor = 'accent'; |
52 | | - fixture.detectChanges(); |
53 | | - expect(badgeNativeElement.classList.contains('mat-badge-accent')).toBe(true); |
| 65 | + expect(badgeNativeElement.classList.contains('mat-badge-below')).toBe(true); |
| 66 | + expect(badgeNativeElement.classList.contains('mat-badge-before')).toBe(true); |
| 67 | + }); |
54 | 68 |
|
55 | | - testComponent.badgeColor = 'warn'; |
56 | | - fixture.detectChanges(); |
57 | | - expect(badgeNativeElement.classList.contains('mat-badge-warn')).toBe(true); |
| 69 | + it('should change visibility to hidden', () => { |
| 70 | + expect(badgeNativeElement.classList.contains('mat-badge-hidden')).toBe(false); |
58 | 71 |
|
59 | | - testComponent.badgeColor = undefined; |
60 | | - fixture.detectChanges(); |
| 72 | + testComponent.badgeHidden = true; |
| 73 | + fixture.detectChanges(); |
61 | 74 |
|
62 | | - expect(badgeNativeElement.classList).not.toContain('mat-badge-accent'); |
63 | | - }); |
| 75 | + expect(badgeNativeElement.classList.contains('mat-badge-hidden')).toBe(true); |
| 76 | + }); |
64 | 77 |
|
65 | | - it('should update the badge position on direction change', () => { |
66 | | - expect(badgeNativeElement.classList.contains('mat-badge-above')).toBe(true); |
67 | | - expect(badgeNativeElement.classList.contains('mat-badge-after')).toBe(true); |
| 78 | + it('should change badge sizes', () => { |
| 79 | + expect(badgeNativeElement.classList.contains('mat-badge-medium')).toBe(true); |
68 | 80 |
|
69 | | - testComponent.badgeDirection = 'below before'; |
70 | | - fixture.detectChanges(); |
| 81 | + testComponent.badgeSize = 'small'; |
| 82 | + fixture.detectChanges(); |
71 | 83 |
|
72 | | - expect(badgeNativeElement.classList.contains('mat-badge-below')).toBe(true); |
73 | | - expect(badgeNativeElement.classList.contains('mat-badge-before')).toBe(true); |
74 | | - }); |
| 84 | + expect(badgeNativeElement.classList.contains('mat-badge-small')).toBe(true); |
75 | 85 |
|
76 | | - it('should change visibility to hidden', () => { |
77 | | - expect(badgeNativeElement.classList.contains('mat-badge-hidden')).toBe(false); |
| 86 | + testComponent.badgeSize = 'large'; |
| 87 | + fixture.detectChanges(); |
78 | 88 |
|
79 | | - testComponent.badgeHidden = true; |
80 | | - fixture.detectChanges(); |
| 89 | + expect(badgeNativeElement.classList.contains('mat-badge-large')).toBe(true); |
| 90 | + }); |
81 | 91 |
|
82 | | - expect(badgeNativeElement.classList.contains('mat-badge-hidden')).toBe(true); |
83 | | - }); |
| 92 | + it('should change badge overlap', () => { |
| 93 | + expect(badgeNativeElement.classList.contains('mat-badge-overlap')).toBe(false); |
84 | 94 |
|
85 | | - it('should change badge sizes', () => { |
86 | | - expect(badgeNativeElement.classList.contains('mat-badge-medium')).toBe(true); |
| 95 | + testComponent.badgeOverlap = true; |
| 96 | + fixture.detectChanges(); |
87 | 97 |
|
88 | | - testComponent.badgeSize = 'small'; |
89 | | - fixture.detectChanges(); |
| 98 | + expect(badgeNativeElement.classList.contains('mat-badge-overlap')).toBe(true); |
| 99 | + }); |
90 | 100 |
|
91 | | - expect(badgeNativeElement.classList.contains('mat-badge-small')).toBe(true); |
| 101 | + it('should toggle `aria-describedby` depending on whether the badge has a description', () => { |
| 102 | + const badgeContent = badgeNativeElement.querySelector('.mat-badge-content')!; |
92 | 103 |
|
93 | | - testComponent.badgeSize = 'large'; |
94 | | - fixture.detectChanges(); |
| 104 | + expect(badgeContent.getAttribute('aria-describedby')).toBeFalsy(); |
95 | 105 |
|
96 | | - expect(badgeNativeElement.classList.contains('mat-badge-large')).toBe(true); |
97 | | - }); |
| 106 | + testComponent.badgeDescription = 'Describing a badge'; |
| 107 | + fixture.detectChanges(); |
98 | 108 |
|
99 | | - it('should change badge overlap', () => { |
100 | | - expect(badgeNativeElement.classList.contains('mat-badge-overlap')).toBe(false); |
| 109 | + expect(badgeContent.getAttribute('aria-describedby')).toBeTruthy(); |
101 | 110 |
|
102 | | - testComponent.badgeOverlap = true; |
103 | | - fixture.detectChanges(); |
| 111 | + testComponent.badgeDescription = ''; |
| 112 | + fixture.detectChanges(); |
104 | 113 |
|
105 | | - expect(badgeNativeElement.classList.contains('mat-badge-overlap')).toBe(true); |
106 | | - }); |
| 114 | + expect(badgeContent.getAttribute('aria-describedby')).toBeFalsy(); |
107 | 115 | }); |
108 | 116 |
|
109 | 117 | }); |
110 | 118 |
|
111 | 119 | /** Test component that contains a MatBadge. */ |
112 | 120 | @Component({ |
113 | | - selector: 'test-app', |
114 | 121 | template: ` |
115 | 122 | <span [matBadge]="badgeContent" |
116 | 123 | [matBadgeColor]="badgeColor" |
117 | 124 | [matBadgePosition]="badgeDirection" |
118 | 125 | [matBadgeHidden]="badgeHidden" |
119 | 126 | [matBadgeSize]="badgeSize" |
120 | | - [matBadgeOverlap]="badgeOverlap"> |
| 127 | + [matBadgeOverlap]="badgeOverlap" |
| 128 | + [matBadgeDescription]="badgeDescription"> |
121 | 129 | home |
122 | 130 | </span> |
123 | 131 | ` |
124 | 132 | }) |
125 | | -class BadgeWithTextContent { |
| 133 | +class BadgeTestApp { |
126 | 134 | badgeColor: ThemePalette; |
127 | 135 | badgeContent = '1'; |
128 | 136 | badgeDirection = 'above after'; |
129 | 137 | badgeHidden = false; |
130 | 138 | badgeSize = 'medium'; |
131 | 139 | badgeOverlap = false; |
| 140 | + badgeDescription: string; |
132 | 141 | } |
0 commit comments