1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/model/network/autehentication.service.spec.ts
2018-03-30 15:30:30 -04:00

67 lines
2.0 KiB
TypeScript

import {inject, TestBed} from '@angular/core/testing';
import {UserService} from './user.service';
import {UserDTO} from '../../../../common/entities/UserDTO';
import 'rxjs/Rx';
import {LoginCredential} from '../../../../common/entities/LoginCredential';
import {AuthenticationService} from './authentication.service';
import {NetworkService} from './network.service';
import {ErrorDTO} from '../../../../common/entities/Error';
class MockUserService {
public login(credential: LoginCredential): Promise<UserDTO> {
return Promise.resolve(<UserDTO>{name: 'testUserName'});
}
public async getSessionUser() {
return null;
}
}
class MockNetworkService {
addGlobalErrorHandler(fn: (error: ErrorDTO) => boolean) {
}
}
describe('AuthenticationService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: NetworkService, useClass: MockNetworkService},
{provide: UserService, useClass: MockUserService},
AuthenticationService,
]
});
});
it('should call UserDTO service login', inject([AuthenticationService, UserService], (authService, userService) => {
spyOn(userService, 'login').and.callThrough();
expect(userService.login).not.toHaveBeenCalled();
authService.login();
expect(userService.login).toHaveBeenCalled();
}));
it('should have NO Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
expect(authService.user.value).toBe(null);
expect(authService.isAuthenticated()).toBe(false);
}));
it('should have Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
spyOn(authService.user, 'next').and.callThrough();
authService.user.subscribe((user) => {
if (user == null) {
return;
}
expect(authService.user.next).toHaveBeenCalled();
expect(authService.user.value).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true);
});
authService.login(<any>{});
}));
});