index.ios.spec.js 9.4 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1

yogevbd's avatar
yogevbd committed
2
describe.only('NotificationsIOS', () => {
3
  let deviceEvents = [
yogevbd's avatar
yogevbd committed
4 5 6 7 8 9
    'pushKitRegistered',
    'remoteNotificationsRegistered',
    'remoteNotificationsRegistrationFailed',
    'notificationReceivedForeground',
    'notificationReceivedBackground',
    'notificationOpened'
10 11
  ];

12
  let NotificationsIOS, NotificationAction, NotificationCategory;
yogevbd's avatar
yogevbd committed
13 14
  let constantGuid = 'some-random-uuid';
  let identifiers = ['some-random-uuid', 'other-random-uuid'];
yogevbd's avatar
yogevbd committed
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 55
  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 {
56
        NativeModules: {
yogevbd's avatar
yogevbd committed
57
          RNBridgeModule
58
        },
59
        NativeAppEventEmitter: {
yogevbd's avatar
yogevbd committed
60
          addListener: nativeAppAddEventListener
61 62
        },
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
63 64 65 66 67
          addListener: deviceAddEventListener
        }
      };
    });
    nativeModule = RNBridgeModule;
68

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

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

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

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

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

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

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

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

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

yogevbd's avatar
yogevbd committed
112
      expect(deviceRemoveEventListener).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

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

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

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

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

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

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

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

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

187
  });
188

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

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

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

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

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

209 210
      NotificationsIOS.backgroundTimeRemaining(someCallback);

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

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

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

      NotificationsIOS.localNotification(someLocalNotification);

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

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

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

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

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

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

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

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

    });
  });
269

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

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

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

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

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

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