11import { Component , ElementRef , ViewChild } from '@angular/core' ;
22import {
3- waitForAsync ,
43 ComponentFixture ,
4+ TestBed ,
55 fakeAsync ,
66 inject ,
7- TestBed ,
87 tick ,
8+ waitForAsync ,
99} from '@angular/core/testing' ;
1010import { ContentObserver , MutationObserverFactory , ObserversModule } from './observe-content' ;
1111
@@ -112,9 +112,9 @@ describe('Observe content directive', () => {
112112 } ) ) ;
113113
114114 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' } ] ) ;
118118
119119 tick ( 500 ) ;
120120 expect ( fixture . componentInstance . spy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -166,7 +166,7 @@ describe('ContentObserver injectable', () => {
166166 expect ( spy ) . not . toHaveBeenCalled ( ) ;
167167
168168 fixture . componentInstance . text = 'text' ;
169- invokeCallbacks ( ) ;
169+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
170170
171171 expect ( spy ) . toHaveBeenCalled ( ) ;
172172 } ) ) ;
@@ -186,19 +186,90 @@ describe('ContentObserver injectable', () => {
186186 expect ( mof . create ) . toHaveBeenCalledTimes ( 1 ) ;
187187
188188 fixture . componentInstance . text = 'text' ;
189- invokeCallbacks ( ) ;
189+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
190190
191191 expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
192192
193193 spy . calls . reset ( ) ;
194194 sub1 . unsubscribe ( ) ;
195195 fixture . componentInstance . text = 'text text' ;
196- invokeCallbacks ( ) ;
196+ invokeCallbacks ( [ { type : 'fake' } ] ) ;
197197
198198 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
199199 } ) ,
200200 ) ) ;
201201 } ) ;
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+ } ) ;
202273} ) ;
203274
204275@Component ( {
0 commit comments