Ad

How To Receive The Login Status In Login Unit Test Using Jasmine?

This is my login function:

  login(){
 let response =  this.api.apiPost('auth/login', this.userData)
.then(data => {
  console.log(data);
  let parsed = JSON.parse(data.toString());
  console.log(parsed);
  if(parsed.status == 1){
    this.token = parsed.token;
    this.api.setToken(parsed.token);
    let input = {"term": ""};
    input.term = this.userData.username;
    this.retrieveUserInfo();
    this.navCtrl.push(HomePage,{token:this.api.getToken(),appName:this.appName,quotes:this.quotes,picsURL:this.picsURL,uID:this.userData.username,test:this.params.test,code:this.params.code });
    var status = true;
    return status;
  } else if (parsed.status == 0){
    var status = false;
    return status;
  } else {}
});

}

it('test login', () => {
 var status = component.login();
 expect(status).toBe(true);
});

The problem is that having a return statement inside the then block in the login function doesn't return from the login function. The login function itself doesn't return anything (but returns true or false to the variable 'response') and so the unit test fails as undefined is expected to be true.

I don't have much experience with either typescript or jasmine. Any help will be appreciated.

Ad

Answer

Simply call .then on your method in the test as well:

it('test login', (done) => {
  component.login().then((result) => {
    expect(result).toBe(true);
    done();
  });
});

Also, don't forget to include done

Ad
source: stackoverflow.com
Ad