Could you please guide me on resolving this particular issue?
login.component.spec.ts
const mockModel = {
url: 'login',
model: {
handle: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aeeffe9eedaeeffe9eeb4f9f5f7">[email protected]</a>',
password: '123456789'
}
}
export class HttpCommonServiceMock {
public post(url: any, model: any): any { }
}
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule, CoreModule, HttpModule],
declarations: [],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: HttpCommonService, useClass: HttpCommonServiceMock },
]
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
component.ngOnInit();
});
it('should create component', () => {
expect(component instanceof LoginComponent).toBe(true);//This is working fine
});
it('successfully logging in', fakeAsync(() => {//This is NOT working
let httpCommonService = fixture.debugElement.injector.get(HttpCommonService);
let httpCommonResponse = {
isValid: true
};
spyOn(httpCommonService,'post').and.returnValue(of(httpCommonResponse));
component.login(mockModel.model);
tick();
expect(httpCommonResponse).toBe(true);
}));
});
login.component.ts
@Component({
selector: "login",
templateUrl: "./login.component.html"
})
export class LoginComponent implements OnInit {
form: FormGroup;
message: string;
constructor(private fb: FormBuilder,
private router: Router,
private notification: SiteNotificationService,
private session: UserSessionService,
private httpCommonService: HttpCommonService,
private getStaticComponent: GetStaticComponent
) { }
ngOnInit() {
if (!isDevMode()) Mixpanel.trackEvent("View Screen", { "Screen Name": "Login" });
if (this.session.isActive()) {
this.router.navigate(["my-invites"], { replaceUrl: true });
}
this.createForm();
}
//create Form
createForm() {
this.form = this.fb.group({
handle: ["", Validators.compose([Validators.required, Validators.pattern(RegexValidators.email)])],
password: ["", Validators.required]
});
}
//login
login(model) {
this.httpCommonService.post("login", model).subscribe(
response => {
const tokenData = { user: model.handle, token: response.token };
this.session.store("Session", tokenData);
this.getStaticComponent.getStaticContent();
this.httpCommonService.get("mydownline").subscribe(response => {
localStorage.setItem("mydownline", JSON.stringify(response));
});
Mixpanel.trackEvent("Login");
Mixpanel.identifyPerson(response.token);
if (!response.isLocationSet || (response.profilePicUrl && response.profilePicUrl.slice(0, 6) === "assets")) {
this.router.navigate(["accounts"], { replaceUrl: true });
this.notification.showInfo("Please upload your profile pic and add your location.");
} else if (!response.isLocationSet) {
this.router.navigate(["accounts"], { replaceUrl: true });
this.notification.showInfo("Please add your location");
} else if (!response.profilePicUrl || response.profilePicUrl.slice(0, 6) === "assets") {
this.router.navigate(["accounts"], { replaceUrl: true });
this.notification.showInfo("Please upload your profile pic");
} else {
let redirectPage = localStorage.getItem("redirectPage");
if (!redirectPage) redirectPage = "my-invites";
this.router.navigate([redirectPage], { replaceUrl: true });
}
},
err => {
if (err.error) {
this.notification.showError(err.error.reason);
}
this.createForm();
}
);
}
}
httpcommon.service.ts
@Injectable()
export class HttpCommonService {
appUrl: string;
options: any;
headers;
requestOptions;
constructor(
private commonServiceProvider: CommonServiceProvider,
private http: HttpClient) {
this.appUrl = environment.baseApiUrl;
}
//post
post(url: any, model: any): Observable<any> {
return this.http.post(this.appUrl + url, model);
}
}
Error:
Error: Cannot make XHRs from within a fake async test. Request URL: https://mlj0xk2naws.com/latest/static/videos
at FakeAsyncTestZoneSpec.push../node_modules/zone.js/dist/fake-async-test.js.FakeAsyncTestZoneSpec.onScheduleTask...