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

10
  let NotificationsIOS, NotificationAction, NotificationCategory;
yogevbd's avatar
yogevbd committed
11 12
  let constantGuid = 'some-random-uuid';
  let identifiers = ['some-random-uuid', 'other-random-uuid'];
yogevbd's avatar
yogevbd committed
13 14
  let someHandler = () => {};
  let nativeModule;
yogevbd's avatar
yogevbd committed
15 16
  let NativeAppEventEmitter;
  let DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
17 18 19

  beforeEach(() => {
    jest.mock('react-native', () => {
yogevbd's avatar
yogevbd committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      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()
      };
yogevbd's avatar
yogevbd committed
37
      return {
38
        NativeModules: {
yogevbd's avatar
yogevbd committed
39
          RNBridgeModule
40
        },
41
        NativeAppEventEmitter: {
yogevbd's avatar
yogevbd committed
42 43 44 45 46
          addListener: jest.fn(() => {
            return {
              remove: jest.fn()
            };
          })
47 48
        },
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
49 50 51 52 53
          addListener: jest.fn(() => {
            return {
              remove: jest.fn()
            };
          })
yogevbd's avatar
yogevbd committed
54 55 56
        }
      };
    });
57

yogevbd's avatar
yogevbd committed
58 59 60
    nativeModule = require('react-native').NativeModules.RNBridgeModule;
    NativeAppEventEmitter = require('react-native').NativeAppEventEmitter;
    DeviceEventEmitter = require('react-native').DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
61 62
    jest.mock('uuid', () => {
      return {
yogevbd's avatar
yogevbd committed
63
        v4: () => 'some-random-uuid'
yogevbd's avatar
yogevbd committed
64
      };
65 66
    });

yogevbd's avatar
yogevbd committed
67
    let libUnderTest = require('../lib/src/index.ios');
68
    NotificationsIOS = libUnderTest.default;
69 70
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
71 72
  });

yogevbd's avatar
yogevbd committed
73
  describe('Add Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
74 75
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
76
        NotificationsIOS.addEventListener(event, someHandler);
yogevbd's avatar
yogevbd committed
77
        expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function));
78 79 80
      });
    });

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

yogevbd's avatar
yogevbd committed
84
      expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0);
85 86 87
    });
  });

yogevbd's avatar
yogevbd committed
88
  describe('Remove Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
89 90
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
yogevbd's avatar
yogevbd committed
91 92 93 94
        const removeCallback = jest.fn();
        DeviceEventEmitter.addListener.mockReturnValueOnce({
          remove: removeCallback
        });
95 96
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
97

yogevbd's avatar
yogevbd committed
98
        expect(removeCallback).toHaveBeenCalledTimes(1);
99 100 101
      });
    });

yogevbd's avatar
yogevbd committed
102 103
    it('should not unsubscribe to unknown device events', () => {
      let someUnsupportedEvent = 'someUnsupportedEvent';
yogevbd's avatar
yogevbd committed
104 105 106 107 108
      const removeCallback = jest.fn();
      DeviceEventEmitter.addListener.mockReturnValueOnce({
        remove: removeCallback
      });

109 110
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
111

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

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

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

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

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

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

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

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

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

yogevbd's avatar
yogevbd committed
154 155
    describe('get badges count', () => {
      it('should call native getBadgesCount', () => {
156 157 158
        const callback = (count) => console.log(count);
        NotificationsIOS.getBadgesCount(callback);

yogevbd's avatar
yogevbd committed
159
        expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback);
160 161 162
      });
    });

yogevbd's avatar
yogevbd committed
163 164
    describe('set badges count', () => {
      it('should call native setBadgesCount', () => {
165 166
        NotificationsIOS.setBadgesCount(44);

yogevbd's avatar
yogevbd committed
167
        expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44);
168 169 170
      });
    });

171
  });
172

yogevbd's avatar
yogevbd committed
173 174
  describe('register push kit for background notifications', function () {
    it('should call native register push kit method', function () {
175 176
      NotificationsIOS.registerPushKit();

yogevbd's avatar
yogevbd committed
177
      expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1);
178 179 180
    });
  });

yogevbd's avatar
yogevbd committed
181 182
  describe('Abandon push notifications permissions', () => {
    it('should call native abandon permissions method', () => {
183 184
      NotificationsIOS.abandonPermissions();

yogevbd's avatar
yogevbd committed
185
      expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1);
186 187 188
    });
  });

yogevbd's avatar
yogevbd committed
189 190
  describe('Get background remaining time', () => {
    it('should call native background remaining time method', () => {
yogevbd's avatar
yogevbd committed
191
      let someCallback = () => {};
192

193 194
      NotificationsIOS.backgroundTimeRemaining(someCallback);

yogevbd's avatar
yogevbd committed
195
      expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback);
196 197
    });
  });
198

yogevbd's avatar
yogevbd committed
199 200
  describe('Dispatch local notification', () => {
    it('should return generated notification guid', () => {
yogevbd's avatar
yogevbd committed
201
      expect(NotificationsIOS.localNotification({})).toEqual(constantGuid);
202 203
    });

yogevbd's avatar
yogevbd committed
204
    it('should call native local notification method with generated notification guid and notification object', () => {
205
      let someLocalNotification = {
yogevbd's avatar
WIP  
yogevbd committed
206 207
        body: 'some body',
        title: 'some title',
yogevbd's avatar
yogevbd committed
208
        alertAction: 'some action',
yogevbd's avatar
WIP  
yogevbd committed
209
        sound: 'sound',
yogevbd's avatar
yogevbd committed
210
        category: 'SOME_CATEGORY',
211
        userInfo: {
yogevbd's avatar
yogevbd committed
212
          'key': 'value'
213 214 215 216 217
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

yogevbd's avatar
yogevbd committed
218
      expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid);
219 220 221
    });
  });

yogevbd's avatar
yogevbd committed
222 223
  describe('Cancel local notification', () => {
    it('should call native cancel local notification method', () => {
224 225
      NotificationsIOS.cancelLocalNotification(constantGuid);

yogevbd's avatar
yogevbd committed
226
      expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid);
227 228 229
    });
  });

yogevbd's avatar
yogevbd committed
230 231
  describe('Cancel all local notifications', () => {
    it('should call native cancel all local notifications method', () => {
232 233
      NotificationsIOS.cancelAllLocalNotifications();

yogevbd's avatar
yogevbd committed
234
      expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith();
235 236
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
237

yogevbd's avatar
yogevbd committed
238 239
  describe('Is registered for remote notifications ', () => {
    it('should call native is registered for remote notifications', () => {
Ran Greenberg's avatar
Ran Greenberg committed
240
      NotificationsIOS.isRegisteredForRemoteNotifications();
yogevbd's avatar
yogevbd committed
241
      expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith();
Ran Greenberg's avatar
Ran Greenberg committed
242 243 244

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

yogevbd's avatar
yogevbd committed
246 247
  describe('Check permissions ', () => {
    it('should call native check permissions', () => {
Ryan Eberhardt's avatar
Ryan Eberhardt committed
248
      NotificationsIOS.checkPermissions();
yogevbd's avatar
yogevbd committed
249
      expect(nativeModule.checkPermissions).toHaveBeenCalledWith();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
250 251 252

    });
  });
253

yogevbd's avatar
yogevbd committed
254 255
  describe('Remove all delivered notifications', () => {
    it('should call native remove all delivered notifications method', () => {
256 257
      NotificationsIOS.removeAllDeliveredNotifications();

yogevbd's avatar
yogevbd committed
258
      expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith();
259 260 261
    });
  });

yogevbd's avatar
yogevbd committed
262 263
  describe('Remove delivered notifications', () => {
    it('should call native remove delivered notifications method', () => {
264 265
      NotificationsIOS.removeDeliveredNotifications(identifiers);

yogevbd's avatar
yogevbd committed
266
      expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers);
267 268 269
    });
  });

yogevbd's avatar
yogevbd committed
270 271
  describe('Get delivered notifications', () => {
    it('should call native get delivered notifications method', () => {
272 273 274
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

yogevbd's avatar
yogevbd committed
275
      expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback);
276 277
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
278
});