@@ -765,19 +765,57 @@ describe('MdCheckbox', () => {
765765 } ) ;
766766
767767 describe ( 'without label' , ( ) => {
768- let checkboxDebugElement : DebugElement ;
769- let checkboxNativeElement : HTMLElement ;
768+ let testComponent : CheckboxWithoutLabel ;
769+ let checkboxElement : HTMLElement ;
770+ let checkboxInnerContainer : HTMLElement ;
770771
771- it ( 'should add a css class to inner-container to remove side margin' , ( ) => {
772+ beforeEach ( ( ) => {
772773 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' , ( ) => {
773784 fixture . detectChanges ( ) ;
774- checkboxDebugElement = fixture . debugElement . query ( By . directive ( MdCheckbox ) ) ;
775- checkboxNativeElement = checkboxDebugElement . nativeElement ;
776785
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' ) ;
780788 } ) ;
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+ } ) ) ;
781819 } ) ;
782820} ) ;
783821
@@ -891,6 +929,8 @@ class CheckboxWithFormControl {
891929
892930/** Test component without label */
893931@Component ( {
894- template : `<md-checkbox></md-checkbox>`
932+ template : `<md-checkbox>{{ label }} </md-checkbox>`
895933} )
896- class CheckboxWithoutLabel { }
934+ class CheckboxWithoutLabel {
935+ label : string ;
936+ }
0 commit comments