Commands.test.js 7.46 KB
Newer Older
冷佳娟's avatar
冷佳娟 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts_mockito_1 = require("ts-mockito");
const Commands_1 = require("./Commands");
const NativeCommandsSender_1 = require("../adapters/NativeCommandsSender");
const Notification_1 = require("../DTO/Notification");
const UniqueIdProvider_1 = require("../adapters/UniqueIdProvider");
describe('Commands', () => {
    let uut;
    let mockedNativeCommandsSender;
    let mockedUniqueIdProvider;
    beforeEach(() => {
        mockedNativeCommandsSender = ts_mockito_1.mock(NativeCommandsSender_1.NativeCommandsSender);
        mockedUniqueIdProvider = ts_mockito_1.mock(UniqueIdProvider_1.UniqueIdProvider);
        ts_mockito_1.when(mockedUniqueIdProvider.generate()).thenCall(() => 12);
        uut = new Commands_1.Commands(ts_mockito_1.instance(mockedNativeCommandsSender), ts_mockito_1.instance(mockedUniqueIdProvider));
    });
    describe('getInitialNotification', () => {
        it('sends to native', () => {
            uut.getInitialNotification();
            ts_mockito_1.verify(mockedNativeCommandsSender.getInitialNotification()).called();
        });
        it('returns a promise with the initial notification', async () => {
            const expectedNotification = new Notification_1.Notification({ identifier: 'id' });
            ts_mockito_1.when(mockedNativeCommandsSender.getInitialNotification()).thenResolve({ identifier: 'id' });
            const result = await uut.getInitialNotification();
            expect(result).toEqual(expectedNotification);
        });
    });
    describe('requestPermissions', () => {
        it('sends to native', () => {
            uut.requestPermissions();
            ts_mockito_1.verify(mockedNativeCommandsSender.requestPermissions()).called();
        });
    });
    describe('registerPushKit', () => {
        it('sends to native', () => {
            uut.registerPushKit();
            ts_mockito_1.verify(mockedNativeCommandsSender.registerPushKit()).called();
        });
    });
    describe('setCategories', () => {
        it('sends to native', () => {
            const emptyCategoriesArray = [];
            uut.setCategories(emptyCategoriesArray);
            ts_mockito_1.verify(mockedNativeCommandsSender.setCategories(emptyCategoriesArray)).called();
        });
        it('sends to native with categories', () => {
            const category = { identifier: 'id', actions: [] };
            const categoriesArray = [category];
            uut.setCategories(categoriesArray);
            ts_mockito_1.verify(mockedNativeCommandsSender.setCategories(categoriesArray)).called();
        });
    });
    describe('abandonPermissions', () => {
        it('sends to native', () => {
            uut.abandonPermissions();
            ts_mockito_1.verify(mockedNativeCommandsSender.abandonPermissions()).called();
        });
    });
    describe('postLocalNotification', () => {
        it('sends to native', () => {
            const notification = new Notification_1.Notification({ identifier: 'id' });
            uut.postLocalNotification(notification);
            ts_mockito_1.verify(mockedNativeCommandsSender.postLocalNotification(notification, ts_mockito_1.anyNumber())).called();
        });
        it('generates unique identifier', () => {
            const notification = new Notification_1.Notification({ identifier: 'id' });
            uut.postLocalNotification(notification);
            ts_mockito_1.verify(mockedNativeCommandsSender.postLocalNotification(notification, ts_mockito_1.anyNumber())).called();
        });
        it('use passed notification id', () => {
            const notification = new Notification_1.Notification({ identifier: 'id' });
            const passedId = 2;
            uut.postLocalNotification(notification, passedId);
            ts_mockito_1.verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
        });
    });
    describe('getBadgeCount', () => {
        it('sends to native', () => {
            uut.getBadgeCount();
            ts_mockito_1.verify(mockedNativeCommandsSender.getBadgeCount()).called();
        });
    });
    describe('setBadgeCount', () => {
        it('sends to native', () => {
            uut.setBadgeCount(10);
            ts_mockito_1.verify(mockedNativeCommandsSender.setBadgeCount(10)).called();
        });
    });
    describe('cancelLocalNotification', () => {
        it('sends to native', () => {
            uut.cancelLocalNotification("notificationId");
            ts_mockito_1.verify(mockedNativeCommandsSender.cancelLocalNotification("notificationId")).called();
        });
    });
    describe('cancelAllLocalNotifications', () => {
        it('sends to native', () => {
            uut.cancelAllLocalNotifications();
            ts_mockito_1.verify(mockedNativeCommandsSender.cancelAllLocalNotifications()).called();
        });
    });
    describe('isRegisteredForRemoteNotifications', () => {
        it('sends to native', () => {
            uut.isRegisteredForRemoteNotifications();
            ts_mockito_1.verify(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).called();
        });
        it('return positive response from native', async () => {
            ts_mockito_1.when(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).thenResolve(true);
            const isRegistered = await uut.isRegisteredForRemoteNotifications();
            ts_mockito_1.verify(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).called();
            expect(isRegistered).toEqual(true);
        });
        it('return negative response from native', async () => {
            ts_mockito_1.when(mockedNativeCommandsSender.isRegisteredForRemoteNotifications()).thenResolve(false);
            const isRegistered = await uut.isRegisteredForRemoteNotifications();
            expect(isRegistered).toEqual(false);
        });
    });
    describe('checkPermissions', () => {
        it('sends to native', () => {
            uut.checkPermissions();
            ts_mockito_1.verify(mockedNativeCommandsSender.checkPermissions()).called();
        });
        it('return negative response from native', async () => {
            const expectedPermissions = { badge: false, alert: true, sound: false };
            ts_mockito_1.when(mockedNativeCommandsSender.checkPermissions()).thenResolve(expectedPermissions);
            const permissions = await uut.checkPermissions();
            expect(permissions).toEqual(expectedPermissions);
        });
    });
    describe('removeAllDeliveredNotifications', () => {
        it('sends to native', () => {
            uut.removeAllDeliveredNotifications();
            ts_mockito_1.verify(mockedNativeCommandsSender.removeAllDeliveredNotifications()).called();
        });
    });
    describe('removeDeliveredNotifications', async () => {
        it('sends to native', () => {
            const identifiers = ["id1", "id2"];
            uut.removeDeliveredNotifications(identifiers);
            ts_mockito_1.verify(mockedNativeCommandsSender.removeDeliveredNotifications(identifiers)).called();
        });
    });
    describe('getDeliveredNotifications', () => {
        it('sends to native', () => {
            uut.getDeliveredNotifications();
            ts_mockito_1.verify(mockedNativeCommandsSender.getDeliveredNotifications()).called();
        });
    });
    describe('refreshToken', () => {
        it('sends to native', () => {
            uut.refreshToken();
            ts_mockito_1.verify(mockedNativeCommandsSender.refreshToken()).called();
        });
    });
});