|
| 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 | +}); |
0 commit comments