Konstantinos Kamaropoulos
5 years ago
6 changed files with 2 additions and 117 deletions
@ -1,10 +1,5 @@ |
|||||
<div class="container-fluid"> |
<div class="container-fluid"> |
||||
<div class="jumbotron text-center"> |
<div class="jumbotron text-center"> |
||||
<h1> |
Home Page |
||||
<img class="logo" src="assets/ngx-rocket-logo.png" alt="angular logo" /> |
|
||||
<span translate>Hello world !</span> |
|
||||
</h1> |
|
||||
<app-loader [isLoading]="isLoading"></app-loader> |
|
||||
<q [hidden]="isLoading">{{ quote }}</q> |
|
||||
</div> |
</div> |
||||
</div> |
</div> |
||||
|
@ -1,59 +0,0 @@ |
|||||
import { Type } from '@angular/core'; |
|
||||
import { TestBed, async } from '@angular/core/testing'; |
|
||||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
|
||||
|
|
||||
import { CoreModule, HttpCacheService } from '@app/core'; |
|
||||
import { QuoteService } from './quote.service'; |
|
||||
|
|
||||
describe('QuoteService', () => { |
|
||||
let quoteService: QuoteService; |
|
||||
let httpMock: HttpTestingController; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
TestBed.configureTestingModule({ |
|
||||
imports: [CoreModule, HttpClientTestingModule], |
|
||||
providers: [HttpCacheService, QuoteService] |
|
||||
}); |
|
||||
|
|
||||
quoteService = TestBed.get(QuoteService); |
|
||||
httpMock = TestBed.get(HttpTestingController as Type<HttpTestingController>); |
|
||||
|
|
||||
const htttpCacheService = TestBed.get(HttpCacheService); |
|
||||
htttpCacheService.cleanCache(); |
|
||||
}); |
|
||||
|
|
||||
afterEach(() => { |
|
||||
httpMock.verify(); |
|
||||
}); |
|
||||
|
|
||||
describe('getRandomQuote', () => { |
|
||||
it('should return a random Chuck Norris quote', () => { |
|
||||
// Arrange
|
|
||||
const mockQuote = { value: 'a random quote' }; |
|
||||
|
|
||||
// Act
|
|
||||
const randomQuoteSubscription = quoteService.getRandomQuote({ category: 'toto' }); |
|
||||
|
|
||||
// Assert
|
|
||||
randomQuoteSubscription.subscribe((quote: string) => { |
|
||||
expect(quote).toEqual(mockQuote.value); |
|
||||
}); |
|
||||
httpMock.expectOne({}).flush(mockQuote); |
|
||||
}); |
|
||||
|
|
||||
it('should return a string in case of error', () => { |
|
||||
// Act
|
|
||||
const randomQuoteSubscription = quoteService.getRandomQuote({ category: 'toto' }); |
|
||||
|
|
||||
// Assert
|
|
||||
randomQuoteSubscription.subscribe((quote: string) => { |
|
||||
expect(typeof quote).toEqual('string'); |
|
||||
expect(quote).toContain('Error'); |
|
||||
}); |
|
||||
httpMock.expectOne({}).flush(null, { |
|
||||
status: 500, |
|
||||
statusText: 'error' |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
@ -1,30 +0,0 @@ |
|||||
import { Injectable } from '@angular/core'; |
|
||||
import { HttpClient } from '@angular/common/http'; |
|
||||
import { Observable, of } from 'rxjs'; |
|
||||
import { map, catchError } from 'rxjs/operators'; |
|
||||
|
|
||||
const routes = { |
|
||||
quote: (c: RandomQuoteContext) => `/jokes/random?category=${c.category}` |
|
||||
}; |
|
||||
|
|
||||
export interface RandomQuoteContext { |
|
||||
// The quote's category: 'dev', 'explicit'...
|
|
||||
category: string; |
|
||||
} |
|
||||
|
|
||||
@Injectable({ |
|
||||
providedIn: 'root' |
|
||||
}) |
|
||||
export class QuoteService { |
|
||||
constructor(private httpClient: HttpClient) {} |
|
||||
|
|
||||
getRandomQuote(context: RandomQuoteContext): Observable<string> { |
|
||||
return this.httpClient |
|
||||
.cache() |
|
||||
.get(routes.quote(context)) |
|
||||
.pipe( |
|
||||
map((body: any) => body.value), |
|
||||
catchError(() => of('Error, could not load joke :-(')) |
|
||||
); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue