Skip to content

Instantly share code, notes, and snippets.

@lucassus
Created May 18, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucassus/c66dfe23a5d0b2d45bd871bae93bf612 to your computer and use it in GitHub Desktop.
Save lucassus/c66dfe23a5d0b2d45bd871bae93bf612 to your computer and use it in GitHub Desktop.
import { async, TestBed } from '@angular/core/testing';
import {
BaseRequestOptions,
ConnectionBackend,
Http,
RequestMethod,
Response,
ResponseOptions
} from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { AuthenticationService } from './authentication.service';
describe('AuthenticationService', () => {
let mockBackend: MockBackend;
let authenticationService: AuthenticationService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
MockBackend,
BaseRequestOptions,
{
provide: Http,
deps: [MockBackend, BaseRequestOptions],
useFactory: (backend: ConnectionBackend, options: BaseRequestOptions) => {
return new Http(backend, options);
}
},
AuthenticationService
]
});
mockBackend = TestBed.get(MockBackend);
authenticationService = TestBed.get(AuthenticationService);
});
describe('.login', () => {
let connection: MockConnection;
const credentials = { login: 'login', password: 'password' };
beforeEach(async(() => {
mockBackend.connections.subscribe((c: MockConnection) => {
connection = c;
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.url).toBe('/api/authenticate');
const expectedCredentials = JSON.parse(connection.request.getBody());
expect(expectedCredentials.login).toEqual('login');
expect(expectedCredentials.password).toEqual('password');
// connection.mockRespond(new Response(new ResponseOptions({
// status: 200
// })));
});
}));
describe('on success', () => {
beforeEach(async(() => {
// mockBackend.connections.subscribe((connection: MockConnection) => {
// expect(connection.request.method).toBe(RequestMethod.Post);
// expect(connection.request.url).toBe('/api/authenticate');
//
// const credentials = JSON.parse(connection.request.getBody());
// expect(credentials.login).toEqual('login');
// expect(credentials.password).toEqual('password');
//
// });
connection.mockRespond(new Response(new ResponseOptions({
status: 200
})));
}));
it('emits true', () => {
authenticationService.authenticate(credentials).subscribe((result) => {
expect(result).toBeTruthy();
});
});
});
describe('on error', () => {
beforeEach(() => {
// mockBackend.connections.subscribe((connection: MockConnection) => {
// expect(connection.request.method).toBe(RequestMethod.Post);
// expect(connection.request.url).toBe('/api/authenticate');
//
// const credentials = JSON.parse(connection.request.getBody());
// expect(credentials.login).toEqual('login');
// expect(credentials.password).toEqual('password');
//
// });
connection.mockError();
});
it('emits false', () => {
authenticationService.authenticate(credentials).subscribe((result) => {
expect(result).toBeFalsy();
})
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment