Skip to content

Commit

Permalink
Merge pull request #123 from marc101101/feature/testing
Browse files Browse the repository at this point in the history
Feature/testing
  • Loading branch information
hmSchuller authored Sep 18, 2018
2 parents 34acdea + 0f886c6 commit 38222f3
Show file tree
Hide file tree
Showing 36 changed files with 98,309 additions and 2,770 deletions.
65 changes: 65 additions & 0 deletions client/app/components/home/categories/categories.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { async, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { AlertService } from '../../../services/alert.service';
import { RouterTestingModule } from '@angular/router/testing';
import { SharedModule } from '../../../sharedModule/shared.module';
import { Observable, of } from 'rxjs';
import 'rxjs/add/observable/from';
import { CategoryService } from '../../../services/category.service';
import { CommunicationService } from '../shared/communication.service';
import { CategoriesComponent } from './categories.component';
import {Location} from "@angular/common";



/**
* Test should test all four methods of courses.component.ts
* ngOnInit() / routeToCourse()
**/
describe('CategoriesComponent', () => {
let categoriesModel = [{
"KURS_NAME":"Test Kurs",
"KURS_BESCHREIBUNG": "Test Kurs Beschreibung"
}];

var fixture;
var component;
var categoryService: CategoryService;
var location;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CategoriesComponent ],
imports: [ HttpClientModule, RouterTestingModule, SharedModule ],
providers: [ CategoryService, AlertService, CommunicationService, Location],
})
.compileComponents();

fixture = TestBed.createComponent(CategoriesComponent);
component = fixture.componentInstance;
categoryService = fixture.debugElement.injector.get(CategoryService);
location = TestBed.get(Location);
}));

it('CategoriesComponent: should successfuly be able to create a CategoriesComponent', () => {
expect(fixture.componentInstance instanceof CategoriesComponent).toBe(true, "should create CategoriesComponent");
});

//test ngOnit methods and check its effects by mocking userService method getUserMe
it("CategoriesComponent: ngOnit() sets categories and dataIsAvailable values correctly", fakeAsync(() => {
//set preconditions
spyOn(categoryService, "getAllCategories").and.returnValue(Observable.of(categoriesModel));
//call testing method
component.ngOnInit();
//check results
fixture.detectChanges();
expect(component.categories).toBe(categoriesModel);
expect(component.dataIsAvailable).toBe(true);
}));

it('CategoriesComponent: navigate to course(id: 1111) redirects you to /#/home/kurs-uebersicht/1111', fakeAsync(() => {
component.routeToCourse("1111", "primary");
tick(50);
expect(location._platformStrategy.internalPath).toBe('/#/home/kurs-uebersicht/1111');
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CategoriesComponent implements OnInit {
}

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

Expand Down
2 changes: 2 additions & 0 deletions client/app/components/home/contact/contact.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<div style="padding:1rem">
<textarea class="textarea" placeholder="Wir freuen uns über Ihr Feedback." [(ngModel)]="contactMessage.text" ></textarea>
</div>

<div class="colum is-12" style="text-align: center; font-size: 2rem;">
<i *ngIf="contactMessage.bewertung<1" class="fa fa-star-o" aria-hidden="true" (click)="contactMessage.bewertung = 1"></i>
<i *ngIf="contactMessage.bewertung>=1" class="fa fa-star" aria-hidden="true" (click)="contactMessage.bewertung = 1"></i>
Expand All @@ -29,6 +30,7 @@
<i *ngIf="contactMessage.bewertung<5" class="fa fa-star-o" aria-hidden="true" (click)="contactMessage.bewertung = 5"></i>
<i *ngIf="contactMessage.bewertung>=5" class="fa fa-star" aria-hidden="true" (click)="contactMessage.bewertung = 5"></i>
</div>

<div class="column is-12" style="padding: 1rem; padding-top:0rem">
<button #send class="button is-primary is-medium is-fullwidth" style="margin-top:1.5rem" [disabled]="contactMessage.text == ''" (click)="submit()">
<div>{{button_text}}</div>
Expand Down
97 changes: 97 additions & 0 deletions client/app/components/home/contact/contact.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { async, TestBed, fakeAsync } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { AlertService } from '../../../services/alert.service';
import { RouterTestingModule } from '@angular/router/testing';
import { SharedModule } from '../../../sharedModule/shared.module';
import { Observable, of } from 'rxjs';
import 'rxjs/add/observable/from';
import { CommunicationService } from '../shared/communication.service';
import { CoursesService } from '../shared/courses.service';
import { ActivatedRoute } from '@angular/router';
import { ContactService } from '../shared/contact.service';
import { ContactComponent } from './contact.component';
import { FormsModule } from '@angular/forms';


/**
* Test should test all four methods of contact.component.ts
* ngOnit() / submit()
**/
describe('ContactComponent', () => {

let coursesByCourseIdModel = {
"ANM_DATUM": Date.now(),
"KURS_NAME": "Testname",
"KURS_BESCHREIBUNG": "Testbeschreibung"
};

var fixture;
var component;
var coursesService: CoursesService;
var contactService: ContactService;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ContactComponent ],
imports: [ HttpClientModule, RouterTestingModule, SharedModule, FormsModule ],
providers: [
AlertService,
CommunicationService,
ContactService,
CoursesService,
{
provide: ActivatedRoute,
useValue: {
params: Observable.of({ id: 'test-id' })
}
}],
})
.compileComponents();

fixture = TestBed.createComponent(ContactComponent);
component = fixture.componentInstance;
coursesService = fixture.debugElement.injector.get(CoursesService);
contactService = fixture.debugElement.injector.get(ContactService);
}));

it('ContactComponent: should successfuly be able to create a ContactComponent and check ngOninit', () => {
//set preconditions
component.activatedRoute.params.value.id = "test-id";
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
spyOn(coursesService, "getCoursesByCourseId").and.returnValue(Observable.of(coursesByCourseIdModel));
//call testing method
component.ngOnInit();
//check results
fixture.detectChanges();
expect(component.single_course).toBe(coursesByCourseIdModel);
expect(component.headerText).toBe("Bewertung: " + component.single_course.KURS_NAME);
expect(component.course_feedback).toBe(true);
expect(fixture.componentInstance instanceof ContactComponent).toBe(true, "should create ContactComponent");
});

it('ContactComponent: should successfuly submit feedback for course', () => {
//set preconditions
component.course_feedback = true;
component.single_course = {"KURS_ID": 1234};
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
spyOn(coursesService, "postFeedbackByCourse").and.returnValue(Observable.of(coursesByCourseIdModel));
//call testing method
component.submit();
//check results
fixture.detectChanges();
expect(component.button_text).toBe("Senden erfolgreich");
});

it('ContactComponent: should successfuly submit feedback for app in general', () => {
//set preconditions
component.course_feedback = false;
//spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
spyOn(contactService, "postContactFeedback").and.returnValue(Observable.of(coursesByCourseIdModel));
//call testing method
component.submit();
//check results
fixture.detectChanges();
expect(component.button_text).toBe("Senden erfolgreich");
});

});
7 changes: 2 additions & 5 deletions client/app/components/home/contact/contact.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, OnInit, ViewChild, Renderer2, ElementRef } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { ActivatedRoute, Params } from '@angular/router';
import { ContactService } from '../shared/contact.service';
import { log } from 'util';
import { CoursesService } from '../shared/courses.service';

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

constructor(
private router: Router,
private renderer: Renderer2,
private contactService: ContactService,
private activatedRoute: ActivatedRoute,
private coursesService:CoursesService) { }

ngOnInit(){
this.activatedRoute.params.subscribe((params: Params) => {
this.activatedRoute.params.subscribe((params: Params) => {
if(params.id){
this.coursesService.getCoursesByCourseId(params.id).subscribe(response =>{
this.single_course = response;
Expand All @@ -44,7 +42,6 @@ export class ContactComponent implements OnInit{
submit():void{
if(this.course_feedback){
this.coursesService.postFeedbackByCourse(this.contactMessage, this.single_course.KURS_ID).subscribe(response => {
console.log(response);
if(response.name != "HttpResponseError"){
this.renderer.addClass(this.sendButton.nativeElement, 'is-primary-save');
this.button_text = 'Senden erfolgreich';
Expand Down
122 changes: 122 additions & 0 deletions client/app/components/home/courses/courses.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { async, TestBed, fakeAsync } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { AlertService } from '../../../services/alert.service';
import { RouterTestingModule } from '@angular/router/testing';
import { SharedModule } from '../../../sharedModule/shared.module';
import { Observable, of } from 'rxjs';
import 'rxjs/add/observable/from';
import { CoursesComponent } from './courses.component';
import { CommunicationService } from '../shared/communication.service';
import { UserService } from '../../../services/user.service';
import { CoursesService } from '../shared/courses.service';
import { CategoryService } from '../../../services/category.service';
import { ActivatedRoute } from '@angular/router';


/**
* Test should test all four methods of courses.component.ts
* requestCoursesByUser() / requestCoursesByCategory()
**/
describe('CoursesComponent', () => {
let userCoursesModel = [
{
"ANM_KURS_ID": 1,
"ANM_DATUM": Date.now()
},
{
"ANM_KURS_ID": 2,
"ANM_DATUM": Date.now()
}
];

let coursesByCourseIdModel = {
"ANM_DATUM": Date.now(),
"KURS_NAME": "Testname",
"KURS_BESCHREIBUNG": "Testbeschreibung"
};

var fixture;
var component;
var userService: UserService;
var coursesService: CoursesService;
var categoryService: CategoryService;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CoursesComponent ],
imports: [ HttpClientModule, RouterTestingModule, SharedModule ],
providers: [
CategoryService,
AlertService,
CommunicationService,
UserService,
CoursesService,
{
provide: ActivatedRoute,
useValue: {
params: Observable.of({ id: 'me' })
}
}],
})
.compileComponents();

fixture = TestBed.createComponent(CoursesComponent);
component = fixture.componentInstance;
userService = fixture.debugElement.injector.get(UserService);
coursesService = fixture.debugElement.injector.get(CoursesService);
categoryService = fixture.debugElement.injector.get(CategoryService);
}));

it('CoursesComponent: should successfuly be able to create a CoursesComponent', () => {
expect(fixture.componentInstance instanceof CoursesComponent).toBe(true, "should create CoursesComponent");
});

//test ngOnit methods and check its effects by mocking userService method getUserMe
it("CoursesComponent /me: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
//set preconditions
component.activatedRoute.params.value.id = "me";
spyOn(userService, "getCoursesByUser").and.returnValue(Observable.of(userCoursesModel));
spyOn(coursesService, "getCoursesByCourseId").and.returnValue(Observable.of(coursesByCourseIdModel));
//call testing method
component.ngOnInit();
//check results
fixture.detectChanges();
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
expect(component.courses[0].KURS_NAME).toBe("Testname");
expect(component.dataIsAvailable).toBe(true);
}));

//test ngOnit methods and check its effects by mocking userService method getUserMe
it("CoursesComponent /:kursId: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
//here a different test bed is needed because the ActivatedRoute is /1111
component.category = "Test Category";
component.activatedRoute.params.value.id = "test-kursId";
//set preconditions
spyOn(categoryService, "getCoursesByCategoryId").and.returnValue(Observable.of([coursesByCourseIdModel]));
//call testing method
component.ngOnInit();
//check results
fixture.detectChanges();
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
expect(component.courses[0].KURS_NAME).toBe("Testname");
expect(component.dataIsAvailable).toBe(true);
}));

//test ngOnit methods and check its effects by mocking userService method getUserMe
it("CoursesComponent /highlights: ngOnit() sets courses and dataIsAvailable values correctly", fakeAsync(() => {
//here a different test bed is needed because the ActivatedRoute is /1111
component.category = "Test Category";
component.activatedRoute.params.value.id = "highlights";
//set preconditions
spyOn(coursesService, "getCoursesByHighlight").and.returnValue(Observable.of([coursesByCourseIdModel]));
//call testing method
component.ngOnInit();
//check results
fixture.detectChanges();
expect(component.courses[0].KURS_BESCHREIBUNG).toBe("Testbeschreibung");
expect(component.courses[0].KURS_NAME).toBe("Testname");
expect(component.dataIsAvailable).toBe(true);
}));


});
16 changes: 9 additions & 7 deletions client/app/components/home/courses/courses.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ export class CoursesComponent implements OnInit {
}

ngOnInit(){
this.activatedRoute.params.subscribe((params: Params) => {
this.activatedRoute.params.subscribe((params: Params) => {
if(params.id == "me"){
this.requestCoursesByUser();
}
if(params.id == "highlights"){
this.requestCoursesByHighlights();
}
else{
this.requestCoursesByCategory(params.id);
if(params.id == "highlights"){
this.requestCoursesByHighlights();
}
else{
this.requestCoursesByCategory(params.id);
}
}
});
}

requestCoursesByUser():void{
this.headerText = "Meine";
this.courses = [];
this.userService.getCoursesByUser().subscribe(responseUser =>{
this.userService.getCoursesByUser().subscribe(responseUser =>{
responseUser.forEach(element => {
this.coursesService.getCoursesByCourseId(element.KURS_ID).subscribe(responseCourse => {
responseCourse.ANM_DATUM = element.ANM_DATUM;
Expand All @@ -65,7 +67,7 @@ export class CoursesComponent implements OnInit {
requestCoursesByCategory(courseId: string):void{
this.headerText = this.category;
this.courses = [];
this.categoryService.getCoursesByCategoryId(courseId).subscribe(response =>{
this.categoryService.getCoursesByCategoryId(courseId).subscribe(response =>{
this.courses.push(response);
if(response.name != "HttpResponseError"){
this.dataIsAvailable = true;
Expand Down
2 changes: 1 addition & 1 deletion client/app/components/home/profil/profil.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('ProfilComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProfilComponent ],
imports: [ FormsModule, HttpClientModule, RouterTestingModule, SharedModule],
imports: [ FormsModule, HttpClientModule, RouterTestingModule, SharedModule ],
providers: [ UserService, AlertService ],
})
.compileComponents();
Expand Down
Loading

0 comments on commit 38222f3

Please sign in to comment.