Skip to content

Commit 38222f3

Browse files
authored
Merge pull request #123 from marc101101/feature/testing
Feature/testing
2 parents 34acdea + 0f886c6 commit 38222f3

36 files changed

Lines changed: 98309 additions & 2770 deletions
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { async, TestBed, fakeAsync, tick } from '@angular/core/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { AlertService } from '../../../services/alert.service';
4+
import { RouterTestingModule } from '@angular/router/testing';
5+
import { SharedModule } from '../../../sharedModule/shared.module';
6+
import { Observable, of } from 'rxjs';
7+
import 'rxjs/add/observable/from';
8+
import { CategoryService } from '../../../services/category.service';
9+
import { CommunicationService } from '../shared/communication.service';
10+
import { CategoriesComponent } from './categories.component';
11+
import {Location} from "@angular/common";
12+
13+
14+
15+
/**
16+
* Test should test all four methods of courses.component.ts
17+
* ngOnInit() / routeToCourse()
18+
**/
19+
describe('CategoriesComponent', () => {
20+
let categoriesModel = [{
21+
"KURS_NAME":"Test Kurs",
22+
"KURS_BESCHREIBUNG": "Test Kurs Beschreibung"
23+
}];
24+
25+
var fixture;
26+
var component;
27+
var categoryService: CategoryService;
28+
var location;
29+
30+
beforeEach(async(() => {
31+
TestBed.configureTestingModule({
32+
declarations: [ CategoriesComponent ],
33+
imports: [ HttpClientModule, RouterTestingModule, SharedModule ],
34+
providers: [ CategoryService, AlertService, CommunicationService, Location],
35+
})
36+
.compileComponents();
37+
38+
fixture = TestBed.createComponent(CategoriesComponent);
39+
component = fixture.componentInstance;
40+
categoryService = fixture.debugElement.injector.get(CategoryService);
41+
location = TestBed.get(Location);
42+
}));
43+
44+
it('CategoriesComponent: should successfuly be able to create a CategoriesComponent', () => {
45+
expect(fixture.componentInstance instanceof CategoriesComponent).toBe(true, "should create CategoriesComponent");
46+
});
47+
48+
//test ngOnit methods and check its effects by mocking userService method getUserMe
49+
it("CategoriesComponent: ngOnit() sets categories and dataIsAvailable values correctly", fakeAsync(() => {
50+
//set preconditions
51+
spyOn(categoryService, "getAllCategories").and.returnValue(Observable.of(categoriesModel));
52+
//call testing method
53+
component.ngOnInit();
54+
//check results
55+
fixture.detectChanges();
56+
expect(component.categories).toBe(categoriesModel);
57+
expect(component.dataIsAvailable).toBe(true);
58+
}));
59+
60+
it('CategoriesComponent: navigate to course(id: 1111) redirects you to /#/home/kurs-uebersicht/1111', fakeAsync(() => {
61+
component.routeToCourse("1111", "primary");
62+
tick(50);
63+
expect(location._platformStrategy.internalPath).toBe('/#/home/kurs-uebersicht/1111');
64+
}));
65+
});

client/app/components/home/categories/categories.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class CategoriesComponent implements OnInit {
2525
}
2626

2727
routeToCourse(categoryId: number, color: string, category: string) {
28-
this.router.navigateByUrl('home/kurs-uebersicht/' + categoryId);
28+
this.router.navigateByUrl('#/home/kurs-uebersicht/' + categoryId);
2929
this.comService.setInfo(color, category);
3030
}
3131

client/app/components/home/contact/contact.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<div style="padding:1rem">
1414
<textarea class="textarea" placeholder="Wir freuen uns über Ihr Feedback." [(ngModel)]="contactMessage.text" ></textarea>
1515
</div>
16+
1617
<div class="colum is-12" style="text-align: center; font-size: 2rem;">
1718
<i *ngIf="contactMessage.bewertung<1" class="fa fa-star-o" aria-hidden="true" (click)="contactMessage.bewertung = 1"></i>
1819
<i *ngIf="contactMessage.bewertung>=1" class="fa fa-star" aria-hidden="true" (click)="contactMessage.bewertung = 1"></i>
@@ -29,6 +30,7 @@
2930
<i *ngIf="contactMessage.bewertung<5" class="fa fa-star-o" aria-hidden="true" (click)="contactMessage.bewertung = 5"></i>
3031
<i *ngIf="contactMessage.bewertung>=5" class="fa fa-star" aria-hidden="true" (click)="contactMessage.bewertung = 5"></i>
3132
</div>
33+
3234
<div class="column is-12" style="padding: 1rem; padding-top:0rem">
3335
<button #send class="button is-primary is-medium is-fullwidth" style="margin-top:1.5rem" [disabled]="contactMessage.text == ''" (click)="submit()">
3436
<div>{{button_text}}</div>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { async, TestBed, fakeAsync } from '@angular/core/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { AlertService } from '../../../services/alert.service';
4+
import { RouterTestingModule } from '@angular/router/testing';
5+
import { SharedModule } from '../../../sharedModule/shared.module';
6+
import { Observable, of } from 'rxjs';
7+
import 'rxjs/add/observable/from';
8+
import { CommunicationService } from '../shared/communication.service';
9+
import { CoursesService } from '../shared/courses.service';
10+
import { ActivatedRoute } from '@angular/router';
11+
import { ContactService } from '../shared/contact.service';
12+
import { ContactComponent } from './contact.component';
13+
import { FormsModule } from '@angular/forms';
14+
15+
16+
/**
17+
* Test should test all four methods of contact.component.ts
18+
* ngOnit() / submit()
19+
**/
20+
describe('ContactComponent', () => {
21+
22+
let coursesByCourseIdModel = {
23+
"ANM_DATUM": Date.now(),
24+
"KURS_NAME": "Testname",
25+
"KURS_BESCHREIBUNG": "Testbeschreibung"
26+
};
27+
28+
var fixture;
29+
var component;
30+
var coursesService: CoursesService;
31+
var contactService: ContactService;
32+
33+
beforeEach(async(() => {
34+
TestBed.configureTestingModule({
35+
declarations: [ ContactComponent ],
36+
imports: [ HttpClientModule, RouterTestingModule, SharedModule, FormsModule ],
37+
providers: [
38+
AlertService,
39+
CommunicationService,
40+
ContactService,
41+
CoursesService,
42+
{
43+
provide: ActivatedRoute,
44+
useValue: {
45+
params: Observable.of({ id: 'test-id' })
46+
}
47+
}],
48+
})
49+
.compileComponents();
50+
51+
fixture = TestBed.createComponent(ContactComponent);
52+
component = fixture.componentInstance;
53+
coursesService = fixture.debugElement.injector.get(CoursesService);
54+
contactService = fixture.debugElement.injector.get(ContactService);
55+
}));
56+
57+
it('ContactComponent: should successfuly be able to create a ContactComponent and check ngOninit', () => {
58+
//set preconditions
59+
component.activatedRoute.params.value.id = "test-id";
60+
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
61+
spyOn(coursesService, "getCoursesByCourseId").and.returnValue(Observable.of(coursesByCourseIdModel));
62+
//call testing method
63+
component.ngOnInit();
64+
//check results
65+
fixture.detectChanges();
66+
expect(component.single_course).toBe(coursesByCourseIdModel);
67+
expect(component.headerText).toBe("Bewertung: " + component.single_course.KURS_NAME);
68+
expect(component.course_feedback).toBe(true);
69+
expect(fixture.componentInstance instanceof ContactComponent).toBe(true, "should create ContactComponent");
70+
});
71+
72+
it('ContactComponent: should successfuly submit feedback for course', () => {
73+
//set preconditions
74+
component.course_feedback = true;
75+
component.single_course = {"KURS_ID": 1234};
76+
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
77+
spyOn(coursesService, "postFeedbackByCourse").and.returnValue(Observable.of(coursesByCourseIdModel));
78+
//call testing method
79+
component.submit();
80+
//check results
81+
fixture.detectChanges();
82+
expect(component.button_text).toBe("Senden erfolgreich");
83+
});
84+
85+
it('ContactComponent: should successfuly submit feedback for app in general', () => {
86+
//set preconditions
87+
component.course_feedback = false;
88+
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
89+
spyOn(contactService, "postContactFeedback").and.returnValue(Observable.of(coursesByCourseIdModel));
90+
//call testing method
91+
component.submit();
92+
//check results
93+
fixture.detectChanges();
94+
expect(component.button_text).toBe("Senden erfolgreich");
95+
});
96+
97+
});

client/app/components/home/contact/contact.component.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit, ViewChild, Renderer2, ElementRef } from '@angular/core';
2-
import { Router, ActivatedRoute, Params } from '@angular/router';
2+
import { ActivatedRoute, Params } from '@angular/router';
33
import { ContactService } from '../shared/contact.service';
4-
import { log } from 'util';
54
import { CoursesService } from '../shared/courses.service';
65

76
@Component({
@@ -23,14 +22,13 @@ export class ContactComponent implements OnInit{
2322
@ViewChild('send') sendButton: ElementRef;
2423

2524
constructor(
26-
private router: Router,
2725
private renderer: Renderer2,
2826
private contactService: ContactService,
2927
private activatedRoute: ActivatedRoute,
3028
private coursesService:CoursesService) { }
3129

3230
ngOnInit(){
33-
this.activatedRoute.params.subscribe((params: Params) => {
31+
this.activatedRoute.params.subscribe((params: Params) => {
3432
if(params.id){
3533
this.coursesService.getCoursesByCourseId(params.id).subscribe(response =>{
3634
this.single_course = response;
@@ -44,7 +42,6 @@ export class ContactComponent implements OnInit{
4442
submit():void{
4543
if(this.course_feedback){
4644
this.coursesService.postFeedbackByCourse(this.contactMessage, this.single_course.KURS_ID).subscribe(response => {
47-
console.log(response);
4845
if(response.name != "HttpResponseError"){
4946
this.renderer.addClass(this.sendButton.nativeElement, 'is-primary-save');
5047
this.button_text = 'Senden erfolgreich';
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { async, TestBed, fakeAsync } from '@angular/core/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { AlertService } from '../../../services/alert.service';
4+
import { RouterTestingModule } from '@angular/router/testing';
5+
import { SharedModule } from '../../../sharedModule/shared.module';
6+
import { Observable, of } from 'rxjs';
7+
import 'rxjs/add/observable/from';
8+
import { CoursesComponent } from './courses.component';
9+
import { CommunicationService } from '../shared/communication.service';
10+
import { UserService } from '../../../services/user.service';
11+
import { CoursesService } from '../shared/courses.service';
12+
import { CategoryService } from '../../../services/category.service';
13+
import { ActivatedRoute } from '@angular/router';
14+
15+
16+
/**
17+
* Test should test all four methods of courses.component.ts
18+
* requestCoursesByUser() / requestCoursesByCategory()
19+
**/
20+
describe('CoursesComponent', () => {
21+
let userCoursesModel = [
22+
{
23+
"ANM_KURS_ID": 1,
24+
"ANM_DATUM": Date.now()
25+
},
26+
{
27+
"ANM_KURS_ID": 2,
28+
"ANM_DATUM": Date.now()
29+
}
30+
];
31+
32+
let coursesByCourseIdModel = {
33+
"ANM_DATUM": Date.now(),
34+
"KURS_NAME": "Testname",
35+
"KURS_BESCHREIBUNG": "Testbeschreibung"
36+
};
37+
38+
var fixture;
39+
var component;
40+
var userService: UserService;
41+
var coursesService: CoursesService;
42+
var categoryService: CategoryService;
43+
44+
beforeEach(async(() => {
45+
TestBed.configureTestingModule({
46+
declarations: [ CoursesComponent ],
47+
imports: [ HttpClientModule, RouterTestingModule, SharedModule ],
48+
providers: [
49+
CategoryService,
50+
AlertService,
51+
CommunicationService,
52+
UserService,
53+
CoursesService,
54+
{
55+
provide: ActivatedRoute,
56+
useValue: {
57+
params: Observable.of({ id: 'me' })
58+
}
59+
}],
60+
})
61+
.compileComponents();
62+
63+
fixture = TestBed.createComponent(CoursesComponent);
64+
component = fixture.componentInstance;
65+
userService = fixture.debugElement.injector.get(UserService);
66+
coursesService = fixture.debugElement.injector.get(CoursesService);
67+
categoryService = fixture.debugElement.injector.get(CategoryService);
68+
}));
69+
70+
it('CoursesComponent: should successfuly be able to create a CoursesComponent', () => {
71+
expect(fixture.componentInstance instanceof CoursesComponent).toBe(true, "should create CoursesComponent");
72+
});
73+
74+
//test ngOnit methods and check its effects by mocking userService method getUserMe
75+
it("CoursesComponent /me: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
76+
//set preconditions
77+
component.activatedRoute.params.value.id = "me";
78+
spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
79+
spyOn(coursesService, "getCoursesByCourseId").and.returnValue(Observable.of(coursesByCourseIdModel));
80+
//call testing method
81+
component.ngOnInit();
82+
//check results
83+
fixture.detectChanges();
84+
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
85+
expect(component.courses[0].KURS_NAME).toBe("Testname");
86+
expect(component.dataIsAvailable).toBe(true);
87+
}));
88+
89+
//test ngOnit methods and check its effects by mocking userService method getUserMe
90+
it("CoursesComponent /:kursId: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
91+
//here a different test bed is needed because the ActivatedRoute is /1111
92+
component.category = "Test Category";
93+
component.activatedRoute.params.value.id = "test-kursId";
94+
//set preconditions
95+
spyOn(categoryService, "getCoursesByCategoryId").and.returnValue(Observable.of([coursesByCourseIdModel]));
96+
//call testing method
97+
component.ngOnInit();
98+
//check results
99+
fixture.detectChanges();
100+
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
101+
expect(component.courses[0].KURS_NAME).toBe("Testname");
102+
expect(component.dataIsAvailable).toBe(true);
103+
}));
104+
105+
//test ngOnit methods and check its effects by mocking userService method getUserMe
106+
it("CoursesComponent /highlights: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
107+
//here a different test bed is needed because the ActivatedRoute is /1111
108+
component.category = "Test Category";
109+
component.activatedRoute.params.value.id = "highlights";
110+
//set preconditions
111+
spyOn(coursesService, "getCoursesByHighlight").and.returnValue(Observable.of([coursesByCourseIdModel]));
112+
//call testing method
113+
component.ngOnInit();
114+
//check results
115+
fixture.detectChanges();
116+
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
117+
expect(component.courses[0].KURS_NAME).toBe("Testname");
118+
expect(component.dataIsAvailable).toBe(true);
119+
}));
120+
121+
122+
});

client/app/components/home/courses/courses.component.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ export class CoursesComponent implements OnInit {
3333
}
3434

3535
ngOnInit(){
36-
this.activatedRoute.params.subscribe((params: Params) => {
36+
this.activatedRoute.params.subscribe((params: Params) => {
3737
if(params.id == "me"){
3838
this.requestCoursesByUser();
3939
}
40-
if(params.id == "highlights"){
41-
this.requestCoursesByHighlights();
42-
}
4340
else{
44-
this.requestCoursesByCategory(params.id);
41+
if(params.id == "highlights"){
42+
this.requestCoursesByHighlights();
43+
}
44+
else{
45+
this.requestCoursesByCategory(params.id);
46+
}
4547
}
4648
});
4749
}
4850

4951
requestCoursesByUser():void{
5052
this.headerText = "Meine";
5153
this.courses = [];
52-
this.userService.getCoursesByUser().subscribe(responseUser =>{
54+
this.userService.getCoursesByUser().subscribe(responseUser =>{
5355
responseUser.forEach(element => {
5456
this.coursesService.getCoursesByCourseId(element.KURS_ID).subscribe(responseCourse => {
5557
responseCourse.ANM_DATUM = element.ANM_DATUM;
@@ -65,7 +67,7 @@ export class CoursesComponent implements OnInit {
6567
requestCoursesByCategory(courseId: string):void{
6668
this.headerText = this.category;
6769
this.courses = [];
68-
this.categoryService.getCoursesByCategoryId(courseId).subscribe(response =>{
70+
this.categoryService.getCoursesByCategoryId(courseId).subscribe(response =>{
6971
this.courses.push(response);
7072
if(response.name != "HttpResponseError"){
7173
this.dataIsAvailable = true;

client/app/components/home/profil/profil.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ProfilComponent', () => {
2828
beforeEach(async(() => {
2929
TestBed.configureTestingModule({
3030
declarations: [ ProfilComponent ],
31-
imports: [ FormsModule, HttpClientModule, RouterTestingModule, SharedModule],
31+
imports: [ FormsModule, HttpClientModule, RouterTestingModule, SharedModule ],
3232
providers: [ UserService, AlertService ],
3333
})
3434
.compileComponents();

0 commit comments

Comments
 (0)