Skip to content

Commit 448e01e

Browse files
committed
fix(autocomplete): aria-expanded should be updated when panel hides
1 parent 4d4a63e commit 448e01e

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
114114

115115
/* Whether or not the autocomplete panel is open. */
116116
get panelOpen(): boolean {
117-
return this._panelOpen;
117+
return this._panelOpen && this.autocomplete.showPanel;
118118
}
119119

120120
/** Opens the autocomplete suggestion panel. */

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,39 @@ describe('MdAutocomplete', () => {
6666
input = fixture.debugElement.query(By.css('input')).nativeElement;
6767
});
6868

69-
it('should open the panel when the input is focused', () => {
69+
it('should open the panel when the input is focused', async(() => {
7070
expect(fixture.componentInstance.trigger.panelOpen)
7171
.toBe(false, `Expected panel state to start out closed.`);
7272

7373
dispatchFakeEvent(input, 'focus');
74-
fixture.detectChanges();
74+
fixture.whenStable().then(() => {
75+
fixture.detectChanges();
7576

76-
expect(fixture.componentInstance.trigger.panelOpen)
77-
.toBe(true, `Expected panel state to read open when input is focused.`);
78-
expect(overlayContainerElement.textContent)
79-
.toContain('Alabama', `Expected panel to display when input is focused.`);
80-
expect(overlayContainerElement.textContent)
81-
.toContain('California', `Expected panel to display when input is focused.`);
82-
});
77+
expect(fixture.componentInstance.trigger.panelOpen)
78+
.toBe(true, `Expected panel state to read open when input is focused.`);
79+
expect(overlayContainerElement.textContent)
80+
.toContain('Alabama', `Expected panel to display when input is focused.`);
81+
expect(overlayContainerElement.textContent)
82+
.toContain('California', `Expected panel to display when input is focused.`);
83+
});
84+
}));
8385

84-
it('should open the panel programmatically', () => {
86+
it('should open the panel programmatically', async(() => {
8587
expect(fixture.componentInstance.trigger.panelOpen)
8688
.toBe(false, `Expected panel state to start out closed.`);
8789

8890
fixture.componentInstance.trigger.openPanel();
89-
fixture.detectChanges();
91+
fixture.whenStable().then(() => {
92+
fixture.detectChanges();
9093

91-
expect(fixture.componentInstance.trigger.panelOpen)
92-
.toBe(true, `Expected panel state to read open when opened programmatically.`);
93-
expect(overlayContainerElement.textContent)
94-
.toContain('Alabama', `Expected panel to display when opened programmatically.`);
95-
expect(overlayContainerElement.textContent)
96-
.toContain('California', `Expected panel to display when opened programmatically.`);
97-
});
94+
expect(fixture.componentInstance.trigger.panelOpen)
95+
.toBe(true, `Expected panel state to read open when opened programmatically.`);
96+
expect(overlayContainerElement.textContent)
97+
.toContain('Alabama', `Expected panel to display when opened programmatically.`);
98+
expect(overlayContainerElement.textContent)
99+
.toContain('California', `Expected panel to display when opened programmatically.`);
100+
});
101+
}));
98102

99103
it('should close the panel when blurred', async(() => {
100104
dispatchFakeEvent(input, 'focus');
@@ -190,8 +194,6 @@ describe('MdAutocomplete', () => {
190194
fixture.whenStable().then(() => {
191195
fixture.detectChanges();
192196

193-
expect(fixture.componentInstance.trigger.panelOpen)
194-
.toBe(true, `Expected panel to stay open when options list is empty.`);
195197
expect(panel.classList)
196198
.toContain('mat-autocomplete-hidden', `Expected panel to hide itself when empty.`);
197199
});
@@ -753,20 +755,43 @@ describe('MdAutocomplete', () => {
753755
.toBe('false', 'Expected aria-expanded to be false while panel is closed.');
754756

755757
fixture.componentInstance.trigger.openPanel();
756-
fixture.detectChanges();
758+
fixture.whenStable().then(() => {
759+
fixture.detectChanges();
757760

758-
expect(input.getAttribute('aria-expanded'))
759-
.toBe('true', 'Expected aria-expanded to be true while panel is open.');
761+
expect(input.getAttribute('aria-expanded'))
762+
.toBe('true', 'Expected aria-expanded to be true while panel is open.');
760763

761-
fixture.componentInstance.trigger.closePanel();
762-
fixture.detectChanges();
764+
fixture.componentInstance.trigger.closePanel();
765+
fixture.detectChanges();
763766

764-
fixture.whenStable().then(() => {
765-
expect(input.getAttribute('aria-expanded'))
766-
.toBe('false', 'Expected aria-expanded to be false when panel closes again.');
767+
fixture.whenStable().then(() => {
768+
expect(input.getAttribute('aria-expanded'))
769+
.toBe('false', 'Expected aria-expanded to be false when panel closes again.');
770+
});
767771
});
768772
}));
769773

774+
it('should set aria-expanded properly when the panel is hidden', async(() => {
775+
fixture.componentInstance.trigger.openPanel();
776+
777+
fixture.whenStable().then(() => {
778+
fixture.detectChanges();
779+
expect(input.getAttribute('aria-expanded'))
780+
.toBe('true', 'Expected aria-expanded to be true while panel is open.');
781+
782+
typeInElement('zz', input);
783+
fixture.whenStable().then(() => {
784+
fixture.detectChanges();
785+
786+
fixture.whenStable().then(() => {
787+
fixture.detectChanges();
788+
expect(input.getAttribute('aria-expanded'))
789+
.toBe('false', 'Expected aria-expanded to be false when panel hides itself.');
790+
});
791+
});
792+
});
793+
}));
794+
770795
it('should set aria-owns based on the attached autocomplete', () => {
771796
fixture.componentInstance.trigger.openPanel();
772797
fixture.detectChanges();
@@ -880,21 +905,24 @@ describe('MdAutocomplete', () => {
880905
});
881906
}));
882907

883-
it('should work when input is wrapped in ngIf', () => {
908+
it('should work when input is wrapped in ngIf', async(() => {
884909
const fixture = TestBed.createComponent(NgIfAutocomplete);
885910
fixture.detectChanges();
886911

887912
const input = fixture.debugElement.query(By.css('input')).nativeElement;
888913
dispatchFakeEvent(input, 'focus');
889-
fixture.detectChanges();
890914

891-
expect(fixture.componentInstance.trigger.panelOpen)
892-
.toBe(true, `Expected panel state to read open when input is focused.`);
893-
expect(overlayContainerElement.textContent)
894-
.toContain('One', `Expected panel to display when input is focused.`);
895-
expect(overlayContainerElement.textContent)
896-
.toContain('Two', `Expected panel to display when input is focused.`);
897-
});
915+
fixture.whenStable().then(() => {
916+
fixture.detectChanges();
917+
918+
expect(fixture.componentInstance.trigger.panelOpen)
919+
.toBe(true, `Expected panel state to read open when input is focused.`);
920+
expect(overlayContainerElement.textContent)
921+
.toContain('One', `Expected panel to display when input is focused.`);
922+
expect(overlayContainerElement.textContent)
923+
.toContain('Two', `Expected panel to display when input is focused.`);
924+
});
925+
}));
898926

899927
});
900928
});

0 commit comments

Comments
 (0)