index.android.spec.js 8.35 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
describe('Notifications-Android', () => {
d4vidi's avatar
d4vidi committed
2
  let libUnderTest;
yogevbd's avatar
yogevbd committed
3 4
  let deviceEventEmitterListenerStub;
  let WixRNNotifications;
yogevbd's avatar
yogevbd committed
5

d4vidi's avatar
d4vidi committed
6
  beforeEach(() => {
yogevbd's avatar
yogevbd committed
7 8
    jest.mock('react-native', () => {
      return {
d4vidi's avatar
d4vidi committed
9 10
        NativeModules: {
          WixRNNotifications: {
yogevbd's avatar
yogevbd committed
11 12 13 14
            refreshToken: jest.fn(),
            getInitialNotification: jest.fn(),
            postLocalNotification: jest.fn(),
            cancelLocalNotification: jest.fn()
d4vidi's avatar
d4vidi committed
15 16 17
          }
        },
        DeviceEventEmitter: {
yogevbd's avatar
yogevbd committed
18
          addListener: jest.fn()
d4vidi's avatar
d4vidi committed
19
        }
yogevbd's avatar
yogevbd committed
20
      };
d4vidi's avatar
d4vidi committed
21
    });
yogevbd's avatar
yogevbd committed
22 23 24 25

    deviceEventEmitterListenerStub = require('react-native').DeviceEventEmitter.addListener;
    WixRNNotifications = require('react-native').NativeModules.WixRNNotifications;

yogevbd's avatar
yogevbd committed
26
    libUnderTest = require('../lib/src/index.android');
d4vidi's avatar
d4vidi committed
27 28
  });

yogevbd's avatar
yogevbd committed
29 30
  describe('Registration token API', () => {
    it('should assign callback to native event upon listener registration', () => {
yogevbd's avatar
yogevbd committed
31
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
32 33 34
      const userListener = () => {};
      libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);

yogevbd's avatar
yogevbd committed
35 36
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
37 38
    });

yogevbd's avatar
yogevbd committed
39
    it('should clear native event listener upon listener deregister', () => {
yogevbd's avatar
yogevbd committed
40
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
41 42
      const userListener = () => {};
      const nativeListener = {
yogevbd's avatar
yogevbd committed
43
        remove: jest.fn()
d4vidi's avatar
d4vidi committed
44
      };
yogevbd's avatar
yogevbd committed
45
      deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
d4vidi's avatar
d4vidi committed
46 47 48 49

      libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);
      libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();

yogevbd's avatar
yogevbd committed
50
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
51 52
    });

yogevbd's avatar
yogevbd committed
53
    it('shouldn`t fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
54 55
      libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();

yogevbd's avatar
yogevbd committed
56
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
57 58 59
    });
  });

yogevbd's avatar
yogevbd committed
60 61
  describe('notification-opening API', () => {
    it('should assign callback to native event upon registration', () => {
yogevbd's avatar
yogevbd committed
62 63
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
d4vidi's avatar
d4vidi committed
64 65 66

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
67 68
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function));
d4vidi's avatar
d4vidi committed
69 70
    });

yogevbd's avatar
yogevbd committed
71
    it('should assign a wrapper-callback upon registration', () => {
yogevbd's avatar
yogevbd committed
72 73
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
yogevbd's avatar
yogevbd committed
74
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
75 76 77

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
78 79 80 81
      expect(userListenerStub).toHaveBeenCalledTimes(0);
      deviceEventEmitterListenerStub.mock.calls[0][1](notification);
      expect(userListenerStub).toHaveBeenCalledTimes(1);
      expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
d4vidi's avatar
d4vidi committed
82 83
    });

yogevbd's avatar
yogevbd committed
84
    it('should clear native event listener upon listener deregister', () => {
yogevbd's avatar
yogevbd committed
85
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
86 87
      const userListener = () => {};
      const nativeListener = {
yogevbd's avatar
yogevbd committed
88
        remove: jest.fn()
d4vidi's avatar
d4vidi committed
89
      };
yogevbd's avatar
yogevbd committed
90
      deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
d4vidi's avatar
d4vidi committed
91 92 93 94

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListener);
      libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();

yogevbd's avatar
yogevbd committed
95
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
96 97
    });

yogevbd's avatar
yogevbd committed
98
    it('shouldnt fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
99 100
      libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();

yogevbd's avatar
yogevbd committed
101
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
102 103 104
    });
  });

yogevbd's avatar
yogevbd committed
105 106
  describe('notification-receive API', () => {
    it('should assign callback to native event upon registration', () => {
yogevbd's avatar
yogevbd committed
107 108
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
d4vidi's avatar
d4vidi committed
109 110 111

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
112 113
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function));
d4vidi's avatar
d4vidi committed
114 115
    });

yogevbd's avatar
yogevbd committed
116
    it('should assign a wrapper-callback upon registration', () => {
yogevbd's avatar
yogevbd committed
117 118
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
yogevbd's avatar
yogevbd committed
119
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
120 121 122

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
123 124 125 126
      expect(userListenerStub).toHaveBeenCalledTimes(0);
      deviceEventEmitterListenerStub.mock.calls[0][1](notification);
      expect(userListenerStub).toHaveBeenCalledTimes(1);
      expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
d4vidi's avatar
d4vidi committed
127 128
    });

yogevbd's avatar
yogevbd committed
129
    it('should clear native event listener upon listener deregister', () => {
yogevbd's avatar
yogevbd committed
130
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
131 132
      const userListener = () => {};
      const nativeListener = {
yogevbd's avatar
yogevbd committed
133
        remove: jest.fn()
d4vidi's avatar
d4vidi committed
134
      };
yogevbd's avatar
yogevbd committed
135
      deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
d4vidi's avatar
d4vidi committed
136 137 138 139

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListener);
      libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();

yogevbd's avatar
yogevbd committed
140
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
141 142
    });

yogevbd's avatar
yogevbd committed
143
    it('shouldn`t fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
144 145
      libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();

yogevbd's avatar
yogevbd committed
146
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
147 148 149
    });
  });

yogevbd's avatar
yogevbd committed
150 151
  describe('Notification token', () => {
    it('should refresh notification token upon refreshing request by the user', () => {
yogevbd's avatar
yogevbd committed
152
      expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(0);
153
      libUnderTest.NotificationsAndroid.refreshToken();
yogevbd's avatar
yogevbd committed
154
      expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(1);
155
    });
d4vidi's avatar
d4vidi committed
156 157
  });

yogevbd's avatar
yogevbd committed
158 159
  describe('Initial notification API', () => {
    it('should return initial notification data if available', (done) => {
yogevbd's avatar
yogevbd committed
160
      expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
yogevbd's avatar
yogevbd committed
161
      const rawNotification = {foo: 'bar'};
yogevbd's avatar
yogevbd committed
162
      WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(rawNotification));
d4vidi's avatar
d4vidi committed
163 164 165

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
yogevbd's avatar
yogevbd committed
166
          expect(notification.getData()).toEqual(rawNotification);
d4vidi's avatar
d4vidi committed
167 168 169 170 171
          done();
        })
        .catch((err) => done(err));
    });

yogevbd's avatar
yogevbd committed
172
    it('should return empty notification if not available', (done) => {
yogevbd's avatar
yogevbd committed
173 174
      expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
      WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(null));
d4vidi's avatar
d4vidi committed
175 176 177

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
yogevbd's avatar
yogevbd committed
178
          expect(notification).toBeUndefined();
d4vidi's avatar
d4vidi committed
179 180 181 182 183 184 185
          done();
        })
        .catch((err) => done(err));
    });

  });

yogevbd's avatar
yogevbd committed
186
  describe('Local notification', () => {
187
    const notification = {
yogevbd's avatar
yogevbd committed
188 189
      title: 'notification-title',
      body: 'notification-body'
190 191
    };

yogevbd's avatar
yogevbd committed
192
    it('should get published when posted manually', () => {
yogevbd's avatar
yogevbd committed
193
      expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0);
194 195

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
yogevbd's avatar
yogevbd committed
196
      expect(id).toBeDefined();
yogevbd's avatar
yogevbd committed
197
      expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledWith(notification, id);
198 199
    });

yogevbd's avatar
yogevbd committed
200
    it('should be called with a unique ID', () => {
yogevbd's avatar
yogevbd committed
201
      expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0);
202 203 204

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
      const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
yogevbd's avatar
yogevbd committed
205 206 207
      expect(id).toBeDefined();
      expect(id2).toBeDefined();
      expect(id).not.toBe(id2);
208 209
    });

yogevbd's avatar
yogevbd committed
210
    it('should be cancellable with an ID', () => {
yogevbd's avatar
yogevbd committed
211
      expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledTimes(0);
212 213 214

      libUnderTest.NotificationsAndroid.cancelLocalNotification(666);

yogevbd's avatar
yogevbd committed
215
      expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledWith(666);
216 217
    });
  });
d4vidi's avatar
d4vidi committed
218
});