From 3874957d5aa595a220572faeec17ff5e3dd6090c Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Mon, 10 May 2021 20:11:45 +0200 Subject: [PATCH] BETTERZON-89: Refactoring / Reformatting and adding unit tests (#41) --- .../footer/footer.component.spec.ts | 12 +++--- .../header/header.component.spec.ts | 15 +++---- .../newest-prices-list.component.spec.ts | 13 +++--- .../product-details.component.spec.ts | 41 ++++++++++++++----- .../product-list.component.spec.ts | 22 +++++----- .../landingpage/landingpage.component.spec.ts | 8 ++-- .../product-detail-page.component.spec.ts | 2 +- .../product-search-page.component.spec.ts | 8 ++-- 8 files changed, 72 insertions(+), 49 deletions(-) diff --git a/Frontend/src/app/components/footer/footer.component.spec.ts b/Frontend/src/app/components/footer/footer.component.spec.ts index d75b1f9..f7cfecb 100644 --- a/Frontend/src/app/components/footer/footer.component.spec.ts +++ b/Frontend/src/app/components/footer/footer.component.spec.ts @@ -1,18 +1,18 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {FooterComponent} from './footer.component'; -import {RouterTestingModule} from "@angular/router/testing"; -import {AppComponent} from "../../app.component"; -import {ImprintComponent} from "../../pages/imprint/imprint.component"; -import {ActivatedRoute, Router} from "@angular/router"; +import {RouterTestingModule} from '@angular/router/testing'; +import {AppComponent} from '../../app.component'; +import {ImprintComponent} from '../../pages/imprint/imprint.component'; +import {ActivatedRoute, Router} from '@angular/router'; describe('FooterComponent', () => { let component: FooterComponent; let fixture: ComponentFixture; - let router = { + const router = { navigate: jasmine.createSpy('navigate'), routerState: jasmine.createSpy('routerState') - } + }; beforeEach(async () => { await TestBed.configureTestingModule({ diff --git a/Frontend/src/app/components/header/header.component.spec.ts b/Frontend/src/app/components/header/header.component.spec.ts index b2ecacd..33cfe35 100644 --- a/Frontend/src/app/components/header/header.component.spec.ts +++ b/Frontend/src/app/components/header/header.component.spec.ts @@ -1,21 +1,22 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {HeaderComponent} from './header.component'; -import {RouterTestingModule} from "@angular/router/testing"; -import {MatMenuModule} from "@angular/material/menu"; -import {Router} from "@angular/router"; +import {RouterTestingModule} from '@angular/router/testing'; +import {MatMenuModule} from '@angular/material/menu'; +import {Router} from '@angular/router'; describe('HeaderComponent', () => { let component: HeaderComponent; let fixture: ComponentFixture; - let router = { + const router = { navigate: jasmine.createSpy('navigate'), navigateByUrl: (url: string) => { return { - then: () => {} - } + then: () => { + } + }; } - } + }; beforeEach(async () => { await TestBed.configureTestingModule({ diff --git a/Frontend/src/app/components/newest-prices-list/newest-prices-list.component.spec.ts b/Frontend/src/app/components/newest-prices-list/newest-prices-list.component.spec.ts index 959cbb3..43d2ac9 100644 --- a/Frontend/src/app/components/newest-prices-list/newest-prices-list.component.spec.ts +++ b/Frontend/src/app/components/newest-prices-list/newest-prices-list.component.spec.ts @@ -1,18 +1,19 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {NewestPricesListComponent} from './newest-prices-list.component'; -import {RouterTestingModule} from "@angular/router/testing"; -import {HttpClient} from "@angular/common/http"; -import {AbstractMockObservableService} from "../../mocks/mock.service"; -import {ApiService} from "../../services/api.service"; +import {RouterTestingModule} from '@angular/router/testing'; +import {HttpClient} from '@angular/common/http'; +import {AbstractMockObservableService} from '../../mocks/mock.service'; +import {ApiService} from '../../services/api.service'; +import {Observable} from 'rxjs'; class MockApiService extends AbstractMockObservableService { - getCurrentPricePerVendor() { + getCurrentPricePerVendor(): any { this.content = []; return this; } - getVendors() { + getVendors(): any { const vendor = { vendor_id: 1, name: 'Max Mustermann', diff --git a/Frontend/src/app/components/product-details/product-details.component.spec.ts b/Frontend/src/app/components/product-details/product-details.component.spec.ts index 15b2fa4..bcd489d 100644 --- a/Frontend/src/app/components/product-details/product-details.component.spec.ts +++ b/Frontend/src/app/components/product-details/product-details.component.spec.ts @@ -1,18 +1,33 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ProductDetailsComponent} from './product-details.component'; -import {RouterTestingModule} from "@angular/router/testing"; -import {AbstractMockObservableService} from "../../mocks/mock.service"; -import {ApiService} from "../../services/api.service"; -import {ChartComponent, NgApexchartsModule} from "ng-apexcharts"; +import {RouterTestingModule} from '@angular/router/testing'; +import {AbstractMockObservableService} from '../../mocks/mock.service'; +import {ApiService} from '../../services/api.service'; +import {ChartComponent, NgApexchartsModule} from 'ng-apexcharts'; +import {By} from '@angular/platform-browser'; class MockApiService extends AbstractMockObservableService { - getProduct() { - this.content = {}; + getProduct(): any { + const product = { + product_id: 1, + asin: 'ASIN', + is_active: true, + name: 'Super tolles Produkt', + short_description: 'Descr', + long_description: 'Descr', + image_guid: '123', + date_added: new Date(), + last_modified: new Date(), + manufacturer_id: 1, + selling_rank: '1', + category_id: 1 + }; + this.content = product; return this; } - getLowestPrices() { + getLowestPrices(): any { const price = { price_id: 1, product_id: 1, @@ -24,12 +39,12 @@ class MockApiService extends AbstractMockObservableService { return this; } - getAmazonPrice() { + getAmazonPrice(): any { this.content = {}; return this; } - getVendors() { + getVendors(): any { const vendor = { vendor_id: 1, name: 'Max Mustermann', @@ -39,7 +54,7 @@ class MockApiService extends AbstractMockObservableService { country_code: 'DE', phone: '+49 123 4567890', website: 'https://www.amazon.de', - } + }; this.content = [vendor]; return this; } @@ -72,4 +87,10 @@ describe('ProductDetailsComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + it('should show the product name', () => { + component.ngOnInit(); + const title = fixture.debugElement.query(By.css('.productTitle')); + expect(title.nativeElement.innerText).toEqual('Super tolles Produkt'); + }); }); diff --git a/Frontend/src/app/components/product-list/product-list.component.spec.ts b/Frontend/src/app/components/product-list/product-list.component.spec.ts index b0c733f..46b10a1 100644 --- a/Frontend/src/app/components/product-list/product-list.component.spec.ts +++ b/Frontend/src/app/components/product-list/product-list.component.spec.ts @@ -1,20 +1,20 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ProductListComponent} from './product-list.component'; -import {FooterComponent} from "../footer/footer.component"; -import {HeaderComponent} from "../header/header.component"; -import {RouterTestingModule} from "@angular/router/testing"; -import {ApiService} from "../../services/api.service"; -import {AbstractMockObservableService} from "../../mocks/mock.service"; -import {Router} from "@angular/router"; +import {FooterComponent} from '../footer/footer.component'; +import {HeaderComponent} from '../header/header.component'; +import {RouterTestingModule} from '@angular/router/testing'; +import {ApiService} from '../../services/api.service'; +import {AbstractMockObservableService} from '../../mocks/mock.service'; +import {Router} from '@angular/router'; class MockApiService extends AbstractMockObservableService { - getProducts() { + getProducts(): any { this.content = []; return this; } - getProductsByQuery() { + getProductsByQuery(): any { this.content = []; return this; } @@ -24,10 +24,10 @@ describe('ProductListComponent', () => { let component: ProductListComponent; let fixture: ComponentFixture; let mockService; - let router = { + const router = { navigate: jasmine.createSpy('navigate'), routerState: jasmine.createSpy('routerState') - } + }; beforeEach(async () => { mockService = new MockApiService(); @@ -71,7 +71,7 @@ describe('ProductListComponent', () => { manufacturer_id: 1, selling_rank: '1', category_id: 1 - } + }; component.clickedProduct(product); expect(router.navigate).toHaveBeenCalledWith(['/product/1']); diff --git a/Frontend/src/app/pages/landingpage/landingpage.component.spec.ts b/Frontend/src/app/pages/landingpage/landingpage.component.spec.ts index 92bdbb7..187bfdd 100644 --- a/Frontend/src/app/pages/landingpage/landingpage.component.spec.ts +++ b/Frontend/src/app/pages/landingpage/landingpage.component.spec.ts @@ -1,16 +1,16 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {LandingpageComponent} from './landingpage.component'; -import {RouterTestingModule} from "@angular/router/testing"; -import {Router} from "@angular/router"; +import {RouterTestingModule} from '@angular/router/testing'; +import {Router} from '@angular/router'; describe('LandingpageComponent', () => { let component: LandingpageComponent; let fixture: ComponentFixture; - let router = { + const router = { navigate: jasmine.createSpy('navigate'), routerState: jasmine.createSpy('routerState') - } + }; beforeEach(async () => { await TestBed.configureTestingModule({ diff --git a/Frontend/src/app/pages/product-detail-page/product-detail-page.component.spec.ts b/Frontend/src/app/pages/product-detail-page/product-detail-page.component.spec.ts index 10faa22..cfb33ce 100644 --- a/Frontend/src/app/pages/product-detail-page/product-detail-page.component.spec.ts +++ b/Frontend/src/app/pages/product-detail-page/product-detail-page.component.spec.ts @@ -1,7 +1,7 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ProductDetailPageComponent} from './product-detail-page.component'; -import {RouterTestingModule} from "@angular/router/testing"; +import {RouterTestingModule} from '@angular/router/testing'; describe('ProductDetailPageComponent', () => { let component: ProductDetailPageComponent; diff --git a/Frontend/src/app/pages/product-search-page/product-search-page.component.spec.ts b/Frontend/src/app/pages/product-search-page/product-search-page.component.spec.ts index be7670f..a953b18 100644 --- a/Frontend/src/app/pages/product-search-page/product-search-page.component.spec.ts +++ b/Frontend/src/app/pages/product-search-page/product-search-page.component.spec.ts @@ -1,10 +1,10 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ProductSearchPageComponent} from './product-search-page.component'; -import {HeaderComponent} from "../../components/header/header.component"; -import {FooterComponent} from "../../components/footer/footer.component"; -import {ProductListComponent} from "../../components/product-list/product-list.component"; -import {RouterTestingModule} from "@angular/router/testing"; +import {HeaderComponent} from '../../components/header/header.component'; +import {FooterComponent} from '../../components/footer/footer.component'; +import {ProductListComponent} from '../../components/product-list/product-list.component'; +import {RouterTestingModule} from '@angular/router/testing'; describe('ProductSearchPageComponent', () => { let component: ProductSearchPageComponent;