|
7 | 7 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
8 | 8 | */ |
9 | 9 |
|
10 | | -import { async, ComponentFixture, ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing'; |
| 10 | +import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; |
11 | 11 | 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'; |
14 | 13 | import { By } from '@angular/platform-browser'; |
15 | 14 | 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'; |
16 | 18 |
|
17 | 19 | describe('SpinnerComponent', () => { |
18 | 20 | let component: SpinnerComponent; |
19 | 21 | let fixture: ComponentFixture<SpinnerComponent>; |
20 | 22 |
|
| 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 | + |
21 | 33 | beforeEach(async(() => { |
22 | 34 | TestBed.configureTestingModule({ |
23 | 35 | declarations: [SpinnerComponent], |
24 | | - providers: [HttpInterceptorServiceFactoryProvider], |
| 36 | + providers: [MockBackend, HttpInterceptorServiceFactoryProvider], |
25 | 37 | imports: [HttpModule] |
26 | 38 | }) |
27 | 39 | .compileComponents(); |
@@ -81,4 +93,28 @@ describe('SpinnerComponent', () => { |
81 | 93 |
|
82 | 94 | expect(element.className).toBe('sk-rotating-plane colored-parent'); |
83 | 95 | }); |
| 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 | + ); |
84 | 120 | }); |
0 commit comments