Commands.test.ts 6.33 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2 3 4 5
import * as _ from 'lodash';
import { mock, verify, instance, deepEqual, when, anything, anyString } from 'ts-mockito';

import { Commands } from './Commands';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
6
import { Notification, NotificationCategory, NotificationPermissions } from '../interfaces/Notification';
7
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
yogevbd's avatar
yogevbd committed
8 9 10 11

describe('Commands', () => {
  let uut: Commands;
  let mockedNativeCommandsSender: NativeCommandsSender;
12 13
  let mockedUniqueIdProvider: UniqueIdProvider;
  
yogevbd's avatar
yogevbd committed
14 15
  beforeEach(() => {
    mockedNativeCommandsSender = mock(NativeCommandsSender);
16 17
    mockedUniqueIdProvider = mock(UniqueIdProvider);
    when(mockedUniqueIdProvider.generate(anything())).thenCall((prefix) => `${prefix}+UNIQUE_ID`);
yogevbd's avatar
yogevbd committed
18
    uut = new Commands(
19 20
      instance(mockedNativeCommandsSender),
      instance(mockedUniqueIdProvider)
yogevbd's avatar
yogevbd committed
21 22 23 24
    );
  });

  describe('getInitialNotification', () => {
25
    it('sends to native', () => {
yogevbd's avatar
yogevbd committed
26 27 28 29 30
      uut.getInitialNotification();
      verify(mockedNativeCommandsSender.getInitialNotification()).called();
    });

    it('returns a promise with the initial notification', async () => {
31
      const expectedNotification: Notification = {data: {}, alert: 'alert'};
yogevbd's avatar
yogevbd committed
32
      when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
33
        expectedNotification
yogevbd's avatar
yogevbd committed
34 35
      );
      const result = await uut.getInitialNotification();
36
      expect(result).toEqual(expectedNotification);
yogevbd's avatar
yogevbd committed
37 38 39 40
    });
  });

  describe('requestPermissions', () => {
41
    it('sends to native', () => {
yogevbd's avatar
yogevbd committed
42 43 44 45 46
      uut.requestPermissions();
      verify(mockedNativeCommandsSender.requestPermissions()).called();
    });
  });

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  describe('registerPushKit', () => {
    it('sends to native', () => {
      uut.registerPushKit();
      verify(mockedNativeCommandsSender.registerPushKit()).called();
    });
  });

  describe('setCategories', () => {
    it('sends to native', () => {
      const emptyCategoriesArray: [NotificationCategory?] = [];
      uut.setCategories(emptyCategoriesArray);
      verify(mockedNativeCommandsSender.setCategories(emptyCategoriesArray)).called();
    });

    it('sends to native with categories', () => {
      const category: NotificationCategory = {identifier: 'id', actions: []};
      const categoriesArray: [NotificationCategory] = [category];
      uut.setCategories(categoriesArray);
      verify(mockedNativeCommandsSender.setCategories(categoriesArray)).called();
    });
  });

yogevbd's avatar
yogevbd committed
69
  describe('abandonPermissions', () => {
70
    it('sends to native', () => {
yogevbd's avatar
yogevbd committed
71 72 73 74 75 76
      uut.abandonPermissions();
      verify(mockedNativeCommandsSender.abandonPermissions()).called();
    });
  });

  describe('sendLocalNotification', () => {
77
    it('sends to native', () => {
yogevbd's avatar
yogevbd committed
78 79
      const notification: Notification = {data: {}, alert: 'alert'};
      uut.sendLocalNotification(notification);
80 81 82 83 84 85 86 87 88 89 90 91 92 93
      verify(mockedNativeCommandsSender.sendLocalNotification(notification, anyString())).called();
    });

    it('generates unique identifier', () => {
      const notification: Notification = {data: {}, alert: 'alert'};
      uut.sendLocalNotification(notification);
      verify(mockedNativeCommandsSender.sendLocalNotification(notification, `Notification_+UNIQUE_ID`)).called();
    });

    it('use passed notification id', () => {
      const notification: Notification = {data: {}, alert: 'alert'};
      const passedId: string = "passedId";
      uut.sendLocalNotification(notification, passedId);
      verify(mockedNativeCommandsSender.sendLocalNotification(notification, passedId)).called();
yogevbd's avatar
yogevbd committed
94 95
    });
  });
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  
  describe('getBadgeCount', () => {
    it('sends to native', () => {
      uut.getBadgeCount();
      verify(mockedNativeCommandsSender.getBadgeCount()).called();
    });
  });

  describe('setBadgeCount', () => {
    it('sends to native', () => {
      uut.setBadgeCount(10);
      verify(mockedNativeCommandsSender.setBadgeCount(10)).called();
    });
  });

  describe('cancelLocalNotification', () => {
    it('sends to native', () => {
      uut.cancelLocalNotification("notificationId");
      verify(mockedNativeCommandsSender.cancelLocalNotification("notificationId")).called();
    });
  });

  describe('cancelAllLocalNotifications', () => {
    it('sends to native', () => {
      uut.cancelAllLocalNotifications();
      verify(mockedNativeCommandsSender.cancelAllLocalNotifications()).called();
    });
  });

  describe('isRegisteredForRemoteNotifications', () => {
    it('sends to native', () => {
      uut.isRegisteredForRemoteNotifications();
      verify(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).called();
    });

    it('return positive response from native', async () => {
      when(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).thenResolve(
        true
      );
      const isRegistered = await uut.isRegisteredForRemoteNotifications();
      verify(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).called();
      expect(isRegistered).toEqual(true);
    });

    it('return negative response from native', async () => {
      when(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).thenResolve(
        false
      );
      const isRegistered = await uut.isRegisteredForRemoteNotifications();
      expect(isRegistered).toEqual(false);
    });
  });
  
  describe('checkPermissions', () => {
    it('sends to native', () => {
      uut.checkPermissions();
      verify(mockedNativeCommandsSender.checkPermissions()).called();
    });

    it('return negative response from native', async () => {
      const expectedPermissions: NotificationPermissions = {badge: false, alert: true, sound: false};
      when(mockedNativeCommandsSender.checkPermissions()).thenResolve(
        expectedPermissions
      );
      const permissions = await uut.checkPermissions();
      expect(permissions).toEqual(expectedPermissions);
    });
  });

  describe('removeAllDeliveredNotifications', () => {
    it('sends to native', () => {
      uut.removeAllDeliveredNotifications();
      verify(mockedNativeCommandsSender.removeAllDeliveredNotifications()).called();
    });
  });

  describe('removeDeliveredNotifications', async () => {
    it('sends to native', () => {
      const identifiers: Array<string> = ["id1", "id2"];
      uut.removeDeliveredNotifications(identifiers);
      verify(mockedNativeCommandsSender.removeDeliveredNotifications(identifiers)).called();
    });
  });
yogevbd's avatar
yogevbd committed
179
});