Skip to content

Commit f87a855

Browse files
committed
Add test with ChangeDetectionStrategy.OnPush on an host component
1 parent 5fe2a9d commit f87a855

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 { HttpClient } from '@angular/common/http';
11+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
12+
import { ChangeDetectionStrategy, Component } from '@angular/core';
13+
import { async, ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
14+
import { By } from '@angular/platform-browser';
15+
import { NgHttpLoaderModule } from '../../lib/ng-http-loader.module';
16+
17+
@Component({
18+
template: '<ng-http-loader></ng-http-loader>',
19+
changeDetection: ChangeDetectionStrategy.OnPush
20+
})
21+
export class HostComponent {
22+
}
23+
24+
describe('NgHttpLoaderComponent OnPush', () => {
25+
let component: HostComponent;
26+
let fixture: ComponentFixture<HostComponent>;
27+
28+
beforeEach(async(() => {
29+
TestBed.configureTestingModule({
30+
declarations: [HostComponent],
31+
imports: [HttpClientTestingModule, NgHttpLoaderModule.forRoot()]
32+
})
33+
.compileComponents();
34+
}));
35+
36+
beforeEach(() => {
37+
fixture = TestBed.createComponent(HostComponent);
38+
component = fixture.componentInstance;
39+
});
40+
41+
it('should work as expected when the host component has ChangeDetectionStrategy.OnPush', fakeAsync(inject(
42+
[HttpClient, HttpTestingController], (http: HttpClient, httpMock: HttpTestingController) => {
43+
http.get('/fake').subscribe();
44+
tick();
45+
fixture.detectChanges();
46+
let spinner = fixture
47+
.debugElement
48+
.query(By.css('#spinner'))
49+
.nativeElement;
50+
expect(spinner).toBeTruthy();
51+
52+
httpMock.expectOne('/fake').flush({});
53+
tick();
54+
fixture.detectChanges();
55+
56+
spinner = fixture
57+
.debugElement
58+
.query(By.css('#spinner'));
59+
expect(spinner).toBeNull();
60+
}
61+
)));
62+
});

0 commit comments

Comments
 (0)