@@ -16,7 +16,11 @@ describe('MdSlideToggle without forms', () => {
16
16
beforeEach ( async ( ( ) => {
17
17
TestBed . configureTestingModule ( {
18
18
imports : [ MdSlideToggleModule ] ,
19
- declarations : [ SlideToggleBasic , SlideToggleWithTabindexAttr ] ,
19
+ declarations : [
20
+ SlideToggleBasic ,
21
+ SlideToggleWithTabindexAttr ,
22
+ SlideToggleWithoutLabel
23
+ ] ,
20
24
providers : [
21
25
{ provide : HAMMER_GESTURE_CONFIG , useFactory : ( ) => gestureConfig = new TestGestureConfig ( ) }
22
26
]
@@ -493,6 +497,62 @@ describe('MdSlideToggle without forms', () => {
493
497
expect ( slideThumbContainer . classList ) . not . toContain ( 'mat-dragging' ) ;
494
498
} ) ) ;
495
499
} ) ;
500
+
501
+ describe ( 'without label' , ( ) => {
502
+ let fixture : ComponentFixture < SlideToggleWithoutLabel > ;
503
+ let testComponent : SlideToggleWithoutLabel ;
504
+ let slideToggleElement : HTMLElement ;
505
+ let slideToggleBarElement : HTMLElement ;
506
+
507
+ beforeEach ( ( ) => {
508
+ fixture = TestBed . createComponent ( SlideToggleWithoutLabel ) ;
509
+
510
+ const slideToggleDebugEl = fixture . debugElement . query ( By . directive ( MdSlideToggle ) ) ;
511
+
512
+ testComponent = fixture . componentInstance ;
513
+ slideToggleElement = slideToggleDebugEl . nativeElement ;
514
+ slideToggleBarElement = slideToggleDebugEl
515
+ . query ( By . css ( '.mat-slide-toggle-bar' ) ) . nativeElement ;
516
+ } ) ;
517
+
518
+ it ( 'should remove margin for slide-toggle without a label' , ( ) => {
519
+ fixture . detectChanges ( ) ;
520
+
521
+ expect ( slideToggleBarElement . classList )
522
+ . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
523
+ } ) ;
524
+
525
+ it ( 'should not remove margin if initial label is set through binding' , async ( ( ) => {
526
+ testComponent . label = 'Some content' ;
527
+ fixture . detectChanges ( ) ;
528
+
529
+ expect ( slideToggleBarElement . classList )
530
+ . not . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
531
+ } ) ) ;
532
+
533
+ it ( 'should re-add margin if label is added asynchronously' , async ( ( ) => {
534
+ fixture . detectChanges ( ) ;
535
+
536
+ expect ( slideToggleBarElement . classList )
537
+ . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
538
+
539
+ testComponent . label = 'Some content' ;
540
+ fixture . detectChanges ( ) ;
541
+
542
+ // Wait for the MutationObserver to detect the content change and for the cdkObserveContent
543
+ // to emit the change event to the slide-toggle.
544
+ setTimeout ( ( ) => {
545
+ // The MutationObserver from the cdkObserveContent directive detected the content change
546
+ // and notified the slide-toggle component. The slide-toggle then marks the component as
547
+ // dirty by calling `markForCheck()`. This needs to be reflected by the component template
548
+ // then.
549
+ fixture . detectChanges ( ) ;
550
+
551
+ expect ( slideToggleElement . classList )
552
+ . not . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
553
+ } , 1 ) ;
554
+ } ) ) ;
555
+ } ) ;
496
556
} ) ;
497
557
498
558
describe ( 'MdSlideToggle with forms' , ( ) => {
@@ -806,3 +866,10 @@ class SlideToggleWithFormControl {
806
866
template : `<md-slide-toggle tabindex="5"></md-slide-toggle>`
807
867
} )
808
868
class SlideToggleWithTabindexAttr { }
869
+
870
+ @Component ( {
871
+ template : `<md-slide-toggle>{{label}}</md-slide-toggle>`
872
+ } )
873
+ class SlideToggleWithoutLabel {
874
+ label : string ;
875
+ }
0 commit comments