Skip to content

Commit 31190cb

Browse files
authored
Merge pull request #97 from Akkenar/bugfix/Fix_OnDestroy_When_Used_In_Tests
Fix an error when destroying the component
2 parents ba8fcee + 172d604 commit 31190cb

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/lib/components/ng-http-loader.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
4545
public entryComponent: any = null;
4646

4747
constructor(private pendingInterceptorService: PendingInterceptorService, private spinnerVisibilityService: SpinnerVisibilityService) {
48+
this.initializeSubscription();
4849
}
4950

5051
ngOnInit(): void {
5152
this.nullifySpinnerIfEntryComponentIsDefined();
5253
this.initFilters();
53-
this.initializeSubscription();
5454
}
5555

5656
ngOnDestroy(): void {

src/test/components/ng-http-loader.component.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ import { NgHttpLoaderComponent } from '../../lib/components/ng-http-loader.compo
1616
import { PendingInterceptorServiceInterceptor } from '../../lib/services/pending-interceptor.service';
1717
import { SpinnerVisibilityService } from '../../lib/services/spinner-visibility.service';
1818
import { Spinkit, SPINKIT_COMPONENTS } from '../../lib/spinkits';
19+
import { Component } from '@angular/core';
20+
21+
@Component({
22+
template: '<ng-http-loader id="http-loader"></ng-http-loader>'
23+
})
24+
export class ComponentWithLoaderComponent {
25+
}
1926

2027
describe('NgHttpLoaderComponent', () => {
2128
let component: NgHttpLoaderComponent;
2229
let fixture: ComponentFixture<NgHttpLoaderComponent>;
2330

2431
beforeEach(async(() => {
2532
TestBed.configureTestingModule({
26-
declarations: [NgHttpLoaderComponent, ...SPINKIT_COMPONENTS],
33+
declarations: [NgHttpLoaderComponent, ComponentWithLoaderComponent, ...SPINKIT_COMPONENTS],
2734
imports: [HttpClientTestingModule],
2835
providers: [PendingInterceptorServiceInterceptor]
2936
})
@@ -33,7 +40,6 @@ describe('NgHttpLoaderComponent', () => {
3340
beforeEach(() => {
3441
fixture = TestBed.createComponent(NgHttpLoaderComponent);
3542
component = fixture.componentInstance;
36-
component.ngOnInit();
3743
});
3844

3945
it('should create the ng-http-loader component', () => {
@@ -52,6 +58,19 @@ describe('NgHttpLoaderComponent', () => {
5258
expect(element.className).toBe('sk-cube-grid colored');
5359
});
5460

61+
it('should destroy ng-http-loader as a view dependency without error', () => {
62+
const fixtureWithLoader = TestBed.createComponent(ComponentWithLoaderComponent);
63+
const componentWithLoader = fixtureWithLoader.componentInstance;
64+
expect(componentWithLoader).toBeTruthy();
65+
66+
const element = fixtureWithLoader
67+
.debugElement
68+
.query(By.css('#http-loader'));
69+
expect(element).toBeTruthy();
70+
71+
// There shouldn't be any errors in the console when the testbed cleans up the component.
72+
});
73+
5574
it('should not set the colored class if background-color is defined', () => {
5675
component.isSpinnerVisible = true;
5776
component.backgroundColor = '#ff0000';
@@ -282,7 +301,6 @@ describe('NgHttpLoaderComponent', () => {
282301

283302
const newFixture = TestBed.createComponent(NgHttpLoaderComponent);
284303
const newComponent = newFixture.componentInstance;
285-
newComponent.ngOnInit();
286304

287305
tick();
288306
expect(newComponent.isSpinnerVisible).toBeTruthy();

0 commit comments

Comments
 (0)