1
- import { TestBed , ComponentFixture , async , inject } from '@angular/core/testing' ;
1
+ import { TestBed , ComponentFixture , async , fakeAsync , inject , tick } from '@angular/core/testing' ;
2
2
import { createGenericTestComponent , isBrowser } from '../test/common' ;
3
3
import { expectResults , getWindowLinks } from '../test/typeahead/common' ;
4
4
5
- import { Component , DebugElement , ViewChild } from '@angular/core' ;
5
+ import { Component , DebugElement , ViewChild , ChangeDetectionStrategy } from '@angular/core' ;
6
6
import { Validators , FormControl , FormGroup , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
7
7
import { By } from '@angular/platform-browser' ;
8
8
import { Observable } from 'rxjs/Rx' ;
9
9
import 'rxjs/add/operator/map' ;
10
+ import 'rxjs/add/operator/debounceTime' ;
10
11
11
12
import { NgbTypeahead } from './typeahead' ;
12
13
import { NgbTypeaheadModule } from './typeahead.module' ;
@@ -15,6 +16,9 @@ import {NgbTypeaheadConfig} from './typeahead-config';
15
16
const createTestComponent = ( html : string ) =>
16
17
createGenericTestComponent ( html , TestComponent ) as ComponentFixture < TestComponent > ;
17
18
19
+ const createOnPushTestComponent = ( html : string ) =>
20
+ createGenericTestComponent ( html , TestOnPushComponent ) as ComponentFixture < TestOnPushComponent > ;
21
+
18
22
enum Key {
19
23
Tab = 9 ,
20
24
Enter = 13 ,
@@ -62,8 +66,10 @@ function expectWindowResults(element, expectedResults: string[]) {
62
66
describe ( 'ngb-typeahead' , ( ) => {
63
67
64
68
beforeEach ( ( ) => {
65
- TestBed . configureTestingModule (
66
- { declarations : [ TestComponent ] , imports : [ NgbTypeaheadModule , FormsModule , ReactiveFormsModule ] } ) ;
69
+ TestBed . configureTestingModule ( {
70
+ declarations : [ TestComponent , TestOnPushComponent ] ,
71
+ imports : [ NgbTypeaheadModule , FormsModule , ReactiveFormsModule ]
72
+ } ) ;
67
73
} ) ;
68
74
69
75
describe ( 'valueaccessor' , ( ) => {
@@ -290,6 +296,15 @@ describe('ngb-typeahead', () => {
290
296
expectWindowResults ( compiled , [ '+ONE' , 'ONE MORE' ] ) ;
291
297
} ) ;
292
298
299
+ it ( 'should properly display results when an owning components using OnPush strategy' , fakeAsync ( ( ) => {
300
+ const fixture = createOnPushTestComponent ( `<input type="text" [(ngModel)]="model" [ngbTypeahead]="find"/>` ) ;
301
+ const compiled = fixture . nativeElement ;
302
+
303
+ changeInput ( compiled , 'o' ) ;
304
+ fixture . detectChanges ( ) ;
305
+ tick ( 250 ) ;
306
+ expectWindowResults ( compiled , [ '+one' , 'one more' ] ) ;
307
+ } ) ) ;
293
308
} ) ;
294
309
295
310
describe ( 'objects' , ( ) => {
@@ -588,3 +603,12 @@ class TestComponent {
588
603
589
604
onSelect ( $event ) { this . selectEventValue = $event ; }
590
605
}
606
+
607
+ @Component ( { selector : 'test-onpush-cmp' , changeDetection : ChangeDetectionStrategy . OnPush , template : '' } )
608
+ class TestOnPushComponent {
609
+ private _strings = [ 'one' , 'one more' , 'two' , 'three' ] ;
610
+
611
+ find = ( text$ : Observable < string > ) => {
612
+ return text$ . debounceTime ( 200 ) . map ( text => this . _strings . filter ( v => v . startsWith ( text ) ) ) ;
613
+ } ;
614
+ }
0 commit comments