Skip to content

Commit c5a2bd4

Browse files
committed
fix(tests): inject dependencies in tests
1 parent 84695d3 commit c5a2bd4

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

src/app/app.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { TestBed, async } from '@angular/core/testing';
2+
import { RouterTestingModule } from '@angular/router/testing';
23
import { AppComponent } from './app.component';
34
describe('AppComponent', () => {
45
beforeEach(async(() => {
56
TestBed.configureTestingModule({
67
declarations: [
78
AppComponent
89
],
10+
imports: [ RouterTestingModule ]
911
}).compileComponents();
1012
}));
1113
it('should create the app', async(() => {
@@ -22,6 +24,6 @@ describe('AppComponent', () => {
2224
const fixture = TestBed.createComponent(AppComponent);
2325
fixture.detectChanges();
2426
const compiled = fixture.debugElement.nativeElement;
25-
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
27+
expect(compiled.querySelector('.navbar-brand-icon').textContent).toContain('Node BACStack Browser');
2628
}));
2729
});

src/app/pages/browse/browse.component.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
24

35
import { BrowseComponent } from './browse.component';
6+
import { ApiService } from '../../services/api.service';
47

58
describe('BrowseComponent', () => {
69
let component: BrowseComponent;
710
let fixture: ComponentFixture<BrowseComponent>;
811

912
beforeEach(async(() => {
1013
TestBed.configureTestingModule({
11-
declarations: [ BrowseComponent ]
14+
imports: [ RouterTestingModule, HttpClientTestingModule ],
15+
declarations: [ BrowseComponent ],
16+
providers: [ ApiService ]
17+
1218
})
1319
.compileComponents();
1420
}));

src/app/pages/objects/objects.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
24

35
import { ObjectsComponent } from './objects.component';
6+
import { ApiService } from '../../services/api.service';
47

58
describe('ObjectsComponent', () => {
69
let component: ObjectsComponent;
710
let fixture: ComponentFixture<ObjectsComponent>;
811

912
beforeEach(async(() => {
1013
TestBed.configureTestingModule({
11-
declarations: [ ObjectsComponent ]
14+
imports: [ RouterTestingModule, HttpClientTestingModule ],
15+
declarations: [ ObjectsComponent ],
16+
providers: [ ApiService ]
1217
})
1318
.compileComponents();
1419
}));

src/app/pages/properties/properties.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
24

35
import { PropertiesComponent } from './properties.component';
6+
import { ApiService } from '../../services/api.service';
47

58
describe('PropertiesComponent', () => {
69
let component: PropertiesComponent;
710
let fixture: ComponentFixture<PropertiesComponent>;
811

912
beforeEach(async(() => {
1013
TestBed.configureTestingModule({
11-
declarations: [ PropertiesComponent ]
14+
imports: [ RouterTestingModule, HttpClientTestingModule ],
15+
declarations: [ PropertiesComponent ],
16+
providers: [ ApiService ]
1217
})
1318
.compileComponents();
1419
}));

src/app/pages/settings/settings.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { FormsModule } from '@angular/forms';
3+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
24

35
import { SettingsComponent } from './settings.component';
6+
import { ApiService } from '../../services/api.service';
47

58
describe('SettingsComponent', () => {
69
let component: SettingsComponent;
710
let fixture: ComponentFixture<SettingsComponent>;
811

912
beforeEach(async(() => {
1013
TestBed.configureTestingModule({
11-
declarations: [ SettingsComponent ]
14+
imports: [ FormsModule, HttpClientTestingModule ],
15+
declarations: [ SettingsComponent ],
16+
providers: [ ApiService ]
1217
})
1318
.compileComponents();
1419
}));
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
import { TestBed, inject } from '@angular/core/testing';
2-
1+
import { TestBed, getTestBed, inject } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
33
import { ApiService } from './api.service';
44

5+
// @see: https://medium.com/netscape/testing-with-the-angular-httpclient-api-648203820712 for
6+
// an example of testing http service.
7+
58
describe('ApiService', () => {
9+
let injector: TestBed;
10+
let service: ApiService;
11+
let httpMock: HttpTestingController;
12+
613
beforeEach(() => {
714
TestBed.configureTestingModule({
8-
providers: [ApiService]
15+
imports: [ HttpClientTestingModule ],
16+
providers: [ ApiService ]
917
});
18+
injector = getTestBed();
19+
service = injector.get(ApiService);
20+
httpMock = injector.get(HttpTestingController);
21+
1022
});
1123

12-
it('should be created', inject([ApiService], (service: ApiService) => {
24+
it('should be created', () => {
1325
expect(service).toBeTruthy();
14-
}));
26+
});
1527
});

0 commit comments

Comments
 (0)