I am new to unit testing and just decided to add test coverage for my existing app. I am struggling in writing the unit test cases for the given service . I have created a AWS service file .
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Router } from '@angular/router';
import { Auth } from 'aws-amplify';
import { IUser } from '../../models/userModel';
@Injectable({
providedIn: 'root'
})
export class AwsAmplifyAuthService {
private authenticationSubject: BehaviorSubject<any>;
constructor(private router: Router) {
this.authenticationSubject = new BehaviorSubject<boolean>(false);
}
// SignUp
public signUp(user: IUser): Promise<any> {
return Auth.signUp({
username: user.name,
password: user.password,
attributes: {
email: user.email,
name:user.name
}
});
}
// Confirm Code
public confirmSignUp(user: IUser): Promise<any> {
return Auth.confirmSignUp(user.name, user.code);
}
// SignIn
public signIn(user: IUser): Promise<any> {
return Auth.signIn(user.name, user.password)
.then((r) => {
console.log('signIn response', r);
this.authenticationSubject.next(true);
if (user) {
if (r.challengeName === 'NEW_PASSWORD_REQUIRED') {
this.router.navigateByUrl('/forgotpass');
} else {
this.router.navigateByUrl('/home');
}
}
});
}
and created a interface model
export interface IUser {
email: string;
password: string;
code: string;
name: string;
}
I have tired the mocking the service in the .spec.
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
{
provide: Auth,
useValue: { currentUserInfo: () => Promise.resolve('hello') },
},
]
})
.compileComponents();
service = TestBed.inject(AwsAmplifyAuthService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('#signIn should return expected data', async (done: any) => {
await service.signIn(expectedData).then(data => {
expect(data).toEqual(expectedData);
done();
});
});
I am not able to pass the test case. Any guidance on the same would be appreciated.