5737b1805a5e0d3a2101ab937a5bb617bcb845ce
[sdc/sdc-pubsub.git] / lib / base-pubsub.spec.ts
1 declare const window: Window;
2
3 import {BasePubSub} from './base-pubsub';
4
5 describe('BasePubSub Tests', () => {
6     let basePubSub: BasePubSub;
7
8     let testSub: string = 'testSub';
9     let testWindow = window;
10     let testSubUrl: string = 'http://127.0.0.1';
11
12     beforeEach(() => {
13         basePubSub = new BasePubSub('testId');
14     });
15
16     describe('constructor tests', () => {
17         it('should init class property', () => {
18             expect(basePubSub.subscribers.size).toBe(0);
19             expect(basePubSub.eventsCallbacks.length).toBe(0);
20             expect(basePubSub.eventsToWait.size).toBe(0);
21             expect(basePubSub.clientId).toBe('testId');
22             expect(basePubSub.lastEventNotified).toBe('');
23         });
24     });
25
26     describe('register function tests', () => {
27
28         it('Should add new subscriber with the sent url to subscribers array ' +
29             'when calling register function with url', () => {
30             basePubSub.register(testSub, testWindow, testSubUrl);
31
32             let actualSub = basePubSub.subscribers.get(testSub);
33
34             expect(basePubSub.subscribers.size).toBe(1);
35             expect(actualSub.window).toBe(testWindow);
36             expect(actualSub.locationUrl).toBe(testSubUrl);
37         });
38
39         it('Should add new subscriber with the window location.href to subscribers array ' +
40             'when calling register function without url', () => {
41             basePubSub.register(testSub, testWindow, undefined);
42
43             let actualSub = basePubSub.subscribers.get(testSub);
44
45             expect(basePubSub.subscribers.size).toBe(1);
46             expect(actualSub.window).toBe(testWindow);
47             expect(actualSub.locationUrl).toBe(window.location.href);
48         });
49     });
50
51     describe('unregister function tests', () => {
52
53         it('Should remove subscriber from subscribers list', () => {
54             basePubSub.register(testSub, testWindow, testSubUrl);
55
56             expect(basePubSub.subscribers.size).toBe(1);
57
58             basePubSub.unregister(testSub);
59
60             expect(basePubSub.subscribers.size).toBe(0);
61         });
62     });
63
64     describe('on function tests', () => {
65         let callback = () => {return true};
66
67         it('Should add new callback to events callback array', () => {
68             basePubSub.on(callback);
69
70             expect(basePubSub.eventsCallbacks.length).toBe(1);
71
72             let actualCallback = basePubSub.eventsCallbacks[0];
73
74             expect(actualCallback).toBe(callback);
75         });
76
77         it('Should not add callback to events callback array if it already exists', () => {
78             basePubSub.on(callback);
79
80             expect(basePubSub.eventsCallbacks.length).toBe(1);
81
82             basePubSub.on(callback);
83
84             expect(basePubSub.eventsCallbacks.length).toBe(1);
85         });
86     });
87
88     describe('off function tests', () => {
89         let callback = () => {return true};
90
91         it('Should remove callback from events callback array', () => {
92             basePubSub.on(callback);
93
94             expect(basePubSub.eventsCallbacks.length).toBe(1);
95
96             basePubSub.off(callback);
97
98             expect(basePubSub.eventsCallbacks.length).toBe(0);
99         });
100     });
101
102     describe('isWaitingForEvent function tests', () => {
103         let eventsMap = new Map<string, Array<string>>();
104         eventsMap.set('eventsKey', ['WINDOW_OUT']);
105
106         beforeEach(() => {
107             basePubSub.eventsToWait = eventsMap;
108         });
109
110         it('Should return true when the event is found in the events to wait array', () => {
111             let isWaiting = basePubSub.isWaitingForEvent('WINDOW_OUT');
112
113             expect(isWaiting).toBeTruthy();
114         });
115
116         it('Should return false when the event is not found in the events to wait array', () => {
117             let isWaiting = basePubSub.isWaitingForEvent('CHECK_IN');
118
119             expect(isWaiting).toBeFalsy();
120         });
121     });
122
123     describe('notify function tests', () => {
124         let eventType: string = 'CHECK_IN';
125         let callback;
126         beforeEach(() => {
127              callback = jest.fn();
128         });
129
130         it('should only update the last event notified property when no subscribers registered', () => {
131             basePubSub.notify(eventType);
132
133             expect(basePubSub.lastEventNotified).toBe(eventType);
134         });
135
136         it('should call post message with the right parameters when there are subscribers registered', () => {
137             testWindow.postMessage = jest.fn();
138             basePubSub.register(testSub, testWindow, testSubUrl);
139             basePubSub.notify(eventType);
140
141             let sub = basePubSub.subscribers.get(testSub);
142
143             let eventObj = {
144                 type: eventType,
145                 data: undefined,
146                 originId: basePubSub.clientId
147             };
148
149             expect(sub.window.postMessage).toHaveBeenCalledWith(eventObj, sub.locationUrl);
150         });
151
152         it('should execute the callback function when calling notify with subscription function with no subscribers', () => {
153             let callback = jest.fn();
154
155             basePubSub.notify(eventType).subscribe(callback);
156
157             expect(callback).toHaveBeenCalled();
158         });
159
160         it('should execute the callback function when calling notify with subscription function ' +
161             'with connected subscribers after all been notified', () => {
162             basePubSub.register(testSub, testWindow, testSubUrl);
163
164             basePubSub.notify(eventType).subscribe(callback);
165
166             expect(callback).toHaveBeenCalled();
167         });
168
169         it('should register an action completed function to pub sub when an event that is in the events to wait list ' +
170             'is being fired', () => {
171             let eventsMap = new Map<string, Array<string>>();
172             eventsMap.set(testSub, ['CHECK_IN']);
173             basePubSub.on = jest.fn();
174
175             basePubSub.register(testSub, testWindow, testSubUrl);
176             basePubSub.eventsToWait = eventsMap;
177
178             basePubSub.notify(eventType).subscribe(callback);
179
180             expect(basePubSub.on).toHaveBeenCalled();
181         });
182     })
183 });