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

68 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {inject, TestBed} from '@angular/core/testing';
import {UserService} from './user.service';
import {UserDTO} from '../../../../common/entities/UserDTO';
import {LoginCredential} from '../../../../common/entities/LoginCredential';
import {AuthenticationService} from './authentication.service';
import {NetworkService} from './network.service';
import {ErrorDTO} from '../../../../common/entities/Error';
2016-03-19 17:55:46 +08:00
class MockUserService {
2017-07-04 01:39:09 +08:00
public login(credential: LoginCredential): Promise<UserDTO> {
2018-03-31 03:30:30 +08:00
return Promise.resolve(<UserDTO>{name: 'testUserName'});
}
public async getSessionUser() {
return null;
}
2016-03-19 17:55:46 +08:00
}
2017-07-19 14:00:51 +08:00
class MockNetworkService {
addGlobalErrorHandler(fn: (error: ErrorDTO) => boolean) {
}
}
2016-05-05 03:08:05 +08:00
describe('AuthenticationService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
2017-07-19 14:00:51 +08:00
{provide: NetworkService, useClass: MockNetworkService},
{provide: UserService, useClass: MockUserService},
2017-07-19 14:00:51 +08:00
AuthenticationService,
]
2016-12-27 06:36:38 +08:00
});
});
2018-05-23 08:27:07 +08:00
it('should call UserDTO service login', inject([AuthenticationService, UserService],
async (authService, userService) => {
spyOn(userService, 'login').and.callThrough();
2016-05-09 23:04:56 +08:00
2018-05-23 08:27:07 +08:00
expect(userService.login).not.toHaveBeenCalled();
await authService.login();
expect(userService.login).toHaveBeenCalled();
}));
2018-05-23 08:27:07 +08:00
it('should have NO Authenticated use', inject([AuthenticationService],
(authService: AuthenticationService) => {
expect(authService.user.value).toBe(null);
expect(authService.isAuthenticated()).toBe(false);
}));
2016-05-05 04:20:54 +08:00
2018-05-23 08:27:07 +08:00
it('should have Authenticated use', (done) => 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);
done();
});
authService.login(<any>{});
})());
});