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

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

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

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

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

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

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  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
71
  describe('abandonPermissions', () => {
72
    it('sends to native', () => {
yogevbd's avatar
yogevbd committed
73 74 75 76 77
      uut.abandonPermissions();
      verify(mockedNativeCommandsSender.abandonPermissions()).called();
    });
  });

yogevbd's avatar
yogevbd committed
78
  describe('postLocalNotification', () => {
79
    it('sends to native', () => {
80
      const notification: Notification = new Notification({identifier: 'id'});
yogevbd's avatar
yogevbd committed
81 82
      uut.postLocalNotification(notification);
      verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
83 84 85
    });

    it('generates unique identifier', () => {
86
      const notification: Notification = new Notification({identifier: 'id'});
yogevbd's avatar
yogevbd committed
87 88
      uut.postLocalNotification(notification);
      verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
89 90 91
    });

    it('use passed notification id', () => {
92
      const notification: Notification = new Notification({identifier: 'id'});
yogevbd's avatar
yogevbd committed
93 94 95
      const passedId: number = 2;
      uut.postLocalNotification(notification, passedId);
      verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
yogevbd's avatar
yogevbd committed
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 179 180
  
  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
181 182 183 184 185 186 187

  describe('getDeliveredNotifications', () => {
    it('sends to native', () => {
      uut.getDeliveredNotifications();
      verify(mockedNativeCommandsSender.getDeliveredNotifications()).called();
    });
  });
yogevbd's avatar
yogevbd committed
188
});