From f16a9fb392aaa5ff8d336c74aad2150c0f30cddd Mon Sep 17 00:00:00 2001 From: Konstantinos Kamaropoulos Date: Mon, 2 Mar 2020 16:15:07 +0200 Subject: [PATCH] feat: Remove boilerplate Quotes service and functionality from the home page --- src/app/home/home.component.html | 7 +--- src/app/home/home.component.scss | 6 --- src/app/home/home.component.ts | 16 +------- src/app/home/home.module.ts | 1 - src/app/home/quote.service.spec.ts | 59 ------------------------------ src/app/home/quote.service.ts | 30 --------------- 6 files changed, 2 insertions(+), 117 deletions(-) delete mode 100644 src/app/home/quote.service.spec.ts delete mode 100644 src/app/home/quote.service.ts diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html index a0b5115..bf349ea 100644 --- a/src/app/home/home.component.html +++ b/src/app/home/home.component.html @@ -1,10 +1,5 @@
-

- - Hello world ! -

- - {{ quote }} + Home Page
diff --git a/src/app/home/home.component.scss b/src/app/home/home.component.scss index 9b60463..3db07d5 100644 --- a/src/app/home/home.component.scss +++ b/src/app/home/home.component.scss @@ -1,9 +1,3 @@ .logo { width: 100px; } - -q { - font-style: italic; - font-size: 1.2rem; - quotes: "« " " »"; -} diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 805c9b6..d48df63 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -1,7 +1,4 @@ import { Component, OnInit } from '@angular/core'; -import { finalize } from 'rxjs/operators'; - -import { QuoteService } from './quote.service'; @Component({ selector: 'app-home', @@ -9,22 +6,11 @@ import { QuoteService } from './quote.service'; styleUrls: ['./home.component.scss'] }) export class HomeComponent implements OnInit { - quote: string | undefined; isLoading = false; - constructor(private quoteService: QuoteService) {} + constructor() {} ngOnInit() { this.isLoading = true; - this.quoteService - .getRandomQuote({ category: 'dev' }) - .pipe( - finalize(() => { - this.isLoading = false; - }) - ) - .subscribe((quote: string) => { - this.quote = quote; - }); } } diff --git a/src/app/home/home.module.ts b/src/app/home/home.module.ts index a49e60d..c2214d0 100644 --- a/src/app/home/home.module.ts +++ b/src/app/home/home.module.ts @@ -6,7 +6,6 @@ import { CoreModule } from '@app/core'; import { SharedModule } from '@app/shared'; import { HomeRoutingModule } from './home-routing.module'; import { HomeComponent } from './home.component'; -import { QuoteService } from './quote.service'; @NgModule({ imports: [CommonModule, TranslateModule, CoreModule, SharedModule, HomeRoutingModule], diff --git a/src/app/home/quote.service.spec.ts b/src/app/home/quote.service.spec.ts deleted file mode 100644 index faa6e6c..0000000 --- a/src/app/home/quote.service.spec.ts +++ /dev/null @@ -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); - - 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' - }); - }); - }); -}); diff --git a/src/app/home/quote.service.ts b/src/app/home/quote.service.ts deleted file mode 100644 index 6b0cc49..0000000 --- a/src/app/home/quote.service.ts +++ /dev/null @@ -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 { - return this.httpClient - .cache() - .get(routes.quote(context)) - .pipe( - map((body: any) => body.value), - catchError(() => of('Error, could not load joke :-(')) - ); - } -}