10. TestBed and Fixtures
import { of } from 'rxjs';
import { AppComponent } from './app.component';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { AppService } from './app.service';
import { Name } from './app.models';
describe(`Component: App Component`, () => {
let appService: AppService;
let fixture: ComponentFixture<AppComponent>
let component: AppComponent;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
providers: [
{
provide: AppService,
useValue: { getNames: () => (of([])) }
}
]
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
appService = TestBed.get(AppService);
});
it('add 1+1 - PASS', () => {
expect(1 + 1).toEqual(2);
});
it(`title equals 'testing'`, () => {
expect(component.title).toEqual('testing');
});
it(`Expect service to return a single name`, () => {
component.ngOnInit();
component.names$.subscribe((names: Name[]) => {
expect(names).toEqual([]);
});
});
it(`Expect service to return a single name`, () => {
const fakeNames = [{ name: 'FAKE_NAME' }];
spyOn(appService, 'getNames').and.returnValue(of(fakeNames));
component.ngOnInit();
component.names$.subscribe((names: Name[]) => {
expect(names).toEqual(fakeNames);
});
});
});
Last updated