1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-16 11:37:13 +02:00
pigallery2/frontend/app/model/network/autehentication.service.spec.ts
2016-05-05 18:14:11 +02:00

58 lines
1.8 KiB
TypeScript

///<reference path="../../../browser.d.ts"/>
import {
it,
inject,
beforeEachProviders
} from '@angular/core/testing';
import {provide} from '@angular/core';
import {UserService} from "./user.service.ts";
import {User} from "../../../../common/entities/User";
import {Message} from "../../../../common/entities/Message";
import "rxjs/Rx";
import {LoginCredential} from "../../../../common/entities/LoginCredential";
import {AuthenticationService} from "./authentication.service";
class MockUserService {
public login(credential:LoginCredential){
return Promise.resolve(new Message<User>(null,new User(0,"testUserName")))
}
}
describe('AuthenticationService', () => {
beforeEachProviders(() => [
provide(UserService, {useClass: MockUserService}),
AuthenticationService
]);
it('should call User 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) => {
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();
authService.OnAuthenticated.on(() =>{
expect(authService.OnAuthenticated.trigger).toHaveBeenCalled();
expect(authService.getUser()).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true);
});
}));
});