index.ios.spec.js 9.38 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
describe('NotificationsIOS', () => {
2
  let deviceEvents = [
yogevbd's avatar
yogevbd committed
3 4 5 6 7 8
    'pushKitRegistered',
    'remoteNotificationsRegistered',
    'remoteNotificationsRegistrationFailed',
    'notificationReceivedForeground',
    'notificationReceivedBackground',
    'notificationOpened'
9 10
  ];

11
  let NotificationsIOS, NotificationAction, NotificationCategory;
yogevbd's avatar
yogevbd committed
12 13
  let constantGuid = 'some-random-uuid';
  let identifiers = ['some-random-uuid', 'other-random-uuid'];
yogevbd's avatar
yogevbd committed
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 53 54
  let someHandler = () => {};
  let nativeAppAddEventListener;
  let deviceAddEventListener;
  let deviceRemoveEventListener;
  let nativeAppRemoveEventListener;
  let nativeModule;

  beforeEach(() => {
    deviceRemoveEventListener = jest.fn();
    nativeAppRemoveEventListener = jest.fn();
    nativeAppAddEventListener = jest.fn(() => {
      return {
        remove: nativeAppRemoveEventListener
      };
    });

    deviceAddEventListener = jest.fn(() => {
      return {
        remove: deviceRemoveEventListener
      };
    });
    const RNBridgeModule = {
      requestPermissionsWithCategories: jest.fn(),
      abandonPermissions: jest.fn(),
      registerPushKit: jest.fn(),
      backgroundTimeRemaining: jest.fn(),
      consumeBackgroundQueue: jest.fn(),
      localNotification: jest.fn(),
      cancelLocalNotification: jest.fn(),
      cancelAllLocalNotifications: jest.fn(),
      getBadgesCount: jest.fn(),
      setBadgesCount: jest.fn(),
      isRegisteredForRemoteNotifications: jest.fn(),
      checkPermissions: jest.fn(),
      removeAllDeliveredNotifications: jest.fn(),
      removeDeliveredNotifications: jest.fn(),
      getDeliveredNotifications: jest.fn()
    };

    jest.mock('react-native', () => {
      return {
55
        NativeModules: {
yogevbd's avatar
yogevbd committed
56
          RNBridgeModule
57
        },
58
        NativeAppEventEmitter: {
yogevbd's avatar
yogevbd committed
59
          addListener: nativeAppAddEventListener
60 61
        },
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
62 63 64 65 66
          addListener: deviceAddEventListener
        }
      };
    });
    nativeModule = RNBridgeModule;
67

yogevbd's avatar
yogevbd committed
68 69 70 71
    jest.mock('uuid', () => {
      return {
        v4: () => constantGuid
      };
72 73
    });

yogevbd's avatar
yogevbd committed
74
    let libUnderTest = require('../lib/src/index.ios');
75
    NotificationsIOS = libUnderTest.default;
76 77
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
78 79
  });

yogevbd's avatar
yogevbd committed
80
  describe('Add Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
81 82
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
83
        NotificationsIOS.addEventListener(event, someHandler);
84

yogevbd's avatar
yogevbd committed
85
        expect(deviceAddEventListener).toHaveBeenCalledWith(event, expect.any(Function));
86 87 88
      });
    });

yogevbd's avatar
yogevbd committed
89 90
    it('should not subscribe to unknown device events', () => {
      NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler);
91

yogevbd's avatar
yogevbd committed
92
      expect(deviceAddEventListener).toHaveBeenCalledTimes(0);
93 94 95
    });
  });

yogevbd's avatar
yogevbd committed
96
  describe('Remove Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
97 98
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
99 100
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
101

yogevbd's avatar
yogevbd committed
102
        expect(deviceRemoveEventListener).toHaveBeenCalledTimes(1);
103 104 105
      });
    });

yogevbd's avatar
yogevbd committed
106 107
    it('should not unsubscribe to unknown device events', () => {
      let someUnsupportedEvent = 'someUnsupportedEvent';
108 109
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
110

yogevbd's avatar
yogevbd committed
111
      expect(deviceRemoveEventListener).toHaveBeenCalledTimes(0);
112
    });
Lidan Hifi's avatar
Lidan Hifi committed
113
  });
114

yogevbd's avatar
yogevbd committed
115
  describe('Notification actions handling', () => {
116 117 118
    let someAction, someCategory;

    let actionOpts = {
yogevbd's avatar
yogevbd committed
119 120 121 122
      activationMode: 'foreground',
      title: 'someAction',
      behavior: 'default',
      identifier: 'SOME_ACTION'
123 124 125
    };

    beforeEach(() => {
126
      someAction = new NotificationAction(actionOpts, () => {});
127 128

      someCategory = new NotificationCategory({
yogevbd's avatar
yogevbd committed
129
        identifier: 'SOME_CATEGORY',
130
        actions: [someAction],
yogevbd's avatar
yogevbd committed
131
        context: 'default'
132 133 134
      });
    });

yogevbd's avatar
yogevbd committed
135 136
    describe('register push notifications', () => {
      it('should call native request permissions with array of categories', () => {
137
        NotificationsIOS.requestPermissions([someCategory]);
138

yogevbd's avatar
yogevbd committed
139
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([{
yogevbd's avatar
yogevbd committed
140
          identifier: 'SOME_CATEGORY',
141
          actions: [actionOpts],
yogevbd's avatar
yogevbd committed
142
          context: 'default'
143 144
        }]);
      });
145

yogevbd's avatar
yogevbd committed
146
      it('should call native request permissions with empty array if no categories specified', () => {
147
        NotificationsIOS.requestPermissions();
148

yogevbd's avatar
yogevbd committed
149
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]);
150
      });
151

yogevbd's avatar
yogevbd committed
152
      it('should subscribe to notificationActionReceived event once, with a single event handler', () => {
153
        NotificationsIOS.requestPermissions([someCategory]);
154

yogevbd's avatar
yogevbd committed
155 156
        expect(nativeAppAddEventListener).toHaveBeenCalledTimes(1);
        expect(nativeAppAddEventListener).toHaveBeenCalledWith('notificationActionReceived', expect.any(Function));
157
      });
158 159
    });

yogevbd's avatar
yogevbd committed
160 161
    describe('reset categories', () => {
      it('should remove notificationActionReceived event handler', () => {
yogevbd's avatar
yogevbd committed
162
        NotificationsIOS.requestPermissions([someCategory]);
163
        NotificationsIOS.resetCategories();
164

yogevbd's avatar
yogevbd committed
165
        expect(nativeAppRemoveEventListener).toHaveBeenCalledTimes(1);
166
      });
167
    });
168

yogevbd's avatar
yogevbd committed
169 170
    describe('get badges count', () => {
      it('should call native getBadgesCount', () => {
171 172 173
        const callback = (count) => console.log(count);
        NotificationsIOS.getBadgesCount(callback);

yogevbd's avatar
yogevbd committed
174
        expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback);
175 176 177
      });
    });

yogevbd's avatar
yogevbd committed
178 179
    describe('set badges count', () => {
      it('should call native setBadgesCount', () => {
180 181
        NotificationsIOS.setBadgesCount(44);

yogevbd's avatar
yogevbd committed
182
        expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44);
183 184 185
      });
    });

186
  });
187

yogevbd's avatar
yogevbd committed
188 189
  describe('register push kit for background notifications', function () {
    it('should call native register push kit method', function () {
190 191
      NotificationsIOS.registerPushKit();

yogevbd's avatar
yogevbd committed
192
      expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1);
193 194 195
    });
  });

yogevbd's avatar
yogevbd committed
196 197
  describe('Abandon push notifications permissions', () => {
    it('should call native abandon permissions method', () => {
198 199
      NotificationsIOS.abandonPermissions();

yogevbd's avatar
yogevbd committed
200
      expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1);
201 202 203
    });
  });

yogevbd's avatar
yogevbd committed
204 205
  describe('Get background remaining time', () => {
    it('should call native background remaining time method', () => {
yogevbd's avatar
yogevbd committed
206
      let someCallback = () => {};
207

208 209
      NotificationsIOS.backgroundTimeRemaining(someCallback);

yogevbd's avatar
yogevbd committed
210
      expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback);
211 212
    });
  });
213

yogevbd's avatar
yogevbd committed
214 215
  describe('Dispatch local notification', () => {
    it('should return generated notification guid', () => {
yogevbd's avatar
yogevbd committed
216
      expect(NotificationsIOS.localNotification({})).toEqual(constantGuid);
217 218
    });

yogevbd's avatar
yogevbd committed
219
    it('should call native local notification method with generated notification guid and notification object', () => {
220
      let someLocalNotification = {
yogevbd's avatar
WIP  
yogevbd committed
221 222
        body: 'some body',
        title: 'some title',
yogevbd's avatar
yogevbd committed
223
        alertAction: 'some action',
yogevbd's avatar
WIP  
yogevbd committed
224
        sound: 'sound',
yogevbd's avatar
yogevbd committed
225
        category: 'SOME_CATEGORY',
226
        userInfo: {
yogevbd's avatar
yogevbd committed
227
          'key': 'value'
228 229 230 231 232
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

yogevbd's avatar
yogevbd committed
233
      expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid);
234 235 236
    });
  });

yogevbd's avatar
yogevbd committed
237 238
  describe('Cancel local notification', () => {
    it('should call native cancel local notification method', () => {
239 240
      NotificationsIOS.cancelLocalNotification(constantGuid);

yogevbd's avatar
yogevbd committed
241
      expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid);
242 243 244
    });
  });

yogevbd's avatar
yogevbd committed
245 246
  describe('Cancel all local notifications', () => {
    it('should call native cancel all local notifications method', () => {
247 248
      NotificationsIOS.cancelAllLocalNotifications();

yogevbd's avatar
yogevbd committed
249
      expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith();
250 251
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
252

yogevbd's avatar
yogevbd committed
253 254
  describe('Is registered for remote notifications ', () => {
    it('should call native is registered for remote notifications', () => {
Ran Greenberg's avatar
Ran Greenberg committed
255
      NotificationsIOS.isRegisteredForRemoteNotifications();
yogevbd's avatar
yogevbd committed
256
      expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith();
Ran Greenberg's avatar
Ran Greenberg committed
257 258 259

    });
  });
Ryan Eberhardt's avatar
Ryan Eberhardt committed
260

yogevbd's avatar
yogevbd committed
261 262
  describe('Check permissions ', () => {
    it('should call native check permissions', () => {
Ryan Eberhardt's avatar
Ryan Eberhardt committed
263
      NotificationsIOS.checkPermissions();
yogevbd's avatar
yogevbd committed
264
      expect(nativeModule.checkPermissions).toHaveBeenCalledWith();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
265 266 267

    });
  });
268

yogevbd's avatar
yogevbd committed
269 270
  describe('Remove all delivered notifications', () => {
    it('should call native remove all delivered notifications method', () => {
271 272
      NotificationsIOS.removeAllDeliveredNotifications();

yogevbd's avatar
yogevbd committed
273
      expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith();
274 275 276
    });
  });

yogevbd's avatar
yogevbd committed
277 278
  describe('Remove delivered notifications', () => {
    it('should call native remove delivered notifications method', () => {
279 280
      NotificationsIOS.removeDeliveredNotifications(identifiers);

yogevbd's avatar
yogevbd committed
281
      expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers);
282 283 284
    });
  });

yogevbd's avatar
yogevbd committed
285 286
  describe('Get delivered notifications', () => {
    it('should call native get delivered notifications method', () => {
287 288 289
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

yogevbd's avatar
yogevbd committed
290
      expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback);
291 292
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
293
});