45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
import { AppComponent } from './app.component';
|
|
|
|
describe('AppComponent', () => {
|
|
let component: AppComponent;
|
|
let fixture: ComponentFixture<AppComponent>;
|
|
let httpMock: HttpTestingController;
|
|
|
|
beforeEach(async () => {
|
|
await TestBed.configureTestingModule({
|
|
declarations: [AppComponent],
|
|
imports: [HttpClientTestingModule]
|
|
}).compileComponents();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
fixture = TestBed.createComponent(AppComponent);
|
|
component = fixture.componentInstance;
|
|
httpMock = TestBed.inject(HttpTestingController);
|
|
});
|
|
|
|
afterEach(() => {
|
|
httpMock.verify();
|
|
});
|
|
|
|
it('should create the app', () => {
|
|
expect(component).toBeTruthy();
|
|
});
|
|
|
|
it('should retrieve weather forecasts from the server', () => {
|
|
const mockForecasts = [
|
|
{ date: '2021-10-01', temperatureC: 20, temperatureF: 68, summary: 'Mild' },
|
|
{ date: '2021-10-02', temperatureC: 25, temperatureF: 77, summary: 'Warm' }
|
|
];
|
|
|
|
component.ngOnInit();
|
|
|
|
const req = httpMock.expectOne('/weatherforecast');
|
|
expect(req.request.method).toEqual('GET');
|
|
req.flush(mockForecasts);
|
|
|
|
expect(component.forecasts).toEqual(mockForecasts);
|
|
});
|
|
}); |