Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocomplete): panel not being shown with delay and OnPush change detection #3977

Merged
merged 1 commit into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {TestBed, async, fakeAsync, tick, ComponentFixture} from '@angular/core/testing';
import {Component, OnDestroy, QueryList, ViewChild, ViewChildren} from '@angular/core';
import {
Component,
OnDestroy,
QueryList,
ViewChild,
ViewChildren,
ChangeDetectionStrategy,
OnInit,
} from '@angular/core';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MdAutocompleteModule, MdAutocompleteTrigger} from './index';
Expand Down Expand Up @@ -38,7 +46,8 @@ describe('MdAutocomplete', () => {
SimpleAutocomplete,
AutocompleteWithoutForms,
NgIfAutocomplete,
AutocompleteWithNgModel
AutocompleteWithNgModel,
AutocompleteWithOnPushDelay
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -1090,6 +1099,24 @@ describe('MdAutocomplete', () => {
expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(500);

});

it('should show the panel when the options are initialized later within a component with ' +
'OnPush change detection', fakeAsync(() => {
let fixture = TestBed.createComponent(AutocompleteWithOnPushDelay);

fixture.detectChanges();
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
tick(1000);
fixture.detectChanges();

Promise.resolve().then(() => {
let panel = overlayContainerElement.querySelector('.mat-autocomplete-panel') as HTMLElement;
let visibleClass = 'mat-autocomplete-visible';

fixture.detectChanges();
expect(panel.classList).toContain(visibleClass, `Expected panel to be visible.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this Promise ever resolve? Don't you need another flushMicroTasks to make it run?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the tick calls flushMicrotasks under the hood.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a tick should automatically clear the event-loop queue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that you're calling tick before kicking off the promise.

Copy link
Member Author

@crisbeto crisbeto Apr 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reconfirmed that it resolves and the test breaks as expected if I remove the newly-added markForCheck.

});
}));
});

@Component({
Expand Down Expand Up @@ -1239,6 +1266,30 @@ class AutocompleteWithNgModel {

}


@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<md-input-container>
<input type="text" mdInput [mdAutocomplete]="auto">
</md-input-container>

<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let option of options" [value]="option">{{ option }}</md-option>
</md-autocomplete>
`
})
class AutocompleteWithOnPushDelay implements OnInit {
options: string[];

ngOnInit() {
setTimeout(() => {
this.options = ['One'];
}, 1000);
}
}


/** This is a mock keyboard event to test keyboard events in the autocomplete. */
class MockKeyboardEvent {
constructor(public keyCode: number) {}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
QueryList,
TemplateRef,
ViewChild,
ViewEncapsulation
ViewEncapsulation,
ChangeDetectorRef,
} from '@angular/core';
import {MdOption} from '../core';
import {ActiveDescendantKeyManager} from '../core/a11y/activedescendant-key-manager';
Expand Down Expand Up @@ -52,6 +53,8 @@ export class MdAutocomplete implements AfterContentInit {
/** Unique ID to be used by autocomplete trigger's "aria-owns" property. */
id: string = `md-autocomplete-${_uniqueAutocompleteIdCounter++}`;

constructor(private _changeDetectorRef: ChangeDetectorRef) { }

ngAfterContentInit() {
this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap();
}
Expand All @@ -68,7 +71,10 @@ export class MdAutocomplete implements AfterContentInit {

/** Panel should hide itself when the option list is empty. */
_setVisibility() {
Promise.resolve().then(() => this.showPanel = !!this.options.length);
Promise.resolve().then(() => {
this.showPanel = !!this.options.length;
this._changeDetectorRef.markForCheck();
});
}

/** Sets a class on the panel based on its position (used to set y-offset). */
Expand Down