index.android.spec.js 8.4 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
describe('Notifications-Android', () => {
d4vidi's avatar
d4vidi committed
2 3
  let refreshTokenStub;
  let getInitialNotificationStub;
4 5
  let postLocalNotificationStub;
  let cancelLocalNotificationStub;
d4vidi's avatar
d4vidi committed
6 7
  let deviceEventEmitterListenerStub;
  let libUnderTest;
yogevbd's avatar
yogevbd committed
8

d4vidi's avatar
d4vidi committed
9
  beforeEach(() => {
yogevbd's avatar
yogevbd committed
10 11 12 13 14 15 16 17
    refreshTokenStub = jest.fn();
    getInitialNotificationStub = jest.fn();
    postLocalNotificationStub = jest.fn();
    cancelLocalNotificationStub = jest.fn();
    deviceEventEmitterListenerStub = jest.fn();

    jest.mock('react-native', () => {
      return {
d4vidi's avatar
d4vidi committed
18 19 20
        NativeModules: {
          WixRNNotifications: {
            refreshToken: refreshTokenStub,
21 22 23
            getInitialNotification: getInitialNotificationStub,
            postLocalNotification: postLocalNotificationStub,
            cancelLocalNotification: cancelLocalNotificationStub
d4vidi's avatar
d4vidi committed
24 25 26 27 28
          }
        },
        DeviceEventEmitter: {
          addListener: deviceEventEmitterListenerStub
        }
yogevbd's avatar
yogevbd committed
29
      };
d4vidi's avatar
d4vidi committed
30
    });
yogevbd's avatar
yogevbd committed
31
    libUnderTest = require('../lib/src/index.android');
d4vidi's avatar
d4vidi committed
32 33
  });

yogevbd's avatar
yogevbd committed
34 35
  describe('Registration token API', () => {
    it('should assign callback to native event upon listener registration', () => {
yogevbd's avatar
yogevbd committed
36
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
37 38 39
      const userListener = () => {};
      libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);

yogevbd's avatar
yogevbd committed
40 41
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
42 43
    });

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

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

yogevbd's avatar
yogevbd committed
55
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
56 57
    });

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

yogevbd's avatar
yogevbd committed
61
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
62 63 64
    });
  });

yogevbd's avatar
yogevbd committed
65 66
  describe('notification-opening API', () => {
    it('should assign callback to native event upon registration', () => {
yogevbd's avatar
yogevbd committed
67 68
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
d4vidi's avatar
d4vidi committed
69 70 71

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
72 73
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function));
d4vidi's avatar
d4vidi committed
74 75
    });

yogevbd's avatar
yogevbd committed
76
    it('should assign a wrapper-callback upon registration', () => {
yogevbd's avatar
yogevbd committed
77 78
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
yogevbd's avatar
yogevbd committed
79
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
80 81 82

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
83 84 85 86
      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
87 88
    });

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

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

yogevbd's avatar
yogevbd committed
100
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
101 102
    });

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

yogevbd's avatar
yogevbd committed
106
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
107 108 109
    });
  });

yogevbd's avatar
yogevbd committed
110 111
  describe('notification-receive API', () => {
    it('should assign callback to native event upon registration', () => {
yogevbd's avatar
yogevbd committed
112 113
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
d4vidi's avatar
d4vidi committed
114 115 116

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
117 118
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function));
d4vidi's avatar
d4vidi committed
119 120
    });

yogevbd's avatar
yogevbd committed
121
    it('should assign a wrapper-callback upon registration', () => {
yogevbd's avatar
yogevbd committed
122 123
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
      const userListenerStub = jest.fn();
yogevbd's avatar
yogevbd committed
124
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
125 126 127

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

yogevbd's avatar
yogevbd committed
128 129 130 131
      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
132 133
    });

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

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

yogevbd's avatar
yogevbd committed
145
      expect(nativeListener.remove).toHaveBeenCalledTimes(1);
d4vidi's avatar
d4vidi committed
146 147
    });

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

yogevbd's avatar
yogevbd committed
151
      expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
d4vidi's avatar
d4vidi committed
152 153 154
    });
  });

yogevbd's avatar
yogevbd committed
155 156
  describe('Notification token', () => {
    it('should refresh notification token upon refreshing request by the user', () => {
yogevbd's avatar
yogevbd committed
157
      expect(refreshTokenStub).toHaveBeenCalledTimes(0);
158
      libUnderTest.NotificationsAndroid.refreshToken();
yogevbd's avatar
yogevbd committed
159
      expect(refreshTokenStub).toHaveBeenCalledTimes(1);
160
    });
d4vidi's avatar
d4vidi committed
161 162
  });

yogevbd's avatar
yogevbd committed
163 164
  describe('Initial notification API', () => {
    it('should return initial notification data if available', (done) => {
yogevbd's avatar
yogevbd committed
165
      expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
yogevbd's avatar
yogevbd committed
166
      const rawNotification = {foo: 'bar'};
yogevbd's avatar
yogevbd committed
167
      getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(rawNotification));
d4vidi's avatar
d4vidi committed
168 169 170

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
yogevbd's avatar
yogevbd committed
171
          expect(notification.getData()).toEqual(rawNotification);
d4vidi's avatar
d4vidi committed
172 173 174 175 176
          done();
        })
        .catch((err) => done(err));
    });

yogevbd's avatar
yogevbd committed
177
    it('should return empty notification if not available', (done) => {
yogevbd's avatar
yogevbd committed
178 179
      expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
      getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null));
d4vidi's avatar
d4vidi committed
180 181 182

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
yogevbd's avatar
yogevbd committed
183
          expect(notification).toBeUndefined();
d4vidi's avatar
d4vidi committed
184 185 186 187 188 189 190
          done();
        })
        .catch((err) => done(err));
    });

  });

yogevbd's avatar
yogevbd committed
191
  describe('Local notification', () => {
192
    const notification = {
yogevbd's avatar
yogevbd committed
193 194
      title: 'notification-title',
      body: 'notification-body'
195 196
    };

yogevbd's avatar
yogevbd committed
197
    it('should get published when posted manually', () => {
yogevbd's avatar
yogevbd committed
198
      expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);
199 200

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
yogevbd's avatar
yogevbd committed
201 202
      expect(id).toBeDefined();
      expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id);
203 204
    });

yogevbd's avatar
yogevbd committed
205
    it('should be called with a unique ID', () => {
yogevbd's avatar
yogevbd committed
206
      expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);
207 208 209

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
      const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
yogevbd's avatar
yogevbd committed
210 211 212
      expect(id).toBeDefined();
      expect(id2).toBeDefined();
      expect(id).not.toBe(id2);
213 214
    });

yogevbd's avatar
yogevbd committed
215
    it('should be cancellable with an ID', () => {
yogevbd's avatar
yogevbd committed
216
      expect(cancelLocalNotificationStub).toHaveBeenCalledTimes(0);
217 218 219

      libUnderTest.NotificationsAndroid.cancelLocalNotification(666);

yogevbd's avatar
yogevbd committed
220
      expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666);
221 222
    });
  });
d4vidi's avatar
d4vidi committed
223
});