Skip to content

Commit 6bde7e0

Browse files
authored
Merge pull request #24 from amitport/patch-1
use ReplaySubject for pendingRequestsStatus
2 parents a4820e9 + f26d9d6 commit 6bde7e0

File tree

4 files changed

+94
-68
lines changed

4 files changed

+94
-68
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- 6.11.4
3+
- 6.12.0
44
- lts/*
55
- node

src/services/pending-interceptor.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { Injectable } from '@angular/core';
1111
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
1212
import { Observable } from 'rxjs/Observable';
13-
import { Subject } from 'rxjs/Subject';
13+
import { ReplaySubject } from 'rxjs/ReplaySubject';
1414
import 'rxjs/add/operator/map';
1515
import 'rxjs/add/operator/catch';
1616
import 'rxjs/add/operator/finally';
@@ -19,7 +19,7 @@ import 'rxjs/add/observable/throw';
1919
@Injectable()
2020
export class PendingInterceptorService implements HttpInterceptor {
2121
private _pendingRequests = 0;
22-
private _pendingRequestsStatus: Subject<boolean> = new Subject<boolean>();
22+
private _pendingRequestsStatus: ReplaySubject<boolean> = new ReplaySubject<boolean>(1);
2323
private _filteredUrlPatterns: RegExp[] = [];
2424

2525
get pendingRequestsStatus(): Observable<boolean> {

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

Lines changed: 75 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -98,75 +98,85 @@ 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',
102-
inject(
103-
[PendingInterceptorService, HttpClient, HttpTestingController],
104-
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
105-
106-
function runQuery(url: string): Observable<any> {
107-
return http.get(url);
108-
}
109-
110-
Observable.forkJoin([runQuery('/fake'), runQuery('/fake2')]).subscribe();
111-
112-
const firstRequest = httpMock.expectOne('/fake');
113-
const secondRequest = httpMock.expectOne('/fake2');
114-
115-
expect(component.isSpinnerVisible).toBeTruthy();
116-
117-
firstRequest.flush({});
118-
expect(component.isSpinnerVisible).toBeTruthy();
119-
120-
secondRequest.flush({});
121-
expect(component.isSpinnerVisible).toBeFalsy();
122-
})
123-
);
124-
125-
it('should hide and show a the spinner for a single http request',
126-
inject(
127-
[PendingInterceptorService, HttpClient, HttpTestingController],
128-
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
129-
http.get('/fake').subscribe();
130-
expect(component.isSpinnerVisible).toBeTruthy();
131-
httpMock.expectOne('/fake').flush({});
132-
expect(component.isSpinnerVisible).toBeFalsy();
133-
})
134-
);
135-
136-
it('should not show the spinner if the request is filtered',
137-
inject(
138-
[PendingInterceptorService, HttpClient, HttpTestingController],
139-
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
140-
component.filteredUrlPatterns.push('fake');
141-
fixture.detectChanges();
142-
143-
http.get('/fake').subscribe();
144-
expect(component.isSpinnerVisible).toBeFalsy();
145-
httpMock.expectOne('/fake').flush({});
146-
})
147-
);
148-
149-
it('should correctly filter with several requests and one pattern',
150-
inject(
151-
[PendingInterceptorService, HttpClient, HttpTestingController],
152-
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
153-
component.filteredUrlPatterns.push('\\d');
154-
fixture.detectChanges();
155-
156-
http.get('/12345').subscribe();
157-
expect(component.isSpinnerVisible).toBeFalsy();
158-
httpMock.expectOne('/12345').flush({});
159-
160-
http.get('/fake').subscribe();
161-
expect(component.isSpinnerVisible).toBeTruthy();
162-
httpMock.expectOne('/fake').flush({});
163-
expect(component.isSpinnerVisible).toBeFalsy();
164-
})
165-
);
101+
it('should show and hide the spinner according to the pending http requests', inject(
102+
[PendingInterceptorService, HttpClient, HttpTestingController],
103+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
104+
105+
function runQuery(url: string): Observable<any> {
106+
return http.get(url);
107+
}
108+
109+
Observable.forkJoin([runQuery('/fake'), runQuery('/fake2')]).subscribe();
110+
111+
const firstRequest = httpMock.expectOne('/fake');
112+
const secondRequest = httpMock.expectOne('/fake2');
113+
114+
expect(component.isSpinnerVisible).toBeTruthy();
115+
116+
firstRequest.flush({});
117+
expect(component.isSpinnerVisible).toBeTruthy();
118+
119+
secondRequest.flush({});
120+
expect(component.isSpinnerVisible).toBeFalsy();
121+
}
122+
));
123+
124+
it('should hide and show a the spinner for a single http request', inject(
125+
[PendingInterceptorService, HttpClient, HttpTestingController],
126+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
127+
http.get('/fake').subscribe();
128+
expect(component.isSpinnerVisible).toBeTruthy();
129+
httpMock.expectOne('/fake').flush({});
130+
expect(component.isSpinnerVisible).toBeFalsy();
131+
}
132+
));
133+
134+
it('should not show the spinner if the request is filtered', inject(
135+
[PendingInterceptorService, HttpClient, HttpTestingController],
136+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
137+
component.filteredUrlPatterns.push('fake');
138+
fixture.detectChanges();
139+
140+
http.get('/fake').subscribe();
141+
expect(component.isSpinnerVisible).toBeFalsy();
142+
httpMock.expectOne('/fake').flush({});
143+
}
144+
));
145+
146+
it('should correctly filter with several requests and one pattern', inject(
147+
[PendingInterceptorService, HttpClient, HttpTestingController],
148+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
149+
component.filteredUrlPatterns.push('\\d');
150+
fixture.detectChanges();
151+
152+
http.get('/12345').subscribe();
153+
expect(component.isSpinnerVisible).toBeFalsy();
154+
httpMock.expectOne('/12345').flush({});
155+
156+
http.get('/fake').subscribe();
157+
expect(component.isSpinnerVisible).toBeTruthy();
158+
httpMock.expectOne('/fake').flush({});
159+
expect(component.isSpinnerVisible).toBeFalsy();
160+
}
161+
));
166162

167163
it('should throw an error if filteredUrlPatterns is not an array', () => {
168164
component.filteredUrlPatterns = null;
169165
expect(() => fixture.detectChanges())
170166
.toThrow(new Error('`filteredUrlPatterns` must be an array.'));
171167
});
168+
169+
it('should show the spinner even if the component is created after the http request is performed', inject(
170+
[PendingInterceptorService, HttpClient, HttpTestingController],
171+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
172+
http.get('/fake').subscribe();
173+
174+
const newFixture = TestBed.createComponent(SpinnerComponent);
175+
const newComponent = newFixture.componentInstance;
176+
expect(newComponent.isSpinnerVisible).toBeTruthy();
177+
httpMock.expectOne('/fake').flush({});
178+
expect(newComponent.isSpinnerVisible).toBeFalsy();
179+
httpMock.verify();
180+
}
181+
));
172182
});

test/services/pending-interceptor.service.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ describe('PendingInterceptorService', () => {
7676
})
7777
));
7878

79+
it('should correctly notify the pendingRequestsStatus observable, even if subscribed after', async(
80+
inject(
81+
[PendingInterceptorService, HttpClient, HttpTestingController],
82+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
83+
http.get('/fake').subscribe();
84+
httpMock.expectOne('/fake');
85+
86+
const pendingRequestsStatus = service.pendingRequestsStatus;
87+
pendingRequestsStatus
88+
.subscribe(
89+
(next: boolean) => expect(next).toBeTruthy(),
90+
(error: HttpErrorResponse) => expect(1).toBe(2)
91+
);
92+
})
93+
));
94+
7995
it('should fail correctly',
8096
inject(
8197
[PendingInterceptorService, HttpClient, HttpTestingController],

0 commit comments

Comments
 (0)