Skip to content

Commit 239533a

Browse files
committed
Add tests for spinner visibility and pending requests
1 parent 433f6da commit 239533a

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

src/app/http-interceptor.service.spec.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,57 @@
88
*/
99

1010
import { inject, TestBed } from '@angular/core/testing';
11-
import { HttpInterceptorService, HttpInterceptorServiceFactoryProvider } from './http-interceptor.service';
12-
import { HttpModule } from '@angular/http';
11+
import { HttpInterceptorService } from './http-interceptor.service';
12+
import { HttpModule, RequestOptions, Response, ResponseOptions } from '@angular/http';
13+
import { MockBackend, MockConnection } from '@angular/http/testing';
14+
import { Observable } from 'rxjs/Observable';
1315

1416
describe('HttpInterceptorService', () => {
17+
18+
function HttpInterceptorMockBackendServiceFactory(backend: MockBackend, defaultOptions: RequestOptions) {
19+
return new HttpInterceptorService(backend, defaultOptions);
20+
}
21+
22+
const HttpInterceptorServiceFactoryProvider = {
23+
provide: HttpInterceptorService,
24+
useFactory: HttpInterceptorMockBackendServiceFactory,
25+
deps: [MockBackend, RequestOptions]
26+
};
27+
1528
beforeEach(() => {
1629
TestBed.configureTestingModule({
1730
imports: [HttpModule],
18-
providers: [HttpInterceptorService, HttpInterceptorServiceFactoryProvider]
31+
providers: [MockBackend, HttpInterceptorService, HttpInterceptorServiceFactoryProvider]
1932
});
2033
});
2134

2235
it('should create the HttpInterceptorService',
2336
inject([HttpInterceptorService], (service: HttpInterceptorService) => {
2437
expect(service).toBeTruthy();
25-
}));
38+
})
39+
);
40+
41+
it('should be aware of the pending http requests',
42+
inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, backend: MockBackend) => {
43+
44+
const connections: MockConnection[] = [],
45+
responseMock = {key: 'value'},
46+
mockResponse: Response = new Response(new ResponseOptions({body: responseMock, status: 200}));
47+
48+
function runQuery(url: string): Observable<Response> {
49+
return service.get(url);
50+
}
51+
52+
backend.connections.subscribe((c: MockConnection) => connections.push(c));
53+
Observable.forkJoin([runQuery('http://www.fake.url'), runQuery('http://www2.fake.url')]).subscribe();
54+
55+
expect(service.pendingRequests).toBe(2);
56+
57+
connections[0].mockRespond(mockResponse);
58+
expect(service.pendingRequests).toBe(1);
59+
60+
connections[1].mockRespond(mockResponse);
61+
expect(service.pendingRequests).toBe(0);
62+
})
63+
);
2664
});

src/app/spinner/spinner.component.spec.ts

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

10-
import { async, ComponentFixture, ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing';
10+
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
1111
import { SpinnerComponent } from './spinner.component';
12-
import { HttpInterceptorServiceFactoryProvider } from '../http-interceptor.service';
13-
import { HttpModule } from '@angular/http';
12+
import { HttpModule, RequestOptions, Response, ResponseOptions } from '@angular/http';
1413
import { By } from '@angular/platform-browser';
1514
import { Spinkit } from '../spinkits';
15+
import { MockBackend, MockConnection } from '@angular/http/testing';
16+
import { HttpInterceptorService } from '../http-interceptor.service';
17+
import { Observable } from 'rxjs/Observable';
1618

1719
describe('SpinnerComponent', () => {
1820
let component: SpinnerComponent;
1921
let fixture: ComponentFixture<SpinnerComponent>;
2022

23+
function HttpInterceptorMockBackendServiceFactory(backend: MockBackend, defaultOptions: RequestOptions) {
24+
return new HttpInterceptorService(backend, defaultOptions);
25+
}
26+
27+
const HttpInterceptorServiceFactoryProvider = {
28+
provide: HttpInterceptorService,
29+
useFactory: HttpInterceptorMockBackendServiceFactory,
30+
deps: [MockBackend, RequestOptions]
31+
};
32+
2133
beforeEach(async(() => {
2234
TestBed.configureTestingModule({
2335
declarations: [SpinnerComponent],
24-
providers: [HttpInterceptorServiceFactoryProvider],
36+
providers: [MockBackend, HttpInterceptorServiceFactoryProvider],
2537
imports: [HttpModule]
2638
})
2739
.compileComponents();
@@ -81,4 +93,28 @@ describe('SpinnerComponent', () => {
8193

8294
expect(element.className).toBe('sk-rotating-plane colored-parent');
8395
});
96+
97+
it('should show and hide the spinner according to the pending http requests',
98+
inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, backend: MockBackend) => {
99+
100+
const connections: MockConnection[] = [],
101+
responseMock = {key: 'value'},
102+
mockResponse: Response = new Response(new ResponseOptions({body: responseMock, status: 200}));
103+
104+
function runQuery(url: string): Observable<Response> {
105+
return service.get(url);
106+
}
107+
108+
backend.connections.subscribe((c: MockConnection) => connections.push(c));
109+
Observable.forkJoin([runQuery('http://www.fake.url'), runQuery('http://www2.fake.url')]).subscribe();
110+
111+
expect(component.isSpinnerVisible).toBeTruthy();
112+
113+
connections[0].mockRespond(mockResponse);
114+
expect(component.isSpinnerVisible).toBeTruthy();
115+
116+
connections[1].mockRespond(mockResponse);
117+
expect(component.isSpinnerVisible).toBeFalsy();
118+
})
119+
);
84120
});

0 commit comments

Comments
 (0)