1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

implementing userService test

This commit is contained in:
Braun Patrik 2016-05-04 22:37:26 +02:00
parent a65e9eade6
commit ffb720757d
2 changed files with 54 additions and 5 deletions

View File

@ -0,0 +1,50 @@
///<reference path="../../../browser.d.ts"/>
import {it, inject, beforeEachProviders, beforeEach, afterEach} from "@angular/core/testing";
import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http";
import {MockBackend, MockConnection} from "@angular/http/testing";
import {provide} from "@angular/core";
import "rxjs/Rx";
import {NetworkService} from "./network.service";
import {Message} from "../../../../common/entities/Message";
import {UserService} from "./user.service";
import {LoginComponent} from "../../login/login.component";
import {LoginCredential} from "../../../../common/entities/LoginCredential";
describe('UserService', () => {
beforeEachProviders(() => [
MockBackend,
BaseRequestOptions,
provide(Http, {
useFactory: (backend, options) => {
return new Http(backend, options);
}, deps: [MockBackend, BaseRequestOptions]
}),
NetworkService,
UserService
]);
it('should call postJson at login', inject([UserService,NetworkService], (userService,networkService) => {
spyOn(networkService,"postJson");
let credential = new LoginCredential("name","pass");
userService.login(credential);
expect(networkService.postJson.calls.argsFor(0)).toEqual(["/user/login",{"loginCredential": credential}]);
}));
it('should call getJson at getSessionUser', inject([UserService,NetworkService], (userService,networkService) => {
spyOn(networkService,"getJson");
let credential = new LoginCredential("name","pass");
userService.login(credential);
}));
});

View File

@ -8,21 +8,20 @@ import {User} from "../../../../common/entities/User";
import {Message} from "../../../../common/entities/Message"; import {Message} from "../../../../common/entities/Message";
@Injectable() @Injectable()
export class UserService extends NetworkService{ export class UserService{
constructor(_http:Http){ constructor(private _networkService:NetworkService){
super(_http);
} }
public login(credential:LoginCredential): Promise<Message<User>>{ public login(credential:LoginCredential): Promise<Message<User>>{
return this.postJson("/user/login",{"loginCredential": credential}); return this._networkService.postJson("/user/login",{"loginCredential": credential});
} }
public getSessionUser(): Promise<Message<User>>{ public getSessionUser(): Promise<Message<User>>{
return this.getJson("/user/login"); return this._networkService.getJson("/user/login");
} }
} }