index.android.spec.js 8.54 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2 3 4
'use strict';
let expect = require('chai').use(require('sinon-chai')).expect;
import proxyquire from 'proxyquire';
import sinon from 'sinon';
d4vidi's avatar
d4vidi committed
5

yogevbd's avatar
yogevbd committed
6
describe('Notifications-Android > ', () => {
d4vidi's avatar
d4vidi committed
7 8 9 10
  proxyquire.noCallThru();

  let refreshTokenStub;
  let getInitialNotificationStub;
11 12
  let postLocalNotificationStub;
  let cancelLocalNotificationStub;
d4vidi's avatar
d4vidi committed
13 14 15 16 17
  let deviceEventEmitterListenerStub;
  let libUnderTest;
  beforeEach(() => {
    refreshTokenStub = sinon.stub();
    getInitialNotificationStub = sinon.stub();
18 19
    postLocalNotificationStub = sinon.stub();
    cancelLocalNotificationStub = sinon.stub();
d4vidi's avatar
d4vidi committed
20 21
    deviceEventEmitterListenerStub = sinon.stub();

yogevbd's avatar
yogevbd committed
22 23
    libUnderTest = proxyquire('../index.android', {
      'react-native': {
d4vidi's avatar
d4vidi committed
24 25 26
        NativeModules: {
          WixRNNotifications: {
            refreshToken: refreshTokenStub,
27 28 29
            getInitialNotification: getInitialNotificationStub,
            postLocalNotification: postLocalNotificationStub,
            cancelLocalNotification: cancelLocalNotificationStub
d4vidi's avatar
d4vidi committed
30 31 32 33 34 35
          }
        },
        DeviceEventEmitter: {
          addListener: deviceEventEmitterListenerStub
        }
      },
yogevbd's avatar
yogevbd committed
36
      './notification': require('../notification.android')
d4vidi's avatar
d4vidi committed
37 38 39
    });
  });

yogevbd's avatar
yogevbd committed
40 41
  describe('Registration token API', () => {
    it('should assign callback to native event upon listener registration', () => {
d4vidi's avatar
d4vidi committed
42 43 44 45 46
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListener = () => {};

      libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);

yogevbd's avatar
yogevbd committed
47
      expect(deviceEventEmitterListenerStub).to.have.been.calledWith('remoteNotificationsRegistered', userListener);
d4vidi's avatar
d4vidi committed
48 49 50
      expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
    });

yogevbd's avatar
yogevbd committed
51
    it('should clear native event listener upon listener deregister', () => {
d4vidi's avatar
d4vidi committed
52 53 54 55 56 57 58 59 60 61 62 63 64
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListener = () => {};
      const nativeListener = {
        remove: sinon.spy()
      };
      deviceEventEmitterListenerStub.returns(nativeListener);

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

      expect(nativeListener.remove).to.have.been.calledOnce;
    });

yogevbd's avatar
yogevbd committed
65
    it('shouldn`t fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
66 67 68 69 70 71
      libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();

      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
    });
  });

yogevbd's avatar
yogevbd committed
72 73
  describe('notification-opening API', () => {
    it('should assign callback to native event upon registration', () => {
d4vidi's avatar
d4vidi committed
74 75 76 77 78 79
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListenerStub = sinon.stub();

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

      expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
yogevbd's avatar
yogevbd committed
80
      expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationOpened', sinon.match.func);
d4vidi's avatar
d4vidi committed
81 82
    });

yogevbd's avatar
yogevbd committed
83
    it('should assign a wrapper-callback upon registration', () => {
d4vidi's avatar
d4vidi committed
84 85
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListenerStub = sinon.stub();
yogevbd's avatar
yogevbd committed
86
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
87 88 89 90 91 92 93 94 95

      libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

      expect(userListenerStub).to.not.have.been.called;
      deviceEventEmitterListenerStub.args[0][1](notification);
      expect(userListenerStub).to.have.been.calledOnce;
      expect(userListenerStub.args[0][0].getData()).to.equal(notification);
    });

yogevbd's avatar
yogevbd committed
96
    it('should clear native event listener upon listener deregister', () => {
d4vidi's avatar
d4vidi committed
97 98 99 100 101 102 103 104 105 106 107 108 109
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListener = () => {};
      const nativeListener = {
        remove: sinon.spy()
      };
      deviceEventEmitterListenerStub.returns(nativeListener);

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

      expect(nativeListener.remove).to.have.been.calledOnce;
    });

yogevbd's avatar
yogevbd committed
110
    it('shouldnt fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
111 112 113 114 115 116
      libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();

      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
    });
  });

yogevbd's avatar
yogevbd committed
117 118
  describe('notification-receive API', () => {
    it('should assign callback to native event upon registration', () => {
d4vidi's avatar
d4vidi committed
119 120 121 122 123 124
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListenerStub = sinon.stub();

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

      expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
yogevbd's avatar
yogevbd committed
125
      expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationReceived', sinon.match.func);
d4vidi's avatar
d4vidi committed
126 127
    });

yogevbd's avatar
yogevbd committed
128
    it('should assign a wrapper-callback upon registration', () => {
d4vidi's avatar
d4vidi committed
129 130
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListenerStub = sinon.stub();
yogevbd's avatar
yogevbd committed
131
      const notification = { foo: 'bar' };
d4vidi's avatar
d4vidi committed
132 133 134 135 136 137 138 139 140

      libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

      expect(userListenerStub).to.not.have.been.called;
      deviceEventEmitterListenerStub.args[0][1](notification);
      expect(userListenerStub).to.have.been.calledOnce;
      expect(userListenerStub.args[0][0].getData()).to.equal(notification);
    });

yogevbd's avatar
yogevbd committed
141
    it('should clear native event listener upon listener deregister', () => {
d4vidi's avatar
d4vidi committed
142 143 144 145 146 147 148 149 150 151 152 153 154
      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
      const userListener = () => {};
      const nativeListener = {
        remove: sinon.spy()
      };
      deviceEventEmitterListenerStub.returns(nativeListener);

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

      expect(nativeListener.remove).to.have.been.calledOnce;
    });

yogevbd's avatar
yogevbd committed
155
    it('shouldn`t fail if deregister without registering', () => {
d4vidi's avatar
d4vidi committed
156 157 158 159 160 161
      libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();

      expect(deviceEventEmitterListenerStub).to.not.have.been.called;
    });
  });

yogevbd's avatar
yogevbd committed
162 163
  describe('Notification token', () => {
    it('should refresh notification token upon refreshing request by the user', () => {
164 165 166 167
      expect(refreshTokenStub).to.not.have.been.called;
      libUnderTest.NotificationsAndroid.refreshToken();
      expect(refreshTokenStub).to.have.been.calledOnce;
    });
d4vidi's avatar
d4vidi committed
168 169
  });

yogevbd's avatar
yogevbd committed
170 171
  describe('Initial notification API', () => {
    it('should return initial notification data if available', (done) => {
d4vidi's avatar
d4vidi committed
172
      expect(getInitialNotificationStub).to.not.have.been.called;
yogevbd's avatar
yogevbd committed
173
      const rawNotification = {foo: 'bar'};
d4vidi's avatar
d4vidi committed
174 175 176 177 178 179 180 181 182 183
      getInitialNotificationStub.returns(Promise.resolve(rawNotification));

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
          expect(notification.getData()).to.equal(rawNotification);
          done();
        })
        .catch((err) => done(err));
    });

yogevbd's avatar
yogevbd committed
184
    it('should return empty notification if not available', (done) => {
d4vidi's avatar
d4vidi committed
185 186 187 188 189
      expect(getInitialNotificationStub).to.not.have.been.called;
      getInitialNotificationStub.returns(Promise.resolve(null));

      libUnderTest.PendingNotifications.getInitialNotification()
        .then((notification) => {
Amit Davidi's avatar
Amit Davidi committed
190
          expect(notification).to.be.undefined;
d4vidi's avatar
d4vidi committed
191 192 193 194 195 196 197
          done();
        })
        .catch((err) => done(err));
    });

  });

yogevbd's avatar
yogevbd committed
198
  describe('Local notification', () => {
199
    const notification = {
yogevbd's avatar
yogevbd committed
200 201
      title: 'notification-title',
      body: 'notification-body'
202 203
    };

yogevbd's avatar
yogevbd committed
204
    it('should get published when posted manually', () => {
205 206 207 208 209 210 211
      expect(postLocalNotificationStub).to.not.have.been.called;

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
      expect(id).to.not.be.undefined;
      expect(postLocalNotificationStub).to.have.been.calledWith(notification, id);
    });

yogevbd's avatar
yogevbd committed
212
    it('should be called with a unique ID', () => {
213 214 215 216 217 218 219 220 221
      expect(postLocalNotificationStub).to.not.have.been.called;

      const id = libUnderTest.NotificationsAndroid.localNotification(notification);
      const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
      expect(id).to.not.be.undefined;
      expect(id2).to.not.be.undefined;
      expect(id).to.not.equal(id2);
    });

yogevbd's avatar
yogevbd committed
222
    it('should be cancellable with an ID', () => {
223 224 225 226 227 228 229
      expect(cancelLocalNotificationStub).to.not.have.been.called;

      libUnderTest.NotificationsAndroid.cancelLocalNotification(666);

      expect(cancelLocalNotificationStub).to.have.been.calledWith(666);
    });
  });
d4vidi's avatar
d4vidi committed
230
});