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

58 lines
1.8 KiB
TypeScript
Raw Normal View History

///<reference path="../../../browser.d.ts"/>
import {
it,
inject,
2016-05-04 23:20:21 +08:00
beforeEachProviders
} from '@angular/core/testing';
2016-05-05 02:10:28 +08:00
import {provide} from '@angular/core';
import {UserService} from "./user.service.ts";
import {User} from "../../../../common/entities/User";
import {Message} from "../../../../common/entities/Message";
2016-03-19 17:55:46 +08:00
import "rxjs/Rx";
import {LoginCredential} from "../../../../common/entities/LoginCredential";
2016-05-05 02:10:28 +08:00
import {AuthenticationService} from "./authentication.service";
2016-03-19 17:55:46 +08:00
class MockUserService {
public login(credential:LoginCredential){
2016-05-06 00:05:40 +08:00
return Promise.resolve(new Message<User>(null,new User(0,"testUserName")))
2016-03-19 17:55:46 +08:00
}
}
2016-05-05 03:08:05 +08:00
describe('AuthenticationService', () => {
beforeEachProviders(() => [
2016-03-19 17:55:46 +08:00
provide(UserService, {useClass: MockUserService}),
AuthenticationService
]);
it('should call User service login', inject([ AuthenticationService,UserService ], (authService, userService) => {
2016-03-19 17:55:46 +08:00
spyOn(userService,"login").and.callThrough();
expect(userService.login).not.toHaveBeenCalled();
authService.login();
expect(userService.login).toHaveBeenCalled();
}));
2016-05-05 04:20:54 +08:00
it('should have NO Authenticated use', inject([ AuthenticationService ], (authService) => {
expect(authService.getUser()).toBe(null);
expect(authService.isAuthenticated()).toBe(false);
}));
it('should have Authenticated use', inject([ AuthenticationService ], (authService) => {
spyOn(authService.OnAuthenticated,"trigger").and.callThrough();
authService.login();
2016-05-06 00:05:40 +08:00
expect(authService.OnAuthenticated.trigger).toHaveBeenCalled();
2016-05-05 04:20:54 +08:00
authService.OnAuthenticated.on(() =>{
expect(authService.getUser()).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true);
});
}));
});