1
1
import { Component , ElementRef , ViewChild } from '@angular/core' ;
2
2
import {
3
- waitForAsync ,
4
3
ComponentFixture ,
4
+ TestBed ,
5
5
fakeAsync ,
6
6
inject ,
7
- TestBed ,
8
7
tick ,
8
+ waitForAsync ,
9
9
} from '@angular/core/testing' ;
10
10
import { ContentObserver , MutationObserverFactory , ObserversModule } from './observe-content' ;
11
11
@@ -112,9 +112,9 @@ describe('Observe content directive', () => {
112
112
} ) ) ;
113
113
114
114
it ( 'should debounce the content changes' , fakeAsync ( ( ) => {
115
- invokeCallbacks ( ) ;
116
- invokeCallbacks ( ) ;
117
- invokeCallbacks ( ) ;
115
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
116
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
117
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
118
118
119
119
tick ( 500 ) ;
120
120
expect ( fixture . componentInstance . spy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -166,7 +166,7 @@ describe('ContentObserver injectable', () => {
166
166
expect ( spy ) . not . toHaveBeenCalled ( ) ;
167
167
168
168
fixture . componentInstance . text = 'text' ;
169
- invokeCallbacks ( ) ;
169
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
170
170
171
171
expect ( spy ) . toHaveBeenCalled ( ) ;
172
172
} ) ) ;
@@ -186,19 +186,108 @@ describe('ContentObserver injectable', () => {
186
186
expect ( mof . create ) . toHaveBeenCalledTimes ( 1 ) ;
187
187
188
188
fixture . componentInstance . text = 'text' ;
189
- invokeCallbacks ( ) ;
189
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
190
190
191
191
expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
192
192
193
193
spy . calls . reset ( ) ;
194
194
sub1 . unsubscribe ( ) ;
195
195
fixture . componentInstance . text = 'text text' ;
196
- invokeCallbacks ( ) ;
196
+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
197
197
198
198
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
199
199
} ) ,
200
200
) ) ;
201
201
} ) ;
202
+
203
+ describe ( 'real behavior' , ( ) => {
204
+ beforeEach ( waitForAsync ( ( ) => {
205
+ TestBed . configureTestingModule ( {
206
+ imports : [ ObserversModule , UnobservedComponentWithTextContent ] ,
207
+ } ) ;
208
+
209
+ TestBed . compileComponents ( ) ;
210
+ } ) ) ;
211
+
212
+ it ( 'should ignore addition or removal of comments' , waitForAsync (
213
+ inject ( [ ContentObserver ] , async ( contentObserver : ContentObserver ) => {
214
+ const spy = jasmine . createSpy ( 'content observer' ) ;
215
+ const comment = document . createComment ( 'cool' ) ;
216
+
217
+ const fixture = TestBed . createComponent ( UnobservedComponentWithTextContent ) ;
218
+ fixture . autoDetectChanges ( ) ;
219
+ contentObserver . observe ( fixture . componentInstance . contentEl ) . subscribe ( spy ) ;
220
+ await new Promise ( r => setTimeout ( r ) ) ;
221
+ spy . calls . reset ( ) ;
222
+
223
+ fixture . componentInstance . contentEl . nativeElement . appendChild ( comment ) ;
224
+ await new Promise ( r => setTimeout ( r ) ) ;
225
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
226
+
227
+ fixture . componentInstance . contentEl . nativeElement . remove ( comment ) ;
228
+ await new Promise ( r => setTimeout ( r ) ) ;
229
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
230
+ } ) ,
231
+ ) ) ;
232
+
233
+ it ( 'should not ignore addition or removal of text' , waitForAsync (
234
+ inject ( [ ContentObserver ] , async ( contentObserver : ContentObserver ) => {
235
+ const spy = jasmine . createSpy ( 'content observer' ) ;
236
+ const text = document . createTextNode ( 'cool' ) ;
237
+
238
+ const fixture = TestBed . createComponent ( UnobservedComponentWithTextContent ) ;
239
+ fixture . autoDetectChanges ( ) ;
240
+ contentObserver . observe ( fixture . componentInstance . contentEl ) . subscribe ( spy ) ;
241
+ await new Promise ( r => setTimeout ( r ) ) ;
242
+
243
+ spy . calls . reset ( ) ;
244
+ fixture . componentInstance . contentEl . nativeElement . appendChild ( text ) ;
245
+ await new Promise ( r => setTimeout ( r ) ) ;
246
+ expect ( spy ) . toHaveBeenCalled ( ) ;
247
+
248
+ spy . calls . reset ( ) ;
249
+ fixture . componentInstance . contentEl . nativeElement . remove ( text ) ;
250
+ await new Promise ( r => setTimeout ( r ) ) ;
251
+ expect ( spy ) . toHaveBeenCalled ( ) ;
252
+ } ) ,
253
+ ) ) ;
254
+
255
+ it ( 'should ignore comment content change' , waitForAsync (
256
+ inject ( [ ContentObserver ] , async ( contentObserver : ContentObserver ) => {
257
+ const spy = jasmine . createSpy ( 'content observer' ) ;
258
+ const comment = document . createComment ( 'cool' ) ;
259
+
260
+ const fixture = TestBed . createComponent ( UnobservedComponentWithTextContent ) ;
261
+ fixture . autoDetectChanges ( ) ;
262
+ contentObserver . observe ( fixture . componentInstance . contentEl ) . subscribe ( spy ) ;
263
+ fixture . componentInstance . contentEl . nativeElement . appendChild ( comment ) ;
264
+ await new Promise ( r => setTimeout ( r ) ) ;
265
+
266
+ spy . calls . reset ( ) ;
267
+ comment . textContent = 'beans' ;
268
+ await new Promise ( r => setTimeout ( r ) ) ;
269
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
270
+ } ) ,
271
+ ) ) ;
272
+
273
+ it ( 'should not ignore text content change' , waitForAsync (
274
+ inject ( [ ContentObserver ] , async ( contentObserver : ContentObserver ) => {
275
+ const spy = jasmine . createSpy ( 'content observer' ) ;
276
+ const text = document . createTextNode ( 'cool' ) ;
277
+
278
+ const fixture = TestBed . createComponent ( UnobservedComponentWithTextContent ) ;
279
+ fixture . autoDetectChanges ( ) ;
280
+ contentObserver . observe ( fixture . componentInstance . contentEl ) . subscribe ( spy ) ;
281
+ fixture . componentInstance . contentEl . nativeElement . appendChild ( text ) ;
282
+ await new Promise ( r => setTimeout ( r ) ) ;
283
+
284
+ spy . calls . reset ( ) ;
285
+ text . textContent = 'beans' ;
286
+ await new Promise ( r => setTimeout ( r ) ) ;
287
+ expect ( spy ) . toHaveBeenCalled ( ) ;
288
+ } ) ,
289
+ ) ) ;
290
+ } ) ;
202
291
} ) ;
203
292
204
293
@Component ( {
0 commit comments