1
1
import { Component } from '@angular/core' ;
2
- import { async , TestBed } from '@angular/core/testing' ;
3
- import { ObserveContentModule } from './observe-content' ;
2
+ import { async , TestBed , ComponentFixture , fakeAsync , tick } from '@angular/core/testing' ;
3
+ import { ObserveContentModule , MutationObserverFactory } from './observe-content' ;
4
4
5
5
/**
6
6
* TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
7
7
* `MutationObserver` and needs to be investigated
8
8
*/
9
9
10
10
describe ( 'Observe content' , ( ) => {
11
- beforeEach ( async ( ( ) => {
12
- TestBed . configureTestingModule ( {
13
- imports : [ ObserveContentModule ] ,
14
- declarations : [ ComponentWithTextContent , ComponentWithChildTextContent ] ,
15
- } ) ;
11
+ describe ( 'basic usage' , ( ) => {
12
+ beforeEach ( async ( ( ) => {
13
+ TestBed . configureTestingModule ( {
14
+ imports : [ ObserveContentModule ] ,
15
+ declarations : [ ComponentWithTextContent , ComponentWithChildTextContent ]
16
+ } ) ;
16
17
17
- TestBed . compileComponents ( ) ;
18
- } ) ) ;
18
+ TestBed . compileComponents ( ) ;
19
+ } ) ) ;
19
20
20
- describe ( 'text content change' , ( ) => {
21
- it ( 'should call the registered for changes function' , done => {
21
+ it ( 'should trigger the callback when the content of the element changes' , done => {
22
22
let fixture = TestBed . createComponent ( ComponentWithTextContent ) ;
23
23
fixture . detectChanges ( ) ;
24
24
@@ -33,10 +33,8 @@ describe('Observe content', () => {
33
33
fixture . componentInstance . text = 'text' ;
34
34
fixture . detectChanges ( ) ;
35
35
} ) ;
36
- } ) ;
37
36
38
- describe ( 'child text content change' , ( ) => {
39
- it ( 'should call the registered for changes function' , done => {
37
+ it ( 'should trigger the callback when the content of the children changes' , done => {
40
38
let fixture = TestBed . createComponent ( ComponentWithChildTextContent ) ;
41
39
fixture . detectChanges ( ) ;
42
40
@@ -52,6 +50,62 @@ describe('Observe content', () => {
52
50
fixture . detectChanges ( ) ;
53
51
} ) ;
54
52
} ) ;
53
+
54
+ describe ( 'debounced' , ( ) => {
55
+ let fixture : ComponentFixture < ComponentWithDebouncedListener > ;
56
+ let instance : ComponentWithDebouncedListener ;
57
+ let callbacks : Function [ ] ;
58
+ let fakeMutationObserver : {
59
+ create ( callback : MutationCallback ) : any ;
60
+ invokeCallbacks : ( args : any [ ] ) => void ;
61
+ } ;
62
+
63
+ beforeEach ( async ( ( ) => {
64
+ callbacks = [ ] ;
65
+
66
+ fakeMutationObserver = {
67
+ invokeCallbacks : ( args ?: any [ ] ) => callbacks . forEach ( callback => callback ( args ) ) ,
68
+ create : ( callback : MutationCallback ) => {
69
+ callbacks . push ( callback ) ;
70
+
71
+ return {
72
+ observe : ( ) => { } ,
73
+ disconnect : ( ) => { }
74
+ } ;
75
+ }
76
+ } ;
77
+
78
+ TestBed . configureTestingModule ( {
79
+ imports : [ ObserveContentModule ] ,
80
+ declarations : [ ComponentWithDebouncedListener ] ,
81
+ providers : [ { provide : MutationObserverFactory , useValue : fakeMutationObserver } ]
82
+ } ) ;
83
+
84
+ TestBed . compileComponents ( ) ;
85
+
86
+ fixture = TestBed . createComponent ( ComponentWithDebouncedListener ) ;
87
+ instance = fixture . componentInstance ;
88
+ fixture . detectChanges ( ) ;
89
+ } ) ) ;
90
+
91
+ it ( 'should debounce the content changes' , fakeAsync ( ( ) => {
92
+ fakeMutationObserver . invokeCallbacks ( [ 1 ] ) ;
93
+ fakeMutationObserver . invokeCallbacks ( [ 2 ] ) ;
94
+ fakeMutationObserver . invokeCallbacks ( [ 3 ] ) ;
95
+
96
+ tick ( 500 ) ;
97
+ expect ( instance . spy ) . toHaveBeenCalledTimes ( 1 ) ;
98
+ } ) ) ;
99
+
100
+ it ( 'should should keep track of all of the mutation records' , fakeAsync ( ( ) => {
101
+ fakeMutationObserver . invokeCallbacks ( [ 1 ] ) ;
102
+ fakeMutationObserver . invokeCallbacks ( [ 2 ] ) ;
103
+ fakeMutationObserver . invokeCallbacks ( [ 3 ] ) ;
104
+
105
+ tick ( 500 ) ;
106
+ expect ( instance . spy ) . toHaveBeenCalledWith ( [ 1 , 2 , 3 ] ) ;
107
+ } ) ) ;
108
+ } ) ;
55
109
} ) ;
56
110
57
111
@@ -66,3 +120,11 @@ class ComponentWithChildTextContent {
66
120
text = '' ;
67
121
doSomething ( ) { }
68
122
}
123
+
124
+ @Component ( {
125
+ template : `<div (cdkObserveContent)="spy($event)" [debounce]="debounce">{{text}}</div>`
126
+ } )
127
+ class ComponentWithDebouncedListener {
128
+ debounce = 500 ;
129
+ spy = jasmine . createSpy ( 'MutationObserver callback' ) ;
130
+ }
0 commit comments