mercredi 3 janvier 2018

Testing simple angular component only with ngOnInit function

I'm a beginner when it comes to testing and I hope that you will explain me what is a good practice.

I have a simple service:

export class SessionService {
fetchFromStorage() {
    let x = localStorage.getItem('email');
    return JSON.parse(x);
}

saveInStorage(email) {
    localStorage.setItem('email', JSON.stringify(email));
}
}

and component which use that service:

export class LoginComponent implements OnInit, OnChanges {
email;

constructor(private service: SessionService) {
}

ngOnInit() {
    this.email = this.service.fetchFromStorage();
}

save() {
    this.service.saveInStorage(this.email);
}
}

I know that I should create localStorageMock ? And for example after calling saveInStorage() I have to check if that localStorageMock contains parameter that I passed to that function? I am not sure how to test fetchFromStorage, should I create in localStorageMock sth like this:

export class {
getItem(x){
if(x = 'email') return 'sth';
}
}

And third question what abou ngOnInit in my component? Isn't it to simple to write a test?

Aucun commentaire:

Enregistrer un commentaire