I've created a service to getting user information from django rest auth. So I need 2 separate requests. one for getting auth token and one for getting user info.
In the userService service, I have a method called login that calls 2 other methods. Each of them sends a http request to a different url. For testing the behavior of login, I need to mock the requests of that 2 methods. First method returns a Promise that includes authentication key, and second method returns a Promise that includes user object. Here is my code in the service class:
public getAuthToken(identifier: string, password: string) {
const requestBody = is_valid_email(identifier) ? {email: identifier, password: password} :
{username: identifier, password: password};
let savedToken = getFromStorage('auth');
if (savedToken) {
try {
savedToken = JSON.parse(savedToken);
} catch (e) {
savedToken = null;
}
}
return new Promise((resolve, reject) => {
if (savedToken) {
resolve(savedToken);
} else {
this.http.post<string>(APIUrlSolver.login, requestBody).subscribe(data => {
const dataObj = JSON.parse(data);
UserService._auth_token = dataObj['key'];
resolve(dataObj['key']);
}, error1 => {
// Rejection code. removed for better reading
});
}
});
}
public getUserProfile(): Promise<UserModel> {
return new Promise<UserModel>((resolve, reject) => {
this.http.get(APIUrlSolver.user).subscribe((data: string) => {
const jsonData = JSON.parse(data);
const userObj = new UserModel(jsonData.username, jsonData.email, jsonData.first_name, jsonData.last_name, jsonData.phone,
jsonData.birth_date);
UserService._user = userObj;
resolve(userObj);
}, error1 => {
// Rejection code. removed for better reading
});
});
}
public login(identifier: string, password: string) {
return new Promise((resolve, reject) => {
this.getAuthToken(identifier, password).then(key => {
this.getUserProfile().then(user => {
// Will resolve user object
}).catch(error => {
UserService._auth_token = undefined;
reject(error);
});
}).catch(reason => {
UserService._auth_token = undefined;
reject(reason);
});
});
}
I've tried to test this method with following code:
describe('userService', () => {
let userService: UserService;
let httpClient: HttpTestingController;
const mockedUser = new UserModel();
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [UserService]
});
userService = TestBed.get(UserService);
httpClient = TestBed.get(HttpTestingController);
});
afterEach(() => {
httpClient.verify();
});
it('#login', () => {
const authResponse = {key: '74f0d5ffb992f5f49533d25c686f36414e64482c'};
const response = {username: 'daaaaaaab', email: 'test@test.ir', first_name: 'test', last_name: 'test', phone: '09123657894',
birth_date: '2018-07-31'};
const expectedUser = new UserModel(response.username, response.email, response.first_name, response.last_name, response.phone,
response.birth_date);
userService.login('identifier', 'password').then(user => {
expect(user).toEqual(expectedUser);
expect(userService.user).toEqual(expectedUser);
});
const req = httpClient.expectOne(APIUrlSolver.login); // This response works correct
expect(req.request.method).toBe('POST');
req.flush(JSON.stringify(authResponse));
const userReq = httpClient.expectOne(APIUrlSolver.user); // I get error here
expect(req.request.method).toBe('GET');
userReq.flush(JSON.stringify(response));
});
});
But this code will always faild on userReq. because expectOne raises:
Error: Expected one matching request for criteria "Match URL: /user/user/", found none.
The real question is how I can test this sequence of http requests Because HttpClientTestingModule did not work
from Angular6 how to test a sequence of http requests
No comments:
Post a Comment