angular4-testing/project-manager/src/app/services/login-service/login.service.spec.ts

54 lines
1.8 KiB
TypeScript

import {MockBackend} from "@angular/http/testing";
import {inject, TestBed} from "@angular/core/testing";
import {mockIO} from "../../mocks/mock-socket";
import {SOCKET_IO} from "../../app.tokens";
import {BaseRequestOptions, ConnectionBackend, Http, RequestMethod, ResponseOptions} from "@angular/http";
import {LoginService} from "./login-service";
import {UserStore} from "../stores/user.store";
describe('Login-Service', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
LoginService,
UserStore,
{provide: SOCKET_IO, useValue: mockIO},
BaseRequestOptions,
MockBackend,
{
provide: Http, useFactory: (mockBackend: ConnectionBackend,
defaultOptions: BaseRequestOptions) => {
return new Http(mockBackend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
]
});
});
let loginService: LoginService;
let userStore: UserStore;
let mockBackend: MockBackend;
beforeEach(inject([LoginService, UserStore, MockBackend],
(_loginService, _userStore, _mockBackend) => {
loginService = _loginService;
userStore = _userStore;
mockBackend = _mockBackend;
})
);
it('should trigger a HTTP-GET and receive User-Object', (() => {
let user = {name: 'admin'};
mockBackend.connections.subscribe(connection => {
const expectedUrl = 'http://localhost:3000/api/users?name=admin';
expect(connection.request.url).toBe(expectedUrl);
expect(connection.request.method).toBe(RequestMethod.Get);
const response = new ResponseOptions({body: JSON.stringify(user)});
connection.mockRespond(new Response(response));
});
loginService.getUser('admin');
}));
});