Skip to content

Instantly share code, notes, and snippets.

@icfantv
Created July 13, 2016 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icfantv/3faf0922fa680383e49523b9f8a3b4f3 to your computer and use it in GitHub Desktop.
Save icfantv/3faf0922fa680383e49523b9f8a3b4f3 to your computer and use it in GitHub Desktop.
ng2 Service Test (RC4)
import {Injector, ReflectiveInjector} from '@angular/core';
import {HTTP_PROVIDERS, Response, ResponseOptions, XHRBackend} from '@angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {ISisConsumer} from '../models';
import {TestSisConsumerGenerators} from '../test/TestSisConsumerGenerators';
import {SisService} from './SisService';
describe('test the sis consumer service', () => {
let injector: Injector;
let backend: MockBackend;
let connection: MockConnection;
let service: SisService;
beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate(
[
HTTP_PROVIDERS,
{
provide: XHRBackend,
useClass: MockBackend,
},
SisService
]);
backend = injector.get(XHRBackend);
service = injector.get(SisService);
});
afterEach(() => {
injector = undefined;
backend = undefined;
connection = undefined;
service = undefined;
});
describe('test initialization', () => {
it('should initialize everything', () => {
expect(SisService).toBeDefined();
expect(service).toBeDefined();
expect(service instanceof SisService).toBeTruthy();
});
});
describe('test the retrieval of the full list of sis consumers', () => {
it('should make a request to the full list of sis consumers API', (done: Function) => {
const url = 'ws/sis/consumers';
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toEqual(url);
done();
});
service.getAll().subscribe();
});
it('should return a list of sis consumers', (done: Function) => {
const size = 3;
const consumers: Array<ISisConsumer> =
TestSisConsumerGenerators.generateMultipleSisConsumers(size);
backend.connections.subscribe((c: MockConnection) => {
c.mockRespond(new Response(new ResponseOptions({body: consumers})));
});
service.getAll().subscribe((c: Array<ISisConsumer>) => {
expect(c).toBeDefined();
expect(Array.isArray(c)).toBeTruthy();
expect(c.length).toBe(size);
for (let i = 0; i < size; i++) {
expect(c[i].allowPII).toMatch(/true|false/);
expect(c[i].id).toBe(`id ${i}`);
expect(c[i].identity).toBe(`identity ${i}`);
expect(c[i].profilesAdded).toMatch(/true|false/);
}
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment