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,90 @@ 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
+ let spy : jasmine . Spy ;
205
+ let contentEl : HTMLElement ;
206
+ let contentObserver : ContentObserver ;
207
+
208
+ beforeEach ( waitForAsync ( ( ) => {
209
+ TestBed . configureTestingModule ( {
210
+ imports : [ ObserversModule , UnobservedComponentWithTextContent ] ,
211
+ } ) ;
212
+
213
+ TestBed . compileComponents ( ) ;
214
+ const fixture = TestBed . createComponent ( UnobservedComponentWithTextContent ) ;
215
+ fixture . autoDetectChanges ( ) ;
216
+ spy = jasmine . createSpy ( 'content observer' ) ;
217
+ contentObserver = TestBed . inject ( ContentObserver ) ;
218
+ contentEl = fixture . componentInstance . contentEl . nativeElement ;
219
+ contentObserver . observe ( contentEl ) . subscribe ( spy ) ;
220
+ } ) ) ;
221
+
222
+ it ( 'should ignore addition or removal of comments' , waitForAsync ( async ( ) => {
223
+ const comment = document . createComment ( 'cool' ) ;
224
+ await new Promise ( r => setTimeout ( r ) ) ;
225
+
226
+ spy . calls . reset ( ) ;
227
+ contentEl . appendChild ( comment ) ;
228
+ await new Promise ( r => setTimeout ( r ) ) ;
229
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
230
+
231
+ comment . remove ( ) ;
232
+ await new Promise ( r => setTimeout ( r ) ) ;
233
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
234
+ } ) ) ;
235
+
236
+ it ( 'should not ignore addition or removal of text' , waitForAsync ( async ( ) => {
237
+ const text = document . createTextNode ( 'cool' ) ;
238
+ await new Promise ( r => setTimeout ( r ) ) ;
239
+
240
+ spy . calls . reset ( ) ;
241
+ contentEl . appendChild ( text ) ;
242
+ await new Promise ( r => setTimeout ( r ) ) ;
243
+ expect ( spy ) . toHaveBeenCalled ( ) ;
244
+
245
+ spy . calls . reset ( ) ;
246
+ text . remove ( ) ;
247
+ await new Promise ( r => setTimeout ( r ) ) ;
248
+ expect ( spy ) . toHaveBeenCalled ( ) ;
249
+ } ) ) ;
250
+
251
+ it ( 'should ignore comment content change' , waitForAsync ( async ( ) => {
252
+ const comment = document . createComment ( 'cool' ) ;
253
+ contentEl . appendChild ( comment ) ;
254
+ await new Promise ( r => setTimeout ( r ) ) ;
255
+
256
+ spy . calls . reset ( ) ;
257
+ comment . textContent = 'beans' ;
258
+ await new Promise ( r => setTimeout ( r ) ) ;
259
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
260
+ } ) ) ;
261
+
262
+ it ( 'should not ignore text content change' , waitForAsync ( async ( ) => {
263
+ const text = document . createTextNode ( 'cool' ) ;
264
+ contentEl . appendChild ( text ) ;
265
+ await new Promise ( r => setTimeout ( r ) ) ;
266
+
267
+ spy . calls . reset ( ) ;
268
+ text . textContent = 'beans' ;
269
+ await new Promise ( r => setTimeout ( r ) ) ;
270
+ expect ( spy ) . toHaveBeenCalled ( ) ;
271
+ } ) ) ;
272
+ } ) ;
202
273
} ) ;
203
274
204
275
@Component ( {
0 commit comments