1 declare const window: Window;
3 import {BasePubSub} from './base-pubsub';
5 describe('BasePubSub Tests', () => {
6 let basePubSub: BasePubSub;
8 let testSub: string = 'testSub';
9 let testWindow = window;
10 let testSubUrl: string = 'http://127.0.0.1';
13 basePubSub = new BasePubSub('testId');
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('');
26 describe('register function tests', () => {
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);
32 let actualSub = basePubSub.subscribers.get(testSub);
34 expect(basePubSub.subscribers.size).toBe(1);
35 expect(actualSub.window).toBe(testWindow);
36 expect(actualSub.locationUrl).toBe(testSubUrl);
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);
43 let actualSub = basePubSub.subscribers.get(testSub);
45 expect(basePubSub.subscribers.size).toBe(1);
46 expect(actualSub.window).toBe(testWindow);
47 expect(actualSub.locationUrl).toBe(window.location.href);
51 describe('unregister function tests', () => {
53 it('Should remove subscriber from subscribers list', () => {
54 basePubSub.register(testSub, testWindow, testSubUrl);
56 expect(basePubSub.subscribers.size).toBe(1);
58 basePubSub.unregister(testSub);
60 expect(basePubSub.subscribers.size).toBe(0);
64 describe('on function tests', () => {
65 let callback = () => {return true};
67 it('Should add new callback to events callback array', () => {
68 basePubSub.on(callback);
70 expect(basePubSub.eventsCallbacks.length).toBe(1);
72 let actualCallback = basePubSub.eventsCallbacks[0];
74 expect(actualCallback).toBe(callback);
77 it('Should not add callback to events callback array if it already exists', () => {
78 basePubSub.on(callback);
80 expect(basePubSub.eventsCallbacks.length).toBe(1);
82 basePubSub.on(callback);
84 expect(basePubSub.eventsCallbacks.length).toBe(1);
88 describe('off function tests', () => {
89 let callback = () => {return true};
91 it('Should remove callback from events callback array', () => {
92 basePubSub.on(callback);
94 expect(basePubSub.eventsCallbacks.length).toBe(1);
96 basePubSub.off(callback);
98 expect(basePubSub.eventsCallbacks.length).toBe(0);
102 describe('isWaitingForEvent function tests', () => {
103 let eventsMap = new Map<string, Array<string>>();
104 eventsMap.set('eventsKey', ['WINDOW_OUT']);
107 basePubSub.eventsToWait = eventsMap;
110 it('Should return true when the event is found in the events to wait array', () => {
111 let isWaiting = basePubSub.isWaitingForEvent('WINDOW_OUT');
113 expect(isWaiting).toBeTruthy();
116 it('Should return false when the event is not found in the events to wait array', () => {
117 let isWaiting = basePubSub.isWaitingForEvent('CHECK_IN');
119 expect(isWaiting).toBeFalsy();
123 describe('notify function tests', () => {
124 let eventType: string = 'CHECK_IN';
127 callback = jest.fn();
130 it('should only update the last event notified property when no subscribers registered', () => {
131 basePubSub.notify(eventType);
133 expect(basePubSub.lastEventNotified).toBe(eventType);
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);
141 let sub = basePubSub.subscribers.get(testSub);
146 originId: basePubSub.clientId
149 expect(sub.window.postMessage).toHaveBeenCalledWith(eventObj, sub.locationUrl);
152 it('should execute the callback function when calling notify with subscription function with no subscribers', () => {
153 let callback = jest.fn();
155 basePubSub.notify(eventType).subscribe(callback);
157 expect(callback).toHaveBeenCalled();
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);
164 basePubSub.notify(eventType).subscribe(callback);
166 expect(callback).toHaveBeenCalled();
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();
175 basePubSub.register(testSub, testWindow, testSubUrl);
176 basePubSub.eventsToWait = eventsMap;
178 basePubSub.notify(eventType).subscribe(callback);
180 expect(basePubSub.on).toHaveBeenCalled();