index.android.spec.js 8.43 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
      const userListener = () => {};
yogevbd's avatar
yogevbd committed
38
      console.log(libUnderTest);
d4vidi's avatar
d4vidi committed
39 40
      libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);

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

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

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

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

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

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

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

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

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

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

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

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

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

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

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

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

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

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

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

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

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

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

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

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

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

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

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

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

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

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

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

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

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

  });

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

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

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

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

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

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

      libUnderTest.NotificationsAndroid.cancelLocalNotification(666);

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