44 */
55
66import { Component , DebugElement } from '@angular/core' ;
7- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
7+ import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
88import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
99import { By } from '@angular/platform-browser' ;
1010import { provideNoopAnimations } from '@angular/platform-browser/animations' ;
@@ -55,6 +55,14 @@ describe('nz-segmented', () => {
5555 expect ( segmentedElement . classList ) . toContain ( 'ant-segmented-sm' ) ;
5656 } ) ;
5757
58+ it ( 'should support vertical mode' , ( ) => {
59+ const segmentedElement : HTMLElement = segmentedComponent . nativeElement ;
60+ expect ( segmentedElement . classList ) . not . toContain ( 'ant-segmented-vertical' ) ;
61+ component . vertical = true ;
62+ fixture . detectChanges ( ) ;
63+ expect ( segmentedElement . classList ) . toContain ( 'ant-segmented-vertical' ) ;
64+ } ) ;
65+
5866 it ( 'should be auto selected the first option when if no value is set' , async ( ) => {
5967 const theFirstElement = getSegmentedOptionByIndex ( 0 ) ;
6068 await fixture . whenStable ( ) ;
@@ -132,6 +140,33 @@ describe('nz-segmented', () => {
132140 expect ( theFirstElement . classList ) . toContain ( 'ant-segmented-item-selected' ) ;
133141 expect ( theSecondElement . classList ) . not . toContain ( 'ant-segmented-item-selected' ) ;
134142 } ) ;
143+
144+ it ( 'should animate thumb correctly in vertical mode' , fakeAsync ( ( ) => {
145+ component . vertical = true ;
146+ fixture . detectChanges ( ) ;
147+
148+ const segmentedComponentInstance = fixture . debugElement . query (
149+ By . directive ( NzSegmentedComponent )
150+ ) . componentInstance ;
151+
152+ expect ( segmentedComponentInstance . nzVertical ) . toBe ( true ) ;
153+
154+ const theSecondElement = getSegmentedOptionByIndex ( 1 ) ;
155+
156+ tick ( 100 ) ;
157+ fixture . detectChanges ( ) ;
158+
159+ dispatchMouseEvent ( theSecondElement , 'click' ) ;
160+ tick ( 100 ) ;
161+ fixture . detectChanges ( ) ;
162+
163+ expect ( theSecondElement . classList ) . toContain ( 'ant-segmented-item-selected' ) ;
164+ expect ( segmentedComponentInstance . animationState ) . toBeDefined ( ) ;
165+
166+ if ( segmentedComponentInstance . animationState ) {
167+ expect ( [ 'fromVertical' , 'toVertical' ] ) . toContain ( segmentedComponentInstance . animationState . value ) ;
168+ }
169+ } ) ) ;
135170 } ) ;
136171
137172 describe ( 'ng model' , ( ) => {
@@ -217,6 +252,54 @@ describe('nz-segmented', () => {
217252 expect ( theThirdElement . classList ) . toContain ( 'ant-segmented-item-selected' ) ;
218253 } ) ;
219254 } ) ;
255+
256+ describe ( 'vertical segmented' , ( ) => {
257+ let fixture : ComponentFixture < NzSegmentedVerticalTestComponent > ;
258+ let component : NzSegmentedVerticalTestComponent ;
259+ let segmentedComponent : DebugElement ;
260+
261+ function getSegmentedOptionByIndex ( index : number ) : HTMLElement {
262+ return segmentedComponent . nativeElement . querySelectorAll ( '.ant-segmented-item' ) [ index ] ;
263+ }
264+
265+ beforeEach ( ( ) => {
266+ TestBed . configureTestingModule ( {
267+ providers : [ provideNoopAnimations ( ) ]
268+ } ) ;
269+ fixture = TestBed . createComponent ( NzSegmentedVerticalTestComponent ) ;
270+ component = fixture . componentInstance ;
271+ spyOn ( component , 'handleValueChange' ) ;
272+ segmentedComponent = fixture . debugElement . query ( By . directive ( NzSegmentedComponent ) ) ;
273+ fixture . detectChanges ( ) ;
274+ } ) ;
275+
276+ it ( 'should render in vertical mode' , ( ) => {
277+ const segmentedElement : HTMLElement = segmentedComponent . nativeElement ;
278+ expect ( segmentedElement . classList ) . toContain ( 'ant-segmented-vertical' ) ;
279+ const groupElement = segmentedElement . querySelector ( '.ant-segmented-group' ) as HTMLElement ;
280+ expect ( groupElement ) . toBeTruthy ( ) ;
281+ } ) ;
282+
283+ it ( 'should change selection in vertical mode' , async ( ) => {
284+ const theFirstElement = getSegmentedOptionByIndex ( 0 ) ;
285+ const theSecondElement = getSegmentedOptionByIndex ( 1 ) ;
286+
287+ await fixture . whenStable ( ) ;
288+ fixture . detectChanges ( ) ;
289+
290+ expect ( theFirstElement . classList ) . toContain ( 'ant-segmented-item-selected' ) ;
291+ expect ( component . handleValueChange ) . toHaveBeenCalledTimes ( 0 ) ;
292+
293+ dispatchMouseEvent ( theSecondElement , 'click' ) ;
294+ await fixture . whenStable ( ) ;
295+ fixture . detectChanges ( ) ;
296+
297+ expect ( theFirstElement . classList ) . not . toContain ( 'ant-segmented-item-selected' ) ;
298+ expect ( theSecondElement . classList ) . toContain ( 'ant-segmented-item-selected' ) ;
299+ expect ( component . handleValueChange ) . toHaveBeenCalledTimes ( 1 ) ;
300+ expect ( component . handleValueChange ) . toHaveBeenCalledWith ( 'Weekly' ) ;
301+ } ) ;
302+ } ) ;
220303} ) ;
221304
222305@Component ( {
@@ -227,15 +310,17 @@ describe('nz-segmented', () => {
227310 [nzOptions]="options"
228311 [nzDisabled]="disabled"
229312 [nzBlock]="block"
313+ [nzVertical]="vertical"
230314 (nzValueChange)="handleValueChange($event)"
231- ></nz-segmented >
315+ / >
232316 `
233317} )
234318export class NzSegmentedTestComponent {
235319 size : NzSizeLDSType = 'default' ;
236320 options : NzSegmentedOptions = [ 1 , 2 , 3 ] ;
237321 block = false ;
238322 disabled = false ;
323+ vertical = false ;
239324
240325 handleValueChange ( _e : string | number ) : void {
241326 // empty
@@ -263,3 +348,15 @@ export class NzSegmentedInReactiveFormTestComponent {
263348 options = [ 'Daily' , 'Weekly' , 'Monthly' , 'Quarterly' , 'Yearly' ] ;
264349 formControl = new FormControl ( 'Weekly' ) ;
265350}
351+
352+ @Component ( {
353+ imports : [ FormsModule , NzSegmentedModule ] ,
354+ template : ` <nz-segmented [nzOptions]="options" [nzVertical]="true" (nzValueChange)="handleValueChange($event)" /> `
355+ } )
356+ export class NzSegmentedVerticalTestComponent {
357+ options = [ 'Daily' , 'Weekly' , 'Monthly' , 'Quarterly' , 'Yearly' ] ;
358+
359+ handleValueChange ( _e : string | number ) : void {
360+ return void 0 ;
361+ }
362+ }
0 commit comments