@@ -765,19 +765,57 @@ describe('MdCheckbox', () => {
765
765
} ) ;
766
766
767
767
describe ( 'without label' , ( ) => {
768
- let checkboxDebugElement : DebugElement ;
769
- let checkboxNativeElement : HTMLElement ;
768
+ let testComponent : CheckboxWithoutLabel ;
769
+ let checkboxElement : HTMLElement ;
770
+ let checkboxInnerContainer : HTMLElement ;
770
771
771
- it ( 'should add a css class to inner-container to remove side margin' , ( ) => {
772
+ beforeEach ( ( ) => {
772
773
fixture = TestBed . createComponent ( CheckboxWithoutLabel ) ;
774
+
775
+ const checkboxDebugEl = fixture . debugElement . query ( By . directive ( MdCheckbox ) ) ;
776
+
777
+ testComponent = fixture . componentInstance ;
778
+ checkboxElement = checkboxDebugEl . nativeElement ;
779
+ checkboxInnerContainer = checkboxDebugEl
780
+ . query ( By . css ( '.mat-checkbox-inner-container' ) ) . nativeElement ;
781
+ } ) ;
782
+
783
+ it ( 'should remove margin for checkbox without a label' , ( ) => {
773
784
fixture . detectChanges ( ) ;
774
- checkboxDebugElement = fixture . debugElement . query ( By . directive ( MdCheckbox ) ) ;
775
- checkboxNativeElement = checkboxDebugElement . nativeElement ;
776
785
777
- let checkboxInnerContainerWithoutMarginCount = checkboxNativeElement
778
- . querySelectorAll ( '.mat-checkbox-inner-container-no-side-margin' ) . length ;
779
- expect ( checkboxInnerContainerWithoutMarginCount ) . toBe ( 1 ) ;
786
+ expect ( checkboxInnerContainer . classList )
787
+ . toContain ( 'mat-checkbox-inner-container-no-side-margin' ) ;
780
788
} ) ;
789
+
790
+ it ( 'should not remove margin if initial label is set through binding' , async ( ( ) => {
791
+ testComponent . label = 'Some content' ;
792
+ fixture . detectChanges ( ) ;
793
+
794
+ expect ( checkboxInnerContainer . classList )
795
+ . not . toContain ( 'mat-checkbox-inner-container-no-side-margin' ) ;
796
+ } ) ) ;
797
+
798
+ it ( 'should re-add margin if label is added asynchronously' , async ( ( ) => {
799
+ fixture . detectChanges ( ) ;
800
+
801
+ expect ( checkboxInnerContainer . classList )
802
+ . toContain ( 'mat-checkbox-inner-container-no-side-margin' ) ;
803
+
804
+ testComponent . label = 'Some content' ;
805
+ fixture . detectChanges ( ) ;
806
+
807
+ // Wait for the MutationObserver to detect the content change and for the cdkObserveContent
808
+ // to emit the change event to the checkbox.
809
+ setTimeout ( ( ) => {
810
+ // The MutationObserver from the cdkObserveContent directive detected the content change
811
+ // and notified the checkbox component. The checkbox then marks the component as dirty
812
+ // by calling `markForCheck()`. This needs to be reflected by the component template then.
813
+ fixture . detectChanges ( ) ;
814
+
815
+ expect ( checkboxInnerContainer . classList )
816
+ . not . toContain ( 'mat-checkbox-inner-container-no-side-margin' ) ;
817
+ } , 1 ) ;
818
+ } ) ) ;
781
819
} ) ;
782
820
} ) ;
783
821
@@ -889,6 +927,8 @@ class CheckboxWithFormControl {
889
927
890
928
/** Test component without label */
891
929
@Component ( {
892
- template : `<md-checkbox></md-checkbox>`
930
+ template : `<md-checkbox>{{ label }} </md-checkbox>`
893
931
} )
894
- class CheckboxWithoutLabel { }
932
+ class CheckboxWithoutLabel {
933
+ label : string ;
934
+ }
0 commit comments