I want to test the value of a variable inside a function which changes multiple time inside that function. However, whenever I expect a value of that variable, it is the value of the latest assignment. Is there a way to check what the first value is?
Here is my ts code:
public triggerLogin() {
this.loading = true;
this.requestsService.login(this.userEmail, this.password)
.subscribe( response => this.handleLogin(response))
}
public handleLogin(response) {
if (_.isEqual(response, 'invalid')) {
this.invalid = true;
} else {
this.invalid = false;
this.tokenManager.store(response);
}
this.loading = false;
}
that's my test so far (which is failing: Expected false to be truthy):
it('should start loading as soon as login is triggered', fakeAsync(() => {
spyOn(mockRequestsService, 'login').and.returnValue(Observable.of(token));
component.triggerLogin();
fixture.detectChanges();
expect(component.loading).toBeTruthy();
expect(mockRequestsService.login).toHaveBeenCalled();
}));
As you can see the variable loading is first set to true but then afterwards in the response of the requestsService its set to false. That's why the test is expecting the value to be false. However, i want to test the first assignment for that variable.