This commit is contained in:
Manu Mtz.-Almeida
2016-11-28 17:49:15 +01:00
2 changed files with 40 additions and 1 deletions

View File

@ -100,7 +100,7 @@ export class Events {
let responses: any[] = [];
t.forEach((handler: any) => {
responses.push(handler(args));
responses.push(handler(...args));
});
return responses;
}

View File

@ -0,0 +1,39 @@
import { Events } from '../events';
describe('Events service', () => {
let events: Events;
let listener: jasmine.Spy;
beforeEach(() => {
events = new Events();
});
it('should call listener when event is published', () => {
const eventParams = [{}, {}, {}];
listener = jasmine.createSpy('listener');
events.subscribe('test', listener);
events.publish('test', ...eventParams);
expect(listener).toHaveBeenCalledWith(...eventParams);
});
it('should unsubscribe listener', () => {
listener = jasmine.createSpy('listener');
events.subscribe('test', listener);
events.unsubscribe('test', listener);
expect(listener).not.toHaveBeenCalled();
});
it('should return an array of responses when event is published', () => {
const [response, anotherResponse] = [{}, {}];
const listener = jasmine.createSpy('listener').and.returnValue(response);
const anotherListener = jasmine.createSpy('anotherListener').and.returnValue(anotherResponse);
events.subscribe('test', listener, anotherListener);
const responses = events.publish('test');
expect(responses).toEqual([response, anotherResponse]);
});
});