index.ios.spec.js 8.62 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
  let DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
16 17 18

  beforeEach(() => {
    jest.mock('react-native', () => {
yogevbd's avatar
yogevbd committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      const RNBridgeModule = {
        requestPermissionsWithCategories: jest.fn(),
        abandonPermissions: jest.fn(),
        registerPushKit: jest.fn(),
        backgroundTimeRemaining: 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
35
      return {
36
        NativeModules: {
yogevbd's avatar
yogevbd committed
37
          RNBridgeModule
38
        },
39
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
40 41 42 43 44
          addListener: jest.fn(() => {
            return {
              remove: jest.fn()
            };
          })
yogevbd's avatar
yogevbd committed
45 46 47
        }
      };
    });
48

yogevbd's avatar
yogevbd committed
49 50
    nativeModule = require('react-native').NativeModules.RNBridgeModule;
    DeviceEventEmitter = require('react-native').DeviceEventEmitter;
yogevbd's avatar
yogevbd committed
51 52
    jest.mock('uuid', () => {
      return {
yogevbd's avatar
yogevbd committed
53
        v4: () => 'some-random-uuid'
yogevbd's avatar
yogevbd committed
54
      };
55 56
    });

yogevbd's avatar
yogevbd committed
57
    let libUnderTest = require('../lib/src/index.ios');
58
    NotificationsIOS = libUnderTest.default;
59 60
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
61 62
  });

yogevbd's avatar
yogevbd committed
63
  describe('Add Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
64 65
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
66
        NotificationsIOS.addEventListener(event, someHandler);
yogevbd's avatar
yogevbd committed
67
        expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function));
68 69 70
      });
    });

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

yogevbd's avatar
yogevbd committed
74
      expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0);
75 76 77
    });
  });

yogevbd's avatar
yogevbd committed
78
  describe('Remove Event Listener', () => {
Lidan Hifi's avatar
Lidan Hifi committed
79 80
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
yogevbd's avatar
yogevbd committed
81 82 83 84
        const removeCallback = jest.fn();
        DeviceEventEmitter.addListener.mockReturnValueOnce({
          remove: removeCallback
        });
85 86
        NotificationsIOS.addEventListener(event, someHandler);
        NotificationsIOS.removeEventListener(event, someHandler);
87

yogevbd's avatar
yogevbd committed
88
        expect(removeCallback).toHaveBeenCalledTimes(1);
89 90 91
      });
    });

yogevbd's avatar
yogevbd committed
92 93
    it('should not unsubscribe to unknown device events', () => {
      let someUnsupportedEvent = 'someUnsupportedEvent';
yogevbd's avatar
yogevbd committed
94 95 96 97 98
      const removeCallback = jest.fn();
      DeviceEventEmitter.addListener.mockReturnValueOnce({
        remove: removeCallback
      });

99 100
      NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
101

yogevbd's avatar
yogevbd committed
102
      expect(removeCallback).toHaveBeenCalledTimes(0);
103
    });
Lidan Hifi's avatar
Lidan Hifi committed
104
  });
105

yogevbd's avatar
yogevbd committed
106
  describe('Notification actions handling', () => {
107 108 109
    let someAction, someCategory;

    let actionOpts = {
yogevbd's avatar
yogevbd committed
110 111 112 113
      activationMode: 'foreground',
      title: 'someAction',
      behavior: 'default',
      identifier: 'SOME_ACTION'
114 115 116
    };

    beforeEach(() => {
117
      someAction = new NotificationAction(actionOpts, () => {});
118 119

      someCategory = new NotificationCategory({
yogevbd's avatar
yogevbd committed
120
        identifier: 'SOME_CATEGORY',
121
        actions: [someAction],
yogevbd's avatar
yogevbd committed
122
        context: 'default'
123 124 125
      });
    });

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

yogevbd's avatar
yogevbd committed
130
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([{
yogevbd's avatar
yogevbd committed
131
          identifier: 'SOME_CATEGORY',
132
          actions: [actionOpts],
yogevbd's avatar
yogevbd committed
133
          context: 'default'
134 135
        }]);
      });
136

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

yogevbd's avatar
yogevbd committed
140
        expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]);
141
      });
142
    });
143

yogevbd's avatar
yogevbd committed
144 145
    describe('get badges count', () => {
      it('should call native getBadgesCount', () => {
146 147 148
        const callback = (count) => console.log(count);
        NotificationsIOS.getBadgesCount(callback);

yogevbd's avatar
yogevbd committed
149
        expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback);
150 151 152
      });
    });

yogevbd's avatar
yogevbd committed
153 154
    describe('set badges count', () => {
      it('should call native setBadgesCount', () => {
155 156
        NotificationsIOS.setBadgesCount(44);

yogevbd's avatar
yogevbd committed
157
        expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44);
158 159 160
      });
    });

161
  });
162

yogevbd's avatar
yogevbd committed
163 164
  describe('register push kit for background notifications', function () {
    it('should call native register push kit method', function () {
165 166
      NotificationsIOS.registerPushKit();

yogevbd's avatar
yogevbd committed
167
      expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1);
168 169 170
    });
  });

yogevbd's avatar
yogevbd committed
171 172
  describe('Abandon push notifications permissions', () => {
    it('should call native abandon permissions method', () => {
173 174
      NotificationsIOS.abandonPermissions();

yogevbd's avatar
yogevbd committed
175
      expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1);
176 177 178
    });
  });

yogevbd's avatar
yogevbd committed
179 180
  describe('Get background remaining time', () => {
    it('should call native background remaining time method', () => {
yogevbd's avatar
yogevbd committed
181
      let someCallback = () => {};
182

183 184
      NotificationsIOS.backgroundTimeRemaining(someCallback);

yogevbd's avatar
yogevbd committed
185
      expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback);
186 187
    });
  });
188

yogevbd's avatar
yogevbd committed
189 190
  describe('Dispatch local notification', () => {
    it('should return generated notification guid', () => {
yogevbd's avatar
yogevbd committed
191
      expect(NotificationsIOS.localNotification({})).toEqual(constantGuid);
192 193
    });

yogevbd's avatar
yogevbd committed
194
    it('should call native local notification method with generated notification guid and notification object', () => {
195
      let someLocalNotification = {
yogevbd's avatar
WIP  
yogevbd committed
196 197
        body: 'some body',
        title: 'some title',
yogevbd's avatar
yogevbd committed
198
        alertAction: 'some action',
yogevbd's avatar
WIP  
yogevbd committed
199
        sound: 'sound',
yogevbd's avatar
yogevbd committed
200
        category: 'SOME_CATEGORY',
201
        userInfo: {
yogevbd's avatar
yogevbd committed
202
          'key': 'value'
203 204 205 206 207
        }
      };

      NotificationsIOS.localNotification(someLocalNotification);

yogevbd's avatar
yogevbd committed
208
      expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid);
209 210 211
    });
  });

yogevbd's avatar
yogevbd committed
212 213
  describe('Cancel local notification', () => {
    it('should call native cancel local notification method', () => {
214 215
      NotificationsIOS.cancelLocalNotification(constantGuid);

yogevbd's avatar
yogevbd committed
216
      expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid);
217 218 219
    });
  });

yogevbd's avatar
yogevbd committed
220 221
  describe('Cancel all local notifications', () => {
    it('should call native cancel all local notifications method', () => {
222 223
      NotificationsIOS.cancelAllLocalNotifications();

yogevbd's avatar
yogevbd committed
224
      expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith();
225 226
    });
  });
Ran Greenberg's avatar
Ran Greenberg committed
227

yogevbd's avatar
yogevbd committed
228 229
  describe('Is registered for remote notifications ', () => {
    it('should call native is registered for remote notifications', () => {
Ran Greenberg's avatar
Ran Greenberg committed
230
      NotificationsIOS.isRegisteredForRemoteNotifications();
yogevbd's avatar
yogevbd committed
231
      expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith();
Ran Greenberg's avatar
Ran Greenberg committed
232 233 234

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

yogevbd's avatar
yogevbd committed
236 237
  describe('Check permissions ', () => {
    it('should call native check permissions', () => {
Ryan Eberhardt's avatar
Ryan Eberhardt committed
238
      NotificationsIOS.checkPermissions();
yogevbd's avatar
yogevbd committed
239
      expect(nativeModule.checkPermissions).toHaveBeenCalledWith();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
240 241 242

    });
  });
243

yogevbd's avatar
yogevbd committed
244 245
  describe('Remove all delivered notifications', () => {
    it('should call native remove all delivered notifications method', () => {
246 247
      NotificationsIOS.removeAllDeliveredNotifications();

yogevbd's avatar
yogevbd committed
248
      expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith();
249 250 251
    });
  });

yogevbd's avatar
yogevbd committed
252 253
  describe('Remove delivered notifications', () => {
    it('should call native remove delivered notifications method', () => {
254 255
      NotificationsIOS.removeDeliveredNotifications(identifiers);

yogevbd's avatar
yogevbd committed
256
      expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers);
257 258 259
    });
  });

yogevbd's avatar
yogevbd committed
260 261
  describe('Get delivered notifications', () => {
    it('should call native get delivered notifications method', () => {
262 263 264
      const callback = (notifications) => console.log(notifications);
      NotificationsIOS.getDeliveredNotifications(callback);

yogevbd's avatar
yogevbd committed
265
      expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback);
266 267
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
268
});