Skip to content

Instantly share code, notes, and snippets.

View IAfanasovMob's full-sized avatar

Igor Afanasov IAfanasovMob

View GitHub Profile
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let httpMock: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [AppComponent]
})
@IAfanasovMob
IAfanasovMob / should-make-http-call-spec.ts
Created October 8, 2019 11:52
should make http call spec
it(`should make http call to proper GitHub API url when show button is clicked`, () => {
component.userName = 'IAfanasov';
fixture.debugElement.query(By.css('button')).nativeElement.click();
expect(() => httpMock.expectOne('https://api.github.com/users/IAfanasov/starred'))
.not.toThrow();
});
@IAfanasovMob
IAfanasovMob / app-component.html
Created October 8, 2019 11:55
app-component.html
<div class="input-group-prepend">
<span class="input-group-text"
id="basic-addon1">@</span>
</div>
<input type="text"
class="form-control"
placeholder="GitHub Username"
(keyup.enter)='loadStarred()'
(input)='userName = $event.target.value'>
<button type="button"
@IAfanasovMob
IAfanasovMob / app-component.ts
Last active October 8, 2019 11:57
app-component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
userName: string;
export interface Repo {
id: number;
created_at: string;
name: string;
stargazers_count: number;
updated_at: string;
}
it('should save list of starred repos from GitHub API when response received', () => {
component.userName = 'IAfanasov';
const repos: Repo[] = [{
id: 1,
created_at: '22-09-2019',
name: 'mock',
stargazers_count: 1000,
updated_at: '30-09-2019'
}];
loadStarred() {
this.http.get<Repo[]>(`https://api.github.com/users/${this.userName}/starred`)
.pipe(
tap(response => this.repos = response)
)
.subscribe();
}
it('should show error message when request fails', async () => {
component.userName = 'IAfanasov';
fixture.debugElement.query(By.css('button')).nativeElement.click();
const request = httpMock.expectOne('https://api.github.com/users/IAfanasov/starred');
request.error(null);
fixture.detectChanges();
await fixture.whenRenderingDone();
const alert = fixture.debugElement.query(By.css('.alert-danger'));
loadStarred() {
this.http.get<Repo[]>(`https://api.github.com/users/${this.userName}/starred`)
.pipe(
tap(
response => {
this.repos = response;
this.errorMessage = null;
}
),
catchError(error => {
<div *ngIf='errorMessage'
class="alert alert-danger"
role="alert">
{{errorMessage}}
</div>