BETTERZON-83: FE unit testing (#35)

* BETTERZON-83: Making pre-generated unit tests work

* BETTERZON-83: Writing unit tests for angular to improve code coverage
This commit is contained in:
Patrick
2021-05-02 15:58:47 +02:00
committed by GitHub
parent a3ac897818
commit e9d03b9cbb
25 changed files with 692 additions and 21126 deletions
@@ -1,25 +1,42 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import { FooterComponent } from './footer.component';
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";
describe('FooterComponent', () => {
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
let router = {
navigate: jasmine.createSpy('navigate'),
routerState: jasmine.createSpy('routerState')
}
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ FooterComponent ]
})
.compileComponents();
});
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [{provide: Router, useValue: router}],
declarations: [FooterComponent],
imports: [
RouterTestingModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(FooterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(FooterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should navigate to /impressum when navigateImprint() is called', () => {
component.navigateImprint();
expect(router.navigate).toHaveBeenCalledWith(['/impressum']);
});
});
@@ -17,7 +17,7 @@ export class FooterComponent implements OnInit {
}
navigateImprint(): void {
this.router.navigate([('/impressum/')]);
this.router.navigate([('/impressum')]);
}
}
@@ -1,25 +1,41 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import { HeaderComponent } from './header.component';
import {HeaderComponent} from './header.component';
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<HeaderComponent>;
let component: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
let router = {
navigate: jasmine.createSpy('navigate'),
navigateByUrl: (url: string) => {
return {
then: () => {}
}
}
}
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ HeaderComponent ]
})
.compileComponents();
});
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [{provide: Router, useValue: router}],
declarations: [HeaderComponent],
imports: [
RouterTestingModule,
MatMenuModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -1,25 +1,57 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import { NewestPricesListComponent } from './newest-prices-list.component';
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";
class MockApiService extends AbstractMockObservableService {
getCurrentPricePerVendor() {
this.content = [];
return this;
}
getVendors() {
const vendor = {
vendor_id: 1,
name: 'Max Mustermann',
streetname: 'Musterstraße 69',
zip_code: '12345',
city: 'Musterhausen',
country_code: 'DE',
phone: '+49 123 4567890',
website: 'https://www.amazon.de',
};
this.content = [vendor];
return this;
}
}
describe('NewestPricesListComponent', () => {
let component: NewestPricesListComponent;
let fixture: ComponentFixture<NewestPricesListComponent>;
let component: NewestPricesListComponent;
let fixture: ComponentFixture<NewestPricesListComponent>;
let mockService;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ NewestPricesListComponent ]
})
.compileComponents();
});
beforeEach(async () => {
mockService = new MockApiService();
await TestBed.configureTestingModule({
providers: [{provide: ApiService, useValue: mockService}],
declarations: [NewestPricesListComponent],
imports: [
RouterTestingModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(NewestPricesListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(NewestPricesListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -1,25 +1,75 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import { ProductDetailsComponent } from './product-details.component';
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";
class MockApiService extends AbstractMockObservableService {
getProduct() {
this.content = {};
return this;
}
getLowestPrices() {
const price = {
price_id: 1,
product_id: 1,
vendor_id: 1,
price_in_cents: 123,
timestamp: new Date()
};
this.content = [price];
return this;
}
getAmazonPrice() {
this.content = {};
return this;
}
getVendors() {
const vendor = {
vendor_id: 1,
name: 'Max Mustermann',
streetname: 'Musterstraße 69',
zip_code: '12345',
city: 'Musterhausen',
country_code: 'DE',
phone: '+49 123 4567890',
website: 'https://www.amazon.de',
}
this.content = [vendor];
return this;
}
}
describe('ProductDetailsComponent', () => {
let component: ProductDetailsComponent;
let fixture: ComponentFixture<ProductDetailsComponent>;
let component: ProductDetailsComponent;
let fixture: ComponentFixture<ProductDetailsComponent>;
let mockService;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ProductDetailsComponent ]
})
.compileComponents();
});
beforeEach(async () => {
mockService = new MockApiService();
await TestBed.configureTestingModule({
providers: [{provide: ApiService, useValue: mockService}],
declarations: [ProductDetailsComponent],
imports: [
RouterTestingModule,
NgApexchartsModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ProductDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(ProductDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -47,7 +47,7 @@ export class ProductDetailsComponent implements OnInit {
}
getProduct(): void {
this.apiService.getProduct(this.productId).subscribe(product => this.product = product);
this.apiService.getProduct(this.productId).subscribe(product => {this.product = product});
}
getPrices(): void {
@@ -1,25 +1,79 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import { ProductListComponent } from './product-list.component';
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";
class MockApiService extends AbstractMockObservableService {
getProducts() {
this.content = [];
return this;
}
getProductsByQuery() {
this.content = [];
return this;
}
}
describe('ProductListComponent', () => {
let component: ProductListComponent;
let fixture: ComponentFixture<ProductListComponent>;
let component: ProductListComponent;
let fixture: ComponentFixture<ProductListComponent>;
let mockService;
let router = {
navigate: jasmine.createSpy('navigate'),
routerState: jasmine.createSpy('routerState')
}
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ProductListComponent ]
})
.compileComponents();
});
beforeEach(async () => {
mockService = new MockApiService();
await TestBed.configureTestingModule({
providers: [{provide: ApiService, useValue: mockService}, {provide: Router, useValue: router}],
declarations: [ProductListComponent],
imports: [
RouterTestingModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ProductListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(ProductListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should load products by search query when type is search', () => {
component.type = 'search';
component.loadParams();
expect(component.products).toBeTruthy();
});
it('should navigate to /product/xyz when navigateImprint() is called', () => {
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
}
component.clickedProduct(product);
expect(router.navigate).toHaveBeenCalledWith(['/product/1']);
});
});