EventsRegistry.test.ts 7.57 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2
import { EventsRegistry } from './EventsRegistry';
import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
3
import { Notification } from '../DTO/Notification';
yogevbd's avatar
yogevbd committed
4 5 6
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock';
import { NotificationResponse } from '../interfaces/NotificationEvents';
yogevbd's avatar
yogevbd committed
7
import { Platform } from 'react-native';
8
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
yogevbd's avatar
yogevbd committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

describe('EventsRegistry', () => {
  let uut: EventsRegistry;
  const mockNativeEventsReceiver = new NativeEventsReceiver();
  const mockNativeCommandsSender = new NativeCommandsSender();
  const completionCallbackWrapper = new CompletionCallbackWrapper(mockNativeCommandsSender);

  beforeEach(() => {
    uut = new EventsRegistry(mockNativeEventsReceiver, completionCallbackWrapper);
  });

  describe('registerRemoteNotificationsReceived', () => {
    it('delegates to nativeEventsReceiver', () => {
      const cb = jest.fn();
  
      uut.registerNotificationReceived(cb);
  
      expect(mockNativeEventsReceiver.registerRemoteNotificationReceived).toHaveBeenCalledTimes(1);
      expect(mockNativeEventsReceiver.registerRemoteNotificationReceived).toHaveBeenCalledWith(expect.any(Function));
    });
  
    it('should wrap callback with completion block', () => {
      const wrappedCallback = jest.fn();
32
      const notification: Notification  = new Notification({identifier: 'identifier'});
yogevbd's avatar
yogevbd committed
33 34 35 36 37 38 39 40 41 42
      
      uut.registerNotificationReceived(wrappedCallback);
      const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
      call(notification);
      
      expect(wrappedCallback).toBeCalledWith(notification, expect.any(Function));
      expect(wrappedCallback).toBeCalledTimes(1);
    });

    it('should wrap callback with completion block', () => {
43
      const expectedNotification: Notification  = new Notification({identifier: 'identifier'});
yogevbd's avatar
yogevbd committed
44 45 46 47 48 49 50 51
      
      uut.registerNotificationReceived((notification) => {
        expect(notification).toEqual(expectedNotification);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
      call(expectedNotification);
    });

yogevbd's avatar
yogevbd committed
52
    it('should invoke finishPresentingNotification', () => {
53
      const notification: Notification  = new Notification({identifier: 'notificationId'});
yogevbd's avatar
yogevbd committed
54 55 56 57 58 59 60 61 62 63
      const response: NotificationCompletion  = {alert: true}
      
      uut.registerNotificationReceived((notification, completion) => {
        completion(response);
        
        expect(mockNativeCommandsSender.finishPresentingNotification).toBeCalledWith(notification.identifier, response);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
      call(notification);
    });
yogevbd's avatar
yogevbd committed
64 65 66

    it('should not invoke finishPresentingNotification on Android', () => {
      Platform.OS = 'android';
67
      const expectedNotification: Notification  = new Notification({identifier: 'notificationId'});
yogevbd's avatar
yogevbd committed
68 69 70 71 72 73 74 75 76 77
      const response: NotificationCompletion  = {alert: true}
      
      uut.registerNotificationReceived((notification, completion) => {
        completion(response);
        expect(expectedNotification).toEqual(notification);
        expect(mockNativeCommandsSender.finishPresentingNotification).toBeCalledTimes(0);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
      call(expectedNotification);
    });
yogevbd's avatar
yogevbd committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91
  });

  describe('', () => {
    it('delegates to nativeEventsReceiver', () => {
      const cb = jest.fn();
  
      uut.registerRemoteNotificationOpened(cb);
  
      expect(mockNativeEventsReceiver.registerRemoteNotificationOpened).toHaveBeenCalledTimes(1);
      expect(mockNativeEventsReceiver.registerRemoteNotificationOpened).toHaveBeenCalledWith(expect.any(Function));
    });
  
    it('should wrap callback with completion block', () => {
      const wrappedCallback = jest.fn();
92
      const notification: Notification  = new Notification({identifier: 'identifier'});
yogevbd's avatar
yogevbd committed
93 94 95 96 97 98 99 100 101 102 103
      const response: NotificationResponse = {notification, identifier: 'responseId'};

      uut.registerRemoteNotificationOpened(wrappedCallback);
      const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
      call(response);
      
      expect(wrappedCallback).toBeCalledWith(response, expect.any(Function));
      expect(wrappedCallback).toBeCalledTimes(1);
    });

    it('should wrap callback with completion block', () => {
104
      const notification: Notification  = new Notification({identifier: 'identifier'});
yogevbd's avatar
yogevbd committed
105 106 107 108 109 110 111 112 113 114
      const expectedResponse: NotificationResponse = {notification, identifier: 'responseId'}
      
      uut.registerRemoteNotificationOpened((response) => {
        expect(response).toEqual(expectedResponse);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
      call(expectedResponse);
    });

    it('calling completion should invoke finishHandlingAction', () => {
115
      const expectedNotification: Notification  = new Notification({identifier: 'notificationId'});
yogevbd's avatar
yogevbd committed
116
      
yogevbd's avatar
yogevbd committed
117
      uut.registerRemoteNotificationOpened((notification, completion) => {
yogevbd's avatar
yogevbd committed
118 119
        completion();
        
yogevbd's avatar
yogevbd committed
120
        expect(expectedNotification).toEqual(notification);
yogevbd's avatar
yogevbd committed
121 122 123
        expect(mockNativeCommandsSender.finishHandlingAction).toBeCalledWith(notification.identifier);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
yogevbd's avatar
yogevbd committed
124 125 126 127 128
      call(expectedNotification);
    });

    it('should not invoke finishHandlingAction on Android', () => {
      Platform.OS = 'android';
129
      const expectedNotification: Notification  = new Notification({identifier: 'notificationId'});
yogevbd's avatar
yogevbd committed
130 131 132 133 134 135 136 137 138
      
      uut.registerRemoteNotificationOpened((notification, completion) => {
        completion();
        
        expect(expectedNotification).toEqual(notification);
        expect(mockNativeCommandsSender.finishHandlingAction).toBeCalledTimes(0);
      });
      const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
      call(expectedNotification);
yogevbd's avatar
yogevbd committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    });
  });

  it('delegates registerRemoteNotificationsRegistered to nativeEventsReceiver', () => {
    const cb = jest.fn();
    uut.registerRemoteNotificationsRegistered(cb);
    expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistered).toHaveBeenCalledTimes(1);
    expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistered).toHaveBeenCalledWith(cb);
  });

  it('delegates registerPushKitRegistered to nativeEventsReceiver', () => {
    const cb = jest.fn();
    uut.registerPushKitRegistered(cb);
    expect(mockNativeEventsReceiver.registerPushKitRegistered).toHaveBeenCalledTimes(1);
    expect(mockNativeEventsReceiver.registerPushKitRegistered).toHaveBeenCalledWith(cb);
  });

  it('delegates registerPushKitNotificationReceived to nativeEventsReceiver', () => {
    const cb = jest.fn();
    uut.registerPushKitNotificationReceived(cb);
    expect(mockNativeEventsReceiver.registerPushKitNotificationReceived).toHaveBeenCalledTimes(1);
    expect(mockNativeEventsReceiver.registerPushKitNotificationReceived).toHaveBeenCalledWith(cb);
  });

  it('delegates registerRemoteNotificationsRegistrationFailed to nativeEventsReceiver', () => {
    const cb = jest.fn();
    uut.registerRemoteNotificationsRegistrationFailed(cb);
    expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistrationFailed).toHaveBeenCalledTimes(1);
    expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistrationFailed).toHaveBeenCalledWith(cb);
  });
});