I am facing an issue in my component where I have a function that calls another asynchronous function in a service. Unfortunately, testing what happens inside the subscription is posing a challenge for me. Can anyone provide guidance on how to test this?
I specifically want to verify if the function this.auth.sendPasswordResetEmail(data.email) is being called.
login.component.ts
async forgotPassword() {
const {
["modules.organizations.sign_in.forgot_password.alert.title"]: title,
["modules.organizations.sign_in.forgot_password.alert.message"]: message,
["modules.organizations.sign_in.forgot_password.alert.message.success"]: success,
} = await this.translate.get([
"modules.organizations.sign_in.forgot_password.alert.title",
"modules.organizations.sign_in.forgot_password.alert.message",
"modules.organizations.sign_in.forgot_password.alert.message.success"
]).pipe(first()).toPromise()
this.alert.input(title, message).afterClosed()
.subscribe(async data => {
if(!data.ok) return
if(data.input == null) return
const loading = this.alert.loading()
try {
await this.auth.sendPasswordResetEmail(data.email)
loading.close()
this.alert.error(title, success)
} catch (error) {
loading.close()
this.alert.error(null, error)
}
})
}
In this segment, I tried to troubleshoot the problem without much success.
login.component.spec.ts
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let alertServiceMock: jasmine.SpyObj<any>;
let authServiceMock: jasmine.SpyObj<any>;
let dialogRefSpy: jasmine.SpyObj<any>;
let router: Router;
dialogRefSpy = jasmine.createSpy();
dialogRefSpy.component = {title: 'error', message: 'error'};
dialogRefSpy.afterClosed = () => of(true);
const matDialogSpy = jasmine.createSpyObj('MatDialog', [
'open',
'close',
]);
matDialogSpy.open.and.returnValue(dialogRefSpy);
matDialogSpy.close.and.returnValue(dialogRefSpy);
beforeEach(waitForAsync(() => {
authServiceMock = jasmine.createSpyObj('AuthService',[
'createUserWithEmailAndPassword',
'updateProfile',
'signInWithEmailAndPassword',
'signInWithPopup',
'sendPasswordResetEmail'
]);
authServiceMock.createUserWithEmailAndPassword.and.returnValue(true);
authServiceMock.updateProfile.and.returnValue(true);
authServiceMock.signInWithEmailAndPassword.and.returnValue(true);
authServiceMock.signInWithPopup.and.returnValue(true);
authServiceMock.sendPasswordResetEmail.and.returnValue(true);
alertServiceMock = jasmine.createSpyObj('AlertService',[
'message',
'error',
'input',
'password',
'loading',
'confirmation'
]);
alertServiceMock.error.and.returnValue(matDialogSpy.open(AlertComponent, {
data: {
type: 'error',
title: 'error',
error: 'description'
},
disableClose: true
}));
alertServiceMock.input.and.returnValue(matDialogSpy.open(AlertComponent, {
data: {
type: 'input',
title: 'title',
description: 'description'
},
disableClose: true
}));
alertServiceMock.loading.and.returnValue(matDialogSpy);
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule,
OverlayModule,
RouterTestingModule,
MatDialogModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateFakeLoader
}
})
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
declarations: [ LoginComponent ],
providers: [
{ provide: AlertService, useValue: alertServiceMock },
{ provide: AuthService, useValue: authServiceMock },
{ provide: MatDialog, useValue: matDialogSpy },
{ provide: ActivatedRoute, useFactory: () => mockActiveRoute },
MatSnackBar,
MatProgressBarModule,
FirestoreService,
FunctionsService,
TranslateService,
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
fireStoreService = TestBed.inject(FirestoreService);
alertServiceMock = TestBed.inject(AlertService);
authServiceMock = TestBed.inject(AuthService);
functionsServiceMock = TestBed.inject(FunctionsService);
router = TestBed.inject(Router);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {
localStorage.removeItem('token');
fixture.destroy();
});
describe('tests on forgot password', () => {
it('should generate a FORGOT PASSWORD ALERT to enter an EMAIL', async () => {
await component.forgotPassword();
expect(authServiceMock.sendPasswordResetEmail).toHaveBeenCalled();
});
});
});