events-hub.service.spec.ts
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { OpaqueToken } from 'ng-forward';
import { EventsHubService, EventsHubKnownEventNames } from './events-hub.service';
describe("EventsHubService", () => {
let eventsHubService: EventsHubService;
let event1 = 'Event 1';
let eventsHubKnownEventNames = <EventsHubKnownEventNames>{ getNames: () => { return [ event1]; }};
it("emits events for the known events", (done) => {
let eventListener = () => {
};
// creates the events hub service which known the event "Event1"
eventsHubService = new EventsHubService(eventsHubKnownEventNames);
// subscribe to the event passing the done Function as the eventListener
// if the event emits works the done function is called and the
// test will pass
eventsHubService.subscribeToEvent<any>(event1, done);
// emits the event
eventsHubService.emitEvent(event1, null);
});
it("throws error when trying to emit an unknow event", () => {
let eventListener = () => {
};
// creates the events hub service which known the event "Event1"
eventsHubService = new EventsHubService(eventsHubKnownEventNames);
// emits the event
expect(
() => { eventsHubService.emitEvent('NotKnownEvent', null); }
).toThrowError('Unknown event named NotKnownEvent');
});
it("throws error when trying to subscribe to an unknow event", () => {
let eventListener = () => {
};
// creates the events hub service which known the event "Event1"
eventsHubService = new EventsHubService(eventsHubKnownEventNames);
// emits the event
expect(
() => { eventsHubService.subscribeToEvent<void>('NotKnownEvent', () => {}); }
).toThrowError('Unknown event named NotKnownEvent');
});
});