1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

fixing tests

This commit is contained in:
Braun Patrik 2017-07-03 19:39:09 +02:00
parent eaa3c15825
commit d07fa645c4
2 changed files with 25 additions and 23 deletions

View File

@ -1,14 +1,13 @@
import {inject, TestBed} from "@angular/core/testing"; import {inject, TestBed} from "@angular/core/testing";
import {UserService} from "./user.service"; import {UserService} from "./user.service";
import {UserDTO} from "../../../../common/entities/UserDTO"; import {UserDTO} from "../../../../common/entities/UserDTO";
import {Message} from "../../../../common/entities/Message";
import "rxjs/Rx"; import "rxjs/Rx";
import {LoginCredential} from "../../../../common/entities/LoginCredential"; import {LoginCredential} from "../../../../common/entities/LoginCredential";
import {AuthenticationService} from "./authentication.service"; import {AuthenticationService} from "./authentication.service";
class MockUserService { class MockUserService {
public login(credential: LoginCredential) { public login(credential: LoginCredential): Promise<UserDTO> {
return Promise.resolve(new Message<UserDTO>(null, <UserDTO>{name: "testUserName"})) return Promise.resolve(<UserDTO>{name: "testUserName"})
} }
public async getSessionUser() { public async getSessionUser() {
@ -34,20 +33,23 @@ describe('AuthenticationService', () => {
expect(userService.login).toHaveBeenCalled(); expect(userService.login).toHaveBeenCalled();
})); }));
it('should have NO Authenticated use', inject([AuthenticationService], (authService) => { it('should have NO Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
expect(authService.getUser()).toBe(null); expect(authService.user.value).toBe(null);
expect(authService.isAuthenticated()).toBe(false); expect(authService.isAuthenticated()).toBe(false);
})); }));
it('should have Authenticated use', inject([AuthenticationService], (authService) => { it('should have Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
spyOn(authService.OnUserChanged, "trigger").and.callThrough(); spyOn(authService.user, "next").and.callThrough();
authService.login(); authService.user.subscribe((user) => {
authService.OnUserChanged.on(() => { if (user == null) {
expect(authService.OnUserChanged.trigger).toHaveBeenCalled(); return;
expect(authService.getUser()).not.toBe(null); }
expect(authService.user.next).toHaveBeenCalled();
expect(authService.user.value).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true); expect(authService.isAuthenticated()).toBe(true);
}); });
authService.login(<any>{});
})); }));

View File

@ -53,37 +53,37 @@ describe('NetworkService Success tests', () => {
it('should call GET', inject([NetworkService], (networkService) => { it('should call GET', inject([NetworkService], (networkService) => {
networkService.getJson(testUrl).then((res: Message<any>) => { networkService.getJson(testUrl).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
})); }));
it('should call POST', inject([NetworkService, MockBackend], (networkService) => { it('should call POST', inject([NetworkService, MockBackend], (networkService) => {
networkService.postJson(testUrl, testData).then((res: Message<any>) => { networkService.postJson(testUrl, testData).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
expect(connection.request.text()).toBe(JSON.stringify(testData)); expect(connection.request.text()).toBe(JSON.stringify(testData));
networkService.postJson(testUrl).then((res: Message<any>) => { networkService.postJson(testUrl).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
expect(connection.request.text()).toBe(JSON.stringify({})); expect(connection.request.text()).toBe(JSON.stringify({}));
})); }));
it('should call PUT', inject([NetworkService, MockBackend], (networkService) => { it('should call PUT', inject([NetworkService, MockBackend], (networkService) => {
networkService.putJson(testUrl, testData).then((res: Message<any>) => { networkService.putJson(testUrl, testData).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
expect(connection.request.text()).toBe(JSON.stringify(testData)); expect(connection.request.text()).toBe(JSON.stringify(testData));
networkService.putJson(testUrl).then((res: Message<any>) => { networkService.putJson(testUrl).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
expect(connection.request.text()).toBe(JSON.stringify({})); expect(connection.request.text()).toBe(JSON.stringify({}));
@ -91,8 +91,8 @@ describe('NetworkService Success tests', () => {
it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => { it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => {
networkService.deleteJson(testUrl).then((res: Message<any>) => { networkService.deleteJson(testUrl).then((res: any) => {
expect(res.result).toBe(testResponse); expect(res).toBe(testResponse);
}); });
})); }));
}); });