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
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad