11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2- import { Component , Input } from '@angular/core' ;
2+ import { Component , Input , getDebugNode } from '@angular/core' ;
33import { ActionsSubject , Action } from '@ngrx/store' ;
44import { Observable } from 'rxjs/Observable' ;
55import { BehaviorSubject } from 'rxjs/BehaviorSubject' ;
@@ -9,6 +9,7 @@ import 'rxjs/add/operator/skip';
99import { FormControlState , createFormControlState } from '../../state' ;
1010import { SetValueAction } from '../../actions' ;
1111import { NgrxFormsModule } from '../../module' ;
12+ import { NgrxSelectControlValueAccessor } from '../../value-accessors' ;
1213
1314const SELECT_OPTIONS = [ 'op1' , 'op2' ] ;
1415
@@ -25,12 +26,13 @@ export class SelectComponent {
2526describe ( SelectComponent . name , ( ) => {
2627 let component : SelectComponent ;
2728 let fixture : ComponentFixture < SelectComponent > ;
29+ let valueAccessor : NgrxSelectControlValueAccessor ;
2830 let actionsSubject : ActionsSubject ;
2931 let actions$ : Observable < Action > ;
3032 let element : HTMLSelectElement ;
3133 const FORM_CONTROL_ID = 'test ID' ;
3234 const INITIAL_FORM_CONTROL_VALUE = SELECT_OPTIONS [ 1 ] ;
33- const INITIAL_STATE = createFormControlState < string > ( FORM_CONTROL_ID , INITIAL_FORM_CONTROL_VALUE ) ;
35+ const INITIAL_STATE = createFormControlState ( FORM_CONTROL_ID , INITIAL_FORM_CONTROL_VALUE ) ;
3436
3537 beforeEach ( ( ) => {
3638 actionsSubject = new BehaviorSubject < Action > ( { type : '' } ) as ActionsSubject ;
@@ -50,11 +52,12 @@ describe(SelectComponent.name, () => {
5052 component = fixture . componentInstance ;
5153 component . state = INITIAL_STATE ;
5254 element = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'select' ) as HTMLSelectElement ;
55+ valueAccessor = getDebugNode ( element ) ! . injector . get ( NgrxSelectControlValueAccessor ) ;
5356 fixture . detectChanges ( ) ;
5457 } ) ;
5558
5659 it ( 'should select the correct option initially' , ( ) => {
57- expect ( element . value ) . toBe ( INITIAL_FORM_CONTROL_VALUE ) ;
60+ expect ( valueAccessor . value ) . toBe ( INITIAL_FORM_CONTROL_VALUE ) ;
5861 } ) ;
5962
6063 it ( 'should trigger a SetValueAction with the selected value when an option is selected' , done => {
@@ -66,5 +69,66 @@ describe(SelectComponent.name, () => {
6669 expect ( ( a as SetValueAction < string > ) . payload . value ) . toBe ( newValue ) ;
6770 done ( ) ;
6871 } ) ;
69- } , 200 ) ;
72+ } ) ;
73+ } ) ;
74+
75+ const SELECT_NUMBER_OPTIONS = [ 1 , 2 ] ;
76+
77+ @Component ( {
78+ // tslint:disable-next-line:component-selector
79+ selector : 'select-test' ,
80+ template : '<select [ngrxFormControlState]="state"><option *ngFor="let o of options" [ngValue]="o">{{o}}</option></select>' ,
81+ } )
82+ export class NgValueSelectComponent {
83+ @Input ( ) state : FormControlState < number > ;
84+ options = SELECT_NUMBER_OPTIONS ;
85+ }
86+
87+ describe ( NgValueSelectComponent . name , ( ) => {
88+ let component : NgValueSelectComponent ;
89+ let fixture : ComponentFixture < NgValueSelectComponent > ;
90+ let valueAccessor : NgrxSelectControlValueAccessor ;
91+ let actionsSubject : ActionsSubject ;
92+ let actions$ : Observable < Action > ;
93+ let element : HTMLSelectElement ;
94+ const FORM_CONTROL_ID = 'test ID' ;
95+ const INITIAL_FORM_CONTROL_VALUE = SELECT_NUMBER_OPTIONS [ 1 ] ;
96+ const INITIAL_STATE = createFormControlState ( FORM_CONTROL_ID , INITIAL_FORM_CONTROL_VALUE ) ;
97+
98+ beforeEach ( ( ) => {
99+ actionsSubject = new BehaviorSubject < Action > ( { type : '' } ) as ActionsSubject ;
100+ actions$ = actionsSubject as Observable < Action > ; // cast required due to mismatch of lift() function signature
101+ } ) ;
102+
103+ beforeEach ( async ( ( ) => {
104+ TestBed . configureTestingModule ( {
105+ imports : [ NgrxFormsModule ] ,
106+ declarations : [ NgValueSelectComponent ] ,
107+ providers : [ { provide : ActionsSubject , useValue : actionsSubject } ] ,
108+ } ) . compileComponents ( ) ;
109+ } ) ) ;
110+
111+ beforeEach ( ( ) => {
112+ fixture = TestBed . createComponent ( NgValueSelectComponent ) ;
113+ component = fixture . componentInstance ;
114+ component . state = INITIAL_STATE ;
115+ element = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'select' ) as HTMLSelectElement ;
116+ valueAccessor = getDebugNode ( element ) ! . injector . get ( NgrxSelectControlValueAccessor ) ;
117+ fixture . detectChanges ( ) ;
118+ } ) ;
119+
120+ it ( 'should select the correct option initially' , ( ) => {
121+ expect ( valueAccessor . value ) . toBe ( INITIAL_FORM_CONTROL_VALUE ) ;
122+ } ) ;
123+
124+ it ( 'should trigger a SetValueAction with the selected value when an option is selected' , done => {
125+ const newValue = SELECT_NUMBER_OPTIONS [ 0 ] ;
126+ element . selectedIndex = 0 ;
127+ element . dispatchEvent ( new Event ( 'change' ) ) ;
128+ actions$ . first ( ) . subscribe ( a => {
129+ expect ( a . type ) . toBe ( SetValueAction . TYPE ) ;
130+ expect ( ( a as SetValueAction < number > ) . payload . value ) . toBe ( newValue ) ;
131+ done ( ) ;
132+ } ) ;
133+ } ) ;
70134} ) ;
0 commit comments