Skip to content

Commit e4c1ee9

Browse files
committed
Add tests for new 'debounceDelay' option. Run old tests in fakeAsync zone now.
1 parent 478871c commit e4c1ee9

File tree

1 file changed

+117
-20
lines changed

1 file changed

+117
-20
lines changed

test/components/spinner/spinner.component.spec.ts

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88
*/
99

10-
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
10+
import { async, ComponentFixture, discardPeriodicTasks, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
1111
import { SpinnerComponent } from '../../../src/components/spinner/spinner.component';
1212
import { By } from '@angular/platform-browser';
1313
import { Spinkit, SPINKIT_COMPONENTS } from '../../../src/spinkits';
@@ -98,7 +98,7 @@ describe('SpinnerComponent', () => {
9898
expect(element.style['background-color']).toBe('rgb(255, 0, 0)');
9999
});
100100

101-
it('should show and hide the spinner according to the pending http requests', inject(
101+
it('should show and hide the spinner according to the pending http requests', fakeAsync(inject(
102102
[PendingInterceptorService, HttpClient, HttpTestingController],
103103
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
104104

@@ -111,72 +111,169 @@ describe('SpinnerComponent', () => {
111111
const firstRequest = httpMock.expectOne('/fake');
112112
const secondRequest = httpMock.expectOne('/fake2');
113113

114+
tick();
114115
expect(component.isSpinnerVisible).toBeTruthy();
115-
116116
firstRequest.flush({});
117-
expect(component.isSpinnerVisible).toBeTruthy();
118117

118+
tick();
119+
expect(component.isSpinnerVisible).toBeTruthy();
119120
secondRequest.flush({});
121+
122+
tick();
120123
expect(component.isSpinnerVisible).toBeFalsy();
121124
}
122-
));
125+
)));
123126

124-
it('should hide and show a the spinner for a single http request', inject(
127+
it('should hide and show a the spinner for a single http request', fakeAsync(inject(
125128
[PendingInterceptorService, HttpClient, HttpTestingController],
126129
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
127130
http.get('/fake').subscribe();
131+
132+
tick();
128133
expect(component.isSpinnerVisible).toBeTruthy();
129134
httpMock.expectOne('/fake').flush({});
135+
136+
tick();
130137
expect(component.isSpinnerVisible).toBeFalsy();
131138
}
132-
));
139+
)));
133140

134-
it('should not show the spinner if the request is filtered', inject(
141+
it('should not show the spinner if the request is filtered', fakeAsync(inject(
135142
[PendingInterceptorService, HttpClient, HttpTestingController],
136143
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
137144
component.filteredUrlPatterns.push('fake');
138145
fixture.detectChanges();
139146

140147
http.get('/fake').subscribe();
148+
tick();
141149
expect(component.isSpinnerVisible).toBeFalsy();
142150
httpMock.expectOne('/fake').flush({});
143151
}
144-
));
152+
)));
145153

146-
it('should correctly filter with several requests and one pattern', inject(
154+
it('should correctly filter with several requests and one pattern', fakeAsync(inject(
147155
[PendingInterceptorService, HttpClient, HttpTestingController],
148156
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
149157
component.filteredUrlPatterns.push('\\d');
150158
fixture.detectChanges();
151159

152160
http.get('/12345').subscribe();
161+
tick();
153162
expect(component.isSpinnerVisible).toBeFalsy();
154163
httpMock.expectOne('/12345').flush({});
155164

156165
http.get('/fake').subscribe();
166+
tick();
157167
expect(component.isSpinnerVisible).toBeTruthy();
158168
httpMock.expectOne('/fake').flush({});
169+
170+
tick();
159171
expect(component.isSpinnerVisible).toBeFalsy();
160172
}
161-
));
173+
)));
162174

163175
it('should throw an error if filteredUrlPatterns is not an array', () => {
164176
component.filteredUrlPatterns = null;
165-
expect(() => fixture.detectChanges())
166-
.toThrow(new Error('`filteredUrlPatterns` must be an array.'));
177+
expect(() => fixture.detectChanges()).toThrow(new Error('`filteredUrlPatterns` must be an array.'));
167178
});
168179

169-
it('should show the spinner even if the component is created after the http request is performed', inject(
180+
it('should show the spinner even if the component is created after the http request is performed',
181+
fakeAsync(inject([PendingInterceptorService, HttpClient, HttpTestingController],
182+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
183+
http.get('/fake').subscribe();
184+
185+
const newFixture = TestBed.createComponent(SpinnerComponent);
186+
const newComponent = newFixture.componentInstance;
187+
188+
tick();
189+
expect(newComponent.isSpinnerVisible).toBeTruthy();
190+
httpMock.expectOne('/fake').flush({});
191+
192+
tick();
193+
expect(newComponent.isSpinnerVisible).toBeFalsy();
194+
httpMock.verify();
195+
}
196+
))
197+
);
198+
199+
it('should correctly handle the debounce delay for a single http request', fakeAsync(inject(
170200
[PendingInterceptorService, HttpClient, HttpTestingController],
171201
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
202+
component.debounceDelay = 2000;
172203
http.get('/fake').subscribe();
173204

174-
const newFixture = TestBed.createComponent(SpinnerComponent);
175-
const newComponent = newFixture.componentInstance;
176-
expect(newComponent.isSpinnerVisible).toBeTruthy();
205+
// the http request is pending for 1 second now
206+
tick(1000);
207+
expect(component.isSpinnerVisible).toBeFalsy();
208+
209+
// the http request is pending for 1,999 seconds now
210+
tick(999);
211+
expect(component.isSpinnerVisible).toBeFalsy();
212+
213+
// the http request is pending for 2 seconds now - the spinner will be displayed
214+
tick(1);
215+
expect(component.isSpinnerVisible).toBeTruthy();
216+
217+
// the http request is pending for 5 seconds now - the spinner is still displayed
218+
tick(3000);
219+
expect(component.isSpinnerVisible).toBeTruthy();
220+
221+
// the http request is finally over, the spinner is hidden
177222
httpMock.expectOne('/fake').flush({});
178-
expect(newComponent.isSpinnerVisible).toBeFalsy();
179-
httpMock.verify();
223+
tick();
224+
expect(component.isSpinnerVisible).toBeFalsy();
225+
226+
// https://github.com/angular/angular/issues/10127
227+
discardPeriodicTasks();
228+
}
229+
)));
230+
231+
it('should correctly handle the debounce delay for multiple http requests', fakeAsync(inject(
232+
[PendingInterceptorService, HttpClient, HttpTestingController],
233+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
234+
component.debounceDelay = 2000;
235+
236+
function runQuery(url: string): Observable<any> {
237+
return http.get(url);
238+
}
239+
240+
Observable.forkJoin([runQuery('/fake'), runQuery('/fake2')]).subscribe();
241+
242+
const firstRequest = httpMock.expectOne('/fake');
243+
const secondRequest = httpMock.expectOne('/fake2');
244+
245+
// the http requests are pending for 1 second now
246+
tick(1000);
247+
expect(component.isSpinnerVisible).toBeFalsy();
248+
249+
// the http requests are pending for 1,999 seconds now
250+
tick(999);
251+
expect(component.isSpinnerVisible).toBeFalsy();
252+
253+
// the http requests are pending for 2 seconds now - the spinner will be displayed
254+
tick(1);
255+
expect(component.isSpinnerVisible).toBeTruthy();
256+
257+
// the http requests are pending for 5 seconds now - the spinner is still displayed
258+
tick(3000);
259+
expect(component.isSpinnerVisible).toBeTruthy();
260+
261+
// the first http request is finally over, the spinner is still displayed
262+
firstRequest.flush({});
263+
tick();
264+
expect(component.isSpinnerVisible).toBeTruthy();
265+
266+
// the second request is pending for 8 seconds now - the spinner is still displayed
267+
tick(3000);
268+
expect(component.isSpinnerVisible).toBeTruthy();
269+
270+
// the second http request is finally over, the spinner is hidden
271+
secondRequest.flush({});
272+
tick();
273+
expect(component.isSpinnerVisible).toBeFalsy();
274+
275+
// https://github.com/angular/angular/issues/10127
276+
discardPeriodicTasks();
180277
}
181-
));
278+
)));
182279
});

0 commit comments

Comments
 (0)