@@ -22,11 +22,11 @@ describe('Observe content', () => {
2222
2323 // If the hint label is empty, expect no label.
2424 const spy = spyOn ( fixture . componentInstance , 'doSomething' ) . and . callFake ( ( ) => {
25- expect ( spy . calls . any ( ) ) . toBe ( true ) ;
25+ expect ( spy ) . toHaveBeenCalled ( ) ;
2626 done ( ) ;
2727 } ) ;
2828
29- expect ( spy . calls . any ( ) ) . toBe ( false ) ;
29+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
3030
3131 fixture . componentInstance . text = 'text' ;
3232 fixture . detectChanges ( ) ;
@@ -38,15 +38,43 @@ describe('Observe content', () => {
3838
3939 // If the hint label is empty, expect no label.
4040 const spy = spyOn ( fixture . componentInstance , 'doSomething' ) . and . callFake ( ( ) => {
41- expect ( spy . calls . any ( ) ) . toBe ( true ) ;
41+ expect ( spy ) . toHaveBeenCalled ( ) ;
4242 done ( ) ;
4343 } ) ;
4444
45- expect ( spy . calls . any ( ) ) . toBe ( false ) ;
45+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
4646
4747 fixture . componentInstance . text = 'text' ;
4848 fixture . detectChanges ( ) ;
4949 } ) ;
50+
51+ it ( 'should disconnect the MutationObserver when the directive is disabled' , ( ) => {
52+ const observeSpy = jasmine . createSpy ( 'observe spy' ) ;
53+ const disconnectSpy = jasmine . createSpy ( 'disconnect spy' ) ;
54+
55+ // Note: since we can't know exactly when the native MutationObserver will emit, we can't
56+ // test this scenario reliably without risking flaky tests, which is why we supply a mock
57+ // MutationObserver and check that the methods are called at the right time.
58+ TestBed . overrideProvider ( MutationObserverFactory , {
59+ deps : [ ] ,
60+ useFactory : ( ) => ( {
61+ create : ( ) => ( { observe : observeSpy , disconnect : disconnectSpy } )
62+ } )
63+ } ) ;
64+
65+ const fixture = TestBed . createComponent ( ComponentWithTextContent ) ;
66+ fixture . detectChanges ( ) ;
67+
68+ expect ( observeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
69+ expect ( disconnectSpy ) . not . toHaveBeenCalled ( ) ;
70+
71+ fixture . componentInstance . disabled = true ;
72+ fixture . detectChanges ( ) ;
73+
74+ expect ( observeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
75+ expect ( disconnectSpy ) . toHaveBeenCalledTimes ( 1 ) ;
76+ } ) ;
77+
5078 } ) ;
5179
5280 describe ( 'debounced' , ( ) => {
@@ -93,9 +121,16 @@ describe('Observe content', () => {
93121} ) ;
94122
95123
96- @Component ( { template : `<div (cdkObserveContent)="doSomething()">{{text}}</div>` } )
124+ @Component ( {
125+ template : `
126+ <div
127+ (cdkObserveContent)="doSomething()"
128+ [cdkObserveContentDisabled]="disabled">{{text}}</div>
129+ `
130+ } )
97131class ComponentWithTextContent {
98132 text = '' ;
133+ disabled = false ;
99134 doSomething ( ) { }
100135}
101136
0 commit comments