mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2025-04-19 15:29:18 +00:00
BETTERZON-83: Writing unit tests for angular to improve code coverage
This commit is contained in:
parent
cab6000233
commit
7bf8b73fb7
|
@ -1,133 +1,135 @@
|
||||||
{
|
{
|
||||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"newProjectRoot": "projects",
|
"newProjectRoot": "projects",
|
||||||
"projects": {
|
"projects": {
|
||||||
"Betterzon": {
|
"Betterzon": {
|
||||||
"projectType": "application",
|
"projectType": "application",
|
||||||
"schematics": {},
|
"schematics": {},
|
||||||
"root": "",
|
"root": "",
|
||||||
"sourceRoot": "src",
|
"sourceRoot": "src",
|
||||||
"prefix": "app",
|
"prefix": "app",
|
||||||
"architect": {
|
"architect": {
|
||||||
"build": {
|
"build": {
|
||||||
"builder": "@angular-devkit/build-angular:browser",
|
"builder": "@angular-devkit/build-angular:browser",
|
||||||
"options": {
|
"options": {
|
||||||
"outputPath": "dist/Betterzon",
|
"outputPath": "dist/Betterzon",
|
||||||
"index": "src/index.html",
|
"index": "src/index.html",
|
||||||
"main": "src/main.ts",
|
"main": "src/main.ts",
|
||||||
"polyfills": "src/polyfills.ts",
|
"polyfills": "src/polyfills.ts",
|
||||||
"tsConfig": "tsconfig.app.json",
|
"tsConfig": "tsconfig.app.json",
|
||||||
"aot": true,
|
"aot": true,
|
||||||
"assets": [
|
"assets": [
|
||||||
"src/favicon.ico",
|
"src/favicon.ico",
|
||||||
"src/assets"
|
"src/assets"
|
||||||
],
|
],
|
||||||
"styles": [
|
"styles": [
|
||||||
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
|
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
|
||||||
"src/styles.css",
|
"src/styles.css",
|
||||||
"./node_modules/cookieconsent/build/cookieconsent.min.css"
|
"./node_modules/cookieconsent/build/cookieconsent.min.css"
|
||||||
],
|
],
|
||||||
"scripts": [
|
"scripts": [
|
||||||
"./node_modules/cookieconsent/build/cookieconsent.min.js"
|
"./node_modules/cookieconsent/build/cookieconsent.min.js"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"configurations": {
|
"configurations": {
|
||||||
"production": {
|
"production": {
|
||||||
"fileReplacements": [
|
"fileReplacements": [
|
||||||
{
|
{
|
||||||
"replace": "src/environments/environment.ts",
|
"replace": "src/environments/environment.ts",
|
||||||
"with": "src/environments/environment.prod.ts"
|
"with": "src/environments/environment.prod.ts"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"optimization": true,
|
"optimization": true,
|
||||||
"outputHashing": "all",
|
"outputHashing": "all",
|
||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
"extractCss": true,
|
"extractCss": true,
|
||||||
"namedChunks": false,
|
"namedChunks": false,
|
||||||
"extractLicenses": true,
|
"extractLicenses": true,
|
||||||
"vendorChunk": false,
|
"vendorChunk": false,
|
||||||
"buildOptimizer": true,
|
"buildOptimizer": true,
|
||||||
"budgets": [
|
"budgets": [
|
||||||
{
|
{
|
||||||
"type": "initial",
|
"type": "initial",
|
||||||
"maximumWarning": "2mb",
|
"maximumWarning": "2mb",
|
||||||
"maximumError": "5mb"
|
"maximumError": "5mb"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "anyComponentStyle",
|
||||||
|
"maximumWarning": "6kb",
|
||||||
|
"maximumError": "10kb"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
"serve": {
|
||||||
"type": "anyComponentStyle",
|
"builder": "@angular-devkit/build-angular:dev-server",
|
||||||
"maximumWarning": "6kb",
|
"options": {
|
||||||
"maximumError": "10kb"
|
"browserTarget": "Betterzon:build"
|
||||||
|
},
|
||||||
|
"configurations": {
|
||||||
|
"production": {
|
||||||
|
"browserTarget": "Betterzon:build:production"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extract-i18n": {
|
||||||
|
"builder": "@angular-devkit/build-angular:extract-i18n",
|
||||||
|
"options": {
|
||||||
|
"browserTarget": "Betterzon:build"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test": {
|
||||||
|
"builder": "@angular-devkit/build-angular:karma",
|
||||||
|
"options": {
|
||||||
|
"main": "src/test.ts",
|
||||||
|
"polyfills": "src/polyfills.ts",
|
||||||
|
"tsConfig": "tsconfig.spec.json",
|
||||||
|
"karmaConfig": "karma.conf.js",
|
||||||
|
"codeCoverage": true,
|
||||||
|
"codeCoverageExclude": ["src/app/mocks/mock.service.ts"],
|
||||||
|
"assets": [
|
||||||
|
"src/favicon.ico",
|
||||||
|
"src/assets"
|
||||||
|
],
|
||||||
|
"styles": [
|
||||||
|
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
|
||||||
|
"src/styles.css",
|
||||||
|
"./node_modules/cookieconsent/build/cookieconsent.min.css"
|
||||||
|
],
|
||||||
|
"scripts": [
|
||||||
|
"./node_modules/cookieconsent/build/cookieconsent.min.js"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"lint": {
|
||||||
|
"builder": "@angular-devkit/build-angular:tslint",
|
||||||
|
"options": {
|
||||||
|
"tsConfig": [
|
||||||
|
"tsconfig.app.json",
|
||||||
|
"tsconfig.spec.json",
|
||||||
|
"e2e/tsconfig.json"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"**/node_modules/**"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"e2e": {
|
||||||
|
"builder": "@angular-devkit/build-angular:protractor",
|
||||||
|
"options": {
|
||||||
|
"protractorConfig": "e2e/protractor.conf.js",
|
||||||
|
"devServerTarget": "Betterzon:serve"
|
||||||
|
},
|
||||||
|
"configurations": {
|
||||||
|
"production": {
|
||||||
|
"devServerTarget": "Betterzon:serve:production"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
"serve": {
|
|
||||||
"builder": "@angular-devkit/build-angular:dev-server",
|
|
||||||
"options": {
|
|
||||||
"browserTarget": "Betterzon:build"
|
|
||||||
},
|
|
||||||
"configurations": {
|
|
||||||
"production": {
|
|
||||||
"browserTarget": "Betterzon:build:production"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"extract-i18n": {
|
|
||||||
"builder": "@angular-devkit/build-angular:extract-i18n",
|
|
||||||
"options": {
|
|
||||||
"browserTarget": "Betterzon:build"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"test": {
|
|
||||||
"builder": "@angular-devkit/build-angular:karma",
|
|
||||||
"options": {
|
|
||||||
"main": "src/test.ts",
|
|
||||||
"polyfills": "src/polyfills.ts",
|
|
||||||
"tsConfig": "tsconfig.spec.json",
|
|
||||||
"karmaConfig": "karma.conf.js",
|
|
||||||
"codeCoverage": true,
|
|
||||||
"assets": [
|
|
||||||
"src/favicon.ico",
|
|
||||||
"src/assets"
|
|
||||||
],
|
|
||||||
"styles": [
|
|
||||||
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
|
|
||||||
"src/styles.css",
|
|
||||||
"./node_modules/cookieconsent/build/cookieconsent.min.css"
|
|
||||||
],
|
|
||||||
"scripts": [
|
|
||||||
"./node_modules/cookieconsent/build/cookieconsent.min.js"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"lint": {
|
|
||||||
"builder": "@angular-devkit/build-angular:tslint",
|
|
||||||
"options": {
|
|
||||||
"tsConfig": [
|
|
||||||
"tsconfig.app.json",
|
|
||||||
"tsconfig.spec.json",
|
|
||||||
"e2e/tsconfig.json"
|
|
||||||
],
|
|
||||||
"exclude": [
|
|
||||||
"**/node_modules/**"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"e2e": {
|
|
||||||
"builder": "@angular-devkit/build-angular:protractor",
|
|
||||||
"options": {
|
|
||||||
"protractorConfig": "e2e/protractor.conf.js",
|
|
||||||
"devServerTarget": "Betterzon:serve"
|
|
||||||
},
|
|
||||||
"configurations": {
|
|
||||||
"production": {
|
|
||||||
"devServerTarget": "Betterzon:serve:production"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}},
|
"defaultProject": "Betterzon"
|
||||||
"defaultProject": "Betterzon"
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ describe('AppComponent', () => {
|
||||||
it('should create the app', () => {
|
it('should create the app', () => {
|
||||||
const fixture = TestBed.createComponent(AppComponent);
|
const fixture = TestBed.createComponent(AppComponent);
|
||||||
const app = fixture.componentInstance;
|
const app = fixture.componentInstance;
|
||||||
|
app.ngOnInit();
|
||||||
expect(app).toBeTruthy();
|
expect(app).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import {ImprintComponent} from './pages/imprint/imprint.component';
|
||||||
import {PrivacyComponent} from './pages/privacy/privacy.component';
|
import {PrivacyComponent} from './pages/privacy/privacy.component';
|
||||||
|
|
||||||
const routes: Routes = [
|
const routes: Routes = [
|
||||||
{path: '', component: LandingpageComponent},
|
{path: '', component: LandingpageComponent, pathMatch: 'full'},
|
||||||
{path: 'search', component: ProductSearchPageComponent},
|
{path: 'search', component: ProductSearchPageComponent},
|
||||||
{path: 'product/:id', component: ProductDetailPageComponent},
|
{path: 'product/:id', component: ProductDetailPageComponent},
|
||||||
{path: 'impressum', component: ImprintComponent},
|
{path: 'impressum', component: ImprintComponent},
|
||||||
|
|
|
@ -2,13 +2,21 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
|
|
||||||
import {FooterComponent} from './footer.component';
|
import {FooterComponent} from './footer.component';
|
||||||
import {RouterTestingModule} from "@angular/router/testing";
|
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', () => {
|
describe('FooterComponent', () => {
|
||||||
let component: FooterComponent;
|
let component: FooterComponent;
|
||||||
let fixture: ComponentFixture<FooterComponent>;
|
let fixture: ComponentFixture<FooterComponent>;
|
||||||
|
let router = {
|
||||||
|
navigate: jasmine.createSpy('navigate'),
|
||||||
|
routerState: jasmine.createSpy('routerState')
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
|
providers: [{provide: Router, useValue: router}],
|
||||||
declarations: [FooterComponent],
|
declarations: [FooterComponent],
|
||||||
imports: [
|
imports: [
|
||||||
RouterTestingModule
|
RouterTestingModule
|
||||||
|
@ -26,4 +34,9 @@ describe('FooterComponent', () => {
|
||||||
it('should create', () => {
|
it('should create', () => {
|
||||||
expect(component).toBeTruthy();
|
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 {
|
navigateImprint(): void {
|
||||||
this.router.navigate([('/impressum/')]);
|
this.router.navigate([('/impressum')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,23 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
import {HeaderComponent} from './header.component';
|
import {HeaderComponent} from './header.component';
|
||||||
import {RouterTestingModule} from "@angular/router/testing";
|
import {RouterTestingModule} from "@angular/router/testing";
|
||||||
import {MatMenuModule} from "@angular/material/menu";
|
import {MatMenuModule} from "@angular/material/menu";
|
||||||
|
import {Router} from "@angular/router";
|
||||||
|
|
||||||
describe('HeaderComponent', () => {
|
describe('HeaderComponent', () => {
|
||||||
let component: HeaderComponent;
|
let component: HeaderComponent;
|
||||||
let fixture: ComponentFixture<HeaderComponent>;
|
let fixture: ComponentFixture<HeaderComponent>;
|
||||||
|
let router = {
|
||||||
|
navigate: jasmine.createSpy('navigate'),
|
||||||
|
navigateByUrl: (url: string) => {
|
||||||
|
return {
|
||||||
|
then: () => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
|
providers: [{provide: Router, useValue: router}],
|
||||||
declarations: [HeaderComponent],
|
declarations: [HeaderComponent],
|
||||||
imports: [
|
imports: [
|
||||||
RouterTestingModule,
|
RouterTestingModule,
|
||||||
|
|
|
@ -8,10 +8,22 @@ import {ApiService} from "../../services/api.service";
|
||||||
|
|
||||||
class MockApiService extends AbstractMockObservableService {
|
class MockApiService extends AbstractMockObservableService {
|
||||||
getCurrentPricePerVendor() {
|
getCurrentPricePerVendor() {
|
||||||
|
this.content = [];
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
getVendors() {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,17 @@ class MockApiService extends AbstractMockObservableService {
|
||||||
}
|
}
|
||||||
|
|
||||||
getVendors() {
|
getVendors() {
|
||||||
this.content = [];
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,16 @@ import {HeaderComponent} from "../header/header.component";
|
||||||
import {RouterTestingModule} from "@angular/router/testing";
|
import {RouterTestingModule} from "@angular/router/testing";
|
||||||
import {ApiService} from "../../services/api.service";
|
import {ApiService} from "../../services/api.service";
|
||||||
import {AbstractMockObservableService} from "../../mocks/mock.service";
|
import {AbstractMockObservableService} from "../../mocks/mock.service";
|
||||||
|
import {Router} from "@angular/router";
|
||||||
|
|
||||||
class MockApiService extends AbstractMockObservableService {
|
class MockApiService extends AbstractMockObservableService {
|
||||||
getProducts() {
|
getProducts() {
|
||||||
|
this.content = [];
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
getProductsByQuery() {
|
getProductsByQuery() {
|
||||||
|
this.content = [];
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,11 +24,15 @@ describe('ProductListComponent', () => {
|
||||||
let component: ProductListComponent;
|
let component: ProductListComponent;
|
||||||
let fixture: ComponentFixture<ProductListComponent>;
|
let fixture: ComponentFixture<ProductListComponent>;
|
||||||
let mockService;
|
let mockService;
|
||||||
|
let router = {
|
||||||
|
navigate: jasmine.createSpy('navigate'),
|
||||||
|
routerState: jasmine.createSpy('routerState')
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
mockService = new MockApiService();
|
mockService = new MockApiService();
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
providers: [{provide: ApiService, useValue: mockService}],
|
providers: [{provide: ApiService, useValue: mockService}, {provide: Router, useValue: router}],
|
||||||
declarations: [ProductListComponent],
|
declarations: [ProductListComponent],
|
||||||
imports: [
|
imports: [
|
||||||
RouterTestingModule
|
RouterTestingModule
|
||||||
|
@ -43,4 +50,30 @@ describe('ProductListComponent', () => {
|
||||||
it('should create', () => {
|
it('should create', () => {
|
||||||
expect(component).toBeTruthy();
|
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']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,13 +2,19 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
|
|
||||||
import {LandingpageComponent} from './landingpage.component';
|
import {LandingpageComponent} from './landingpage.component';
|
||||||
import {RouterTestingModule} from "@angular/router/testing";
|
import {RouterTestingModule} from "@angular/router/testing";
|
||||||
|
import {Router} from "@angular/router";
|
||||||
|
|
||||||
describe('LandingpageComponent', () => {
|
describe('LandingpageComponent', () => {
|
||||||
let component: LandingpageComponent;
|
let component: LandingpageComponent;
|
||||||
let fixture: ComponentFixture<LandingpageComponent>;
|
let fixture: ComponentFixture<LandingpageComponent>;
|
||||||
|
let router = {
|
||||||
|
navigate: jasmine.createSpy('navigate'),
|
||||||
|
routerState: jasmine.createSpy('routerState')
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
|
providers: [{provide: Router, useValue: router}],
|
||||||
declarations: [LandingpageComponent],
|
declarations: [LandingpageComponent],
|
||||||
imports: [
|
imports: [
|
||||||
RouterTestingModule
|
RouterTestingModule
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
||||||
{
|
{
|
||||||
"extends": "./tsconfig.json",
|
"extends": "./tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "./out-tsc/spec",
|
"outDir": "./out-tsc/spec",
|
||||||
"types": [
|
"types": [
|
||||||
"jasmine"
|
"jasmine"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"src/test.ts",
|
||||||
|
"src/polyfills.ts"
|
||||||
|
],
|
||||||
|
"include": [
|
||||||
|
"src/**/*.spec.ts",
|
||||||
|
"src/**/*.d.ts"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"src/test.ts",
|
|
||||||
"src/polyfills.ts"
|
|
||||||
],
|
|
||||||
"include": [
|
|
||||||
"src/**/*.spec.ts",
|
|
||||||
"src/**/*.d.ts"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user