@@ -22,11 +22,11 @@ describe('Observe content', () => {
22
22
23
23
// If the hint label is empty, expect no label.
24
24
const spy = spyOn ( fixture . componentInstance , 'doSomething' ) . and . callFake ( ( ) => {
25
- expect ( spy . calls . any ( ) ) . toBe ( true ) ;
25
+ expect ( spy ) . toHaveBeenCalled ( ) ;
26
26
done ( ) ;
27
27
} ) ;
28
28
29
- expect ( spy . calls . any ( ) ) . toBe ( false ) ;
29
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
30
30
31
31
fixture . componentInstance . text = 'text' ;
32
32
fixture . detectChanges ( ) ;
@@ -38,15 +38,43 @@ describe('Observe content', () => {
38
38
39
39
// If the hint label is empty, expect no label.
40
40
const spy = spyOn ( fixture . componentInstance , 'doSomething' ) . and . callFake ( ( ) => {
41
- expect ( spy . calls . any ( ) ) . toBe ( true ) ;
41
+ expect ( spy ) . toHaveBeenCalled ( ) ;
42
42
done ( ) ;
43
43
} ) ;
44
44
45
- expect ( spy . calls . any ( ) ) . toBe ( false ) ;
45
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
46
46
47
47
fixture . componentInstance . text = 'text' ;
48
48
fixture . detectChanges ( ) ;
49
49
} ) ;
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
+
50
78
} ) ;
51
79
52
80
describe ( 'debounced' , ( ) => {
@@ -93,9 +121,16 @@ describe('Observe content', () => {
93
121
} ) ;
94
122
95
123
96
- @Component ( { template : `<div (cdkObserveContent)="doSomething()">{{text}}</div>` } )
124
+ @Component ( {
125
+ template : `
126
+ <div
127
+ (cdkObserveContent)="doSomething()"
128
+ [cdkObserveContentDisabled]="disabled">{{text}}</div>
129
+ `
130
+ } )
97
131
class ComponentWithTextContent {
98
132
text = '' ;
133
+ disabled = false ;
99
134
doSomething ( ) { }
100
135
}
101
136
0 commit comments