5
5
6
6
import { Component , DebugElement } from '@angular/core' ;
7
7
import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
8
- import { FormsModule } from '@angular/forms' ;
8
+ import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
9
9
import { By } from '@angular/platform-browser' ;
10
10
import { provideNoopAnimations } from '@angular/platform-browser/animations' ;
11
11
@@ -112,6 +112,36 @@ describe('nz-segmented', () => {
112
112
expect ( component . value ) . toBe ( 'Daily' ) ;
113
113
} ) ) ;
114
114
} ) ;
115
+ describe ( 'in reactive form' , ( ) => {
116
+ let fixture : ComponentFixture < NzSegmentedInReactiveFormTestComponent > ;
117
+ let component : NzSegmentedInReactiveFormTestComponent ;
118
+ let segmentedComponent : DebugElement ;
119
+
120
+ function getSegmentedOptionByIndex ( index : number ) : HTMLElement {
121
+ return segmentedComponent . nativeElement . querySelectorAll ( '.ant-segmented-item' ) [ index ] ;
122
+ }
123
+
124
+ beforeEach ( ( ) => {
125
+ TestBed . configureTestingModule ( {
126
+ providers : [ provideNoopAnimations ( ) ]
127
+ } ) ;
128
+ fixture = TestBed . createComponent ( NzSegmentedInReactiveFormTestComponent ) ;
129
+ component = fixture . componentInstance ;
130
+ segmentedComponent = fixture . debugElement . query ( By . directive ( NzSegmentedComponent ) ) ;
131
+ fixture . detectChanges ( ) ;
132
+ } ) ;
133
+
134
+ it ( 'first change form control value should work' , fakeAsync ( ( ) => {
135
+ expect ( component . formControl . value ) . toBe ( 'Weekly' ) ;
136
+ const theThirdElement = getSegmentedOptionByIndex ( 2 ) ;
137
+ dispatchMouseEvent ( theThirdElement . querySelector ( '.ant-segmented-item-label' ) ! , 'click' ) ;
138
+ tick ( 400 ) ;
139
+ fixture . detectChanges ( ) ;
140
+ const theSecondElement = getSegmentedOptionByIndex ( 1 ) ;
141
+ expect ( theSecondElement . classList . contains ( 'ant-segmented-item-selected' ) ) . toBeFalse ( ) ;
142
+ expect ( theThirdElement . classList . contains ( 'ant-segmented-item-selected' ) ) . toBeTrue ( ) ;
143
+ } ) ) ;
144
+ } ) ;
115
145
} ) ;
116
146
117
147
@Component ( {
@@ -138,3 +168,12 @@ export class NzSegmentedTestComponent {
138
168
// empty
139
169
}
140
170
}
171
+
172
+ @Component ( {
173
+ imports : [ ReactiveFormsModule , NzSegmentedModule ] ,
174
+ template : `<nz-segmented [nzOptions]="options" [formControl]="formControl"></nz-segmented>`
175
+ } )
176
+ export class NzSegmentedInReactiveFormTestComponent {
177
+ options = [ 'Daily' , 'Weekly' , 'Monthly' , 'Quarterly' , 'Yearly' ] ;
178
+ formControl = new FormControl ( 'Weekly' ) ;
179
+ }
0 commit comments