Skip to content

Commit 9127bcb

Browse files
committed
Move spinner tests with HttpInterceptorService in another file
1 parent ff91f9c commit 9127bcb

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
4+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
5+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
6+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
7+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+
*/
9+
10+
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
11+
import { SpinnerComponent } from './spinner.component';
12+
import { HttpModule, RequestOptions, Response, ResponseOptions } from '@angular/http';
13+
import { By } from '@angular/platform-browser';
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';
18+
import { PendingInterceptorService } from '../pending-interceptor.service';
19+
20+
describe('SpinnerComponent', () => {
21+
let component: SpinnerComponent;
22+
let fixture: ComponentFixture<SpinnerComponent>;
23+
24+
function HttpInterceptorMockBackendServiceFactory(backend: MockBackend, defaultOptions: RequestOptions) {
25+
return new HttpInterceptorService(backend, defaultOptions);
26+
}
27+
28+
const HttpInterceptorServiceFactoryProvider = {
29+
provide: HttpInterceptorService,
30+
useFactory: HttpInterceptorMockBackendServiceFactory,
31+
deps: [MockBackend, RequestOptions]
32+
};
33+
34+
beforeEach(async(() => {
35+
TestBed.configureTestingModule({
36+
declarations: [SpinnerComponent],
37+
providers: [MockBackend, HttpInterceptorServiceFactoryProvider, PendingInterceptorService],
38+
imports: [HttpModule]
39+
})
40+
.compileComponents();
41+
}));
42+
43+
beforeEach(() => {
44+
fixture = TestBed.createComponent(SpinnerComponent);
45+
component = fixture.componentInstance;
46+
});
47+
48+
it('should create the spinner component', () => {
49+
expect(component).toBeTruthy();
50+
});
51+
52+
it('should create the spinner component with default values', () => {
53+
component.isSpinnerVisible = true;
54+
fixture.detectChanges();
55+
56+
const element = fixture
57+
.debugElement
58+
.query(By.css('.sk-cube-grid'))
59+
.nativeElement;
60+
61+
expect(element.className).toBe('sk-cube-grid colored');
62+
});
63+
64+
it('should not set the colored class if background-color is defined', () => {
65+
component.isSpinnerVisible = true;
66+
component.backgroundColor = '#ff0000';
67+
fixture.detectChanges();
68+
69+
const element = fixture
70+
.debugElement
71+
.query(By.css('.sk-cube-grid'))
72+
.nativeElement;
73+
74+
expect(element.className).toBe('sk-cube-grid');
75+
});
76+
77+
it('should not display anything by default', () => {
78+
const element = fixture
79+
.debugElement
80+
.query(By.css('#http-loader'));
81+
82+
expect(element).toBeNull();
83+
});
84+
85+
it('should be able to specify another known spinner', () => {
86+
component.isSpinnerVisible = true;
87+
component.spinner = Spinkit.skRotatingPlane;
88+
fixture.detectChanges();
89+
90+
const element = fixture
91+
.debugElement
92+
.query(By.css('.sk-rotating-plane'))
93+
.nativeElement;
94+
95+
expect(element.className).toBe('sk-rotating-plane colored-parent');
96+
});
97+
98+
it('should allow us to specify a custom background-color', () => {
99+
component.isSpinnerVisible = true;
100+
component.backgroundColor = '#ff0000';
101+
fixture.detectChanges();
102+
103+
const element = fixture
104+
.debugElement
105+
.query(By.css('.sk-cube.sk-cube1'))
106+
.nativeElement;
107+
108+
expect(element.style['background-color']).toBe('rgb(255, 0, 0)');
109+
});
110+
111+
it('should show and hide the spinner according to the pending http requests',
112+
inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, backend: MockBackend) => {
113+
114+
const connections: MockConnection[] = [],
115+
responseMock = {key: 'value'},
116+
mockResponse: Response = new Response(new ResponseOptions({body: responseMock, status: 200}));
117+
118+
function runQuery(url: string): Observable<Response> {
119+
return service.get(url);
120+
}
121+
122+
backend.connections.subscribe((c: MockConnection) => connections.push(c));
123+
Observable.forkJoin([runQuery('http://www.fake.url'), runQuery('http://www2.fake.url')]).subscribe();
124+
125+
expect(component.isSpinnerVisible).toBeTruthy();
126+
127+
connections[0].mockRespond(mockResponse);
128+
expect(component.isSpinnerVisible).toBeTruthy();
129+
130+
connections[1].mockRespond(mockResponse);
131+
expect(component.isSpinnerVisible).toBeFalsy();
132+
})
133+
);
134+
135+
it('should hide and show a the spinner for a single http request',
136+
inject([HttpInterceptorService, MockBackend], (service: HttpInterceptorService, backend: MockBackend) => {
137+
let connection: MockConnection;
138+
const responseMock = {key: 'value'},
139+
mockResponse: Response = new Response(new ResponseOptions({body: responseMock, status: 200}));
140+
141+
backend.connections.subscribe((c: MockConnection) => connection = c);
142+
service.get('http://www.fake.url').subscribe();
143+
expect(component.isSpinnerVisible).toBeTruthy();
144+
connection.mockRespond(mockResponse);
145+
expect(component.isSpinnerVisible).toBeFalsy();
146+
})
147+
);
148+
});

0 commit comments

Comments
 (0)