events-hub.service.spec.ts
1.7 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
47
48
49
50
51
52
import { OpaqueToken } from 'ng-forward';
import { EventsHubService } from './events-hub.service';
describe("EventsHubService", () => {
let eventsHubService: EventsHubService;
it("emits events for the known events", (done) => {
let event = "Event1";
let eventListener = () => {
};
// creates the events hub service which known the event "Event1"
eventsHubService = new EventsHubService([
event
]);
// 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>(event, done);
// emits the event
eventsHubService.emitEvent(event, 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([
'Event1'
]);
// 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([
'Event1'
]);
// emits the event
expect(
() => { eventsHubService.subscribeToEvent<void>('NotKnownEvent', () => {}); }
).toThrowError('Unknown event named NotKnownEvent');
});
});