1- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { async , TestBed , fakeAsync } from '@angular/core/testing' ;
22import { FormsModule } from '@angular/forms' ;
33import { HttpClientModule } from '@angular/common/http' ;
44import { AlertService } from '../../../services/alert.service' ;
5- import { AuthService } from '../../../services/auth.service' ;
65import { RouterTestingModule } from '@angular/router/testing' ;
76import { ProfilComponent } from './profil.component' ;
8- import { UserService } from '../../../services/user.service' ;
97import { SharedModule } from '../../../sharedModule/shared.module' ;
8+ import { UserService } from '../../../services/user.service' ;
9+ import { Observable , of } from 'rxjs' ;
10+ import { By } from "@angular/platform-browser" ;
11+ import 'rxjs/add/observable/from' ;
1012
13+
14+ /**
15+ * Test should test all four methods of profil.component.ts
16+ * ngOnInit() / onSubmit() / resetButton()
17+ **/
1118describe ( 'ProfilComponent' , ( ) => {
12- let component : ProfilComponent ;
13- let fixture : ComponentFixture < ProfilComponent > ;
19+ let userModel = {
20+ "TEIL_EMAIL" :"test@test.de" ,
21+ "TEIL_ORT" : "Regensburg" ,
22+ "TEIL_BLZ" : "1010101" ,
23+ "TEIL_IBAN" : "1010101"
24+ } ;
25+
26+ var fixture ;
27+ var component ;
28+ var userService : UserService ;
1429
1530 beforeEach ( async ( ( ) => {
1631 TestBed . configureTestingModule ( {
@@ -19,16 +34,56 @@ describe('ProfilComponent', () => {
1934 providers : [ UserService , AlertService ] ,
2035 } )
2136 . compileComponents ( ) ;
22- } ) ) ;
2337
24- beforeEach ( ( ) => {
2538 fixture = TestBed . createComponent ( ProfilComponent ) ;
2639 component = fixture . componentInstance ;
40+ userService = fixture . debugElement . injector . get ( UserService ) ;
41+ } ) ) ;
42+
43+ it ( 'ProfilComponent: should successfuly be able to create a ProfilComponent' , ( ) => {
44+ expect ( fixture . componentInstance instanceof ProfilComponent ) . toBe ( true , "should create ProfilComponent" ) ;
45+ } ) ;
46+
47+ //test ngOnit methods and check its effects by mocking userService method getUserMe
48+ it ( "ProfilComponent: ngOnit() sets user and dataIsAvailable values correctly" , fakeAsync ( ( ) => {
49+ //set preconditions
50+ spyOn ( userService , "getUserMe" ) . and . returnValue ( Observable . of ( userModel ) ) ;
51+ //call testing method
52+ component . ngOnInit ( ) ;
53+ //check results
54+ fixture . detectChanges ( ) ;
55+ expect ( component . user ) . toBe ( userModel ) ;
56+ expect ( component . dataIsAvailable ) . toBe ( true ) ;
57+ } ) ) ;
58+
59+ //test onSubmit methods and check its effects by mocking userService method updateUserMe
60+ it ( "ProfilComponent: onSubmit() change button styles and set button_text" , ( ) => {
61+ //set preconditions
62+ spyOn ( userService , "updateUserMe" ) . and . returnValue ( Observable . of ( userModel ) ) ;
63+ component . dataIsAvailable = true ;
64+ component . user = userModel ;
65+ //call testing method
66+ component . onSubmit ( ) ;
67+ //check results
2768 fixture . detectChanges ( ) ;
69+ let debugUlElement = fixture . debugElement . query ( By . css ( 'button' ) ) ;
70+ expect ( debugUlElement . classes [ "is-primary-save" ] ) . toBe ( true ) ;
71+ expect ( component . button_text ) . toBe ( "Speichern erfolgreich" ) ;
2872 } ) ;
2973
30- it ( 'should create' , ( ) => {
31- expect ( component ) . toBeTruthy ( ) ;
74+ //test onSubmit methods and check its effects by mocking userService method updateUserMe
75+ it ( "ProfilComponent: resetButton() change button styles and set button_text" , ( ) => {
76+ //set preconditions
77+ component . button_text = "Speichern erfolgreich" ;
78+ component . dataIsAvailable = true ;
79+ component . user = userModel ;
80+ //call testing method
81+ component . resetButton ( ) ;
82+ //check results
83+ fixture . detectChanges ( ) ;
84+ let debugUlElement = fixture . debugElement . query ( By . css ( 'button' ) ) ;
85+ expect ( debugUlElement . classes [ "is-primary-save" ] ) . toBe ( false ) ;
86+ expect ( component . button_text ) . toBe ( "Speichern" ) ;
3287 } ) ;
3388
3489} ) ;
0 commit comments