Created
May 18, 2017 15:09
-
-
Save lucassus/c66dfe23a5d0b2d45bd871bae93bf612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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