index.ios.spec.js 4.29 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1
"use strict";
2
let expect = require("chai").use(require("sinon-chai")).expect;
3 4
import proxyquire from "proxyquire";
import sinon from "sinon";
Lidan Hifi's avatar
Lidan Hifi committed
5

6 7
/* eslint-disable no-unused-vars */

Lidan Hifi's avatar
Lidan Hifi committed
8
describe("NotificationsIOS", () => {
9 10 11 12 13 14
  let deviceEvents = [
    "notificationReceivedForeground",
    "notificationReceivedBackground",
    "notificationOpened"
  ];

15 16
  let nativeAddEventListener, nativeRemoveEventListener, nativeUpdateNotificationCategories;
  let NotificationIOS, NotificationAction, NotificationCategory;
17 18 19
  let someHandler = () => {};

  before(() => {
20 21 22 23 24
    nativeAddEventListener = sinon.spy();
    nativeRemoveEventListener = sinon.spy();
    nativeUpdateNotificationCategories = sinon.spy();

    let libUnderTest = proxyquire("../index.ios", {
25 26
      "react-native": {
        NativeModules: {
27 28 29
          RNNotifications: {
            updateNotificationCategories: nativeUpdateNotificationCategories
          }
30 31 32
        },
        DeviceEventEmitter: {
          addListener: (...args) => {
33
            nativeAddEventListener(...args);
34

35
            return { remove: nativeRemoveEventListener };
36 37 38 39
          }
        },
        "@noCallThru": true
      }
40 41 42 43 44
    });

    NotificationIOS = libUnderTest.default;
    NotificationAction = libUnderTest.NotificationAction;
    NotificationCategory = libUnderTest.NotificationCategory;
45 46 47
  });

  afterEach(() => {
48 49 50
    nativeAddEventListener.reset();
    nativeRemoveEventListener.reset();
    nativeUpdateNotificationCategories.reset();
51 52 53
  });

  after(() => {
54 55 56 57 58 59
    nativeAddEventListener = null;
    nativeRemoveEventListener = null;
    nativeUpdateNotificationCategories = null;
    NotificationIOS = null;
    NotificationAction = null;
    NotificationCategory = null;
60 61
  });

Lidan Hifi's avatar
Lidan Hifi committed
62 63 64
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
65
        NotificationIOS.addEventListener(event, someHandler);
66

67
        expect(nativeAddEventListener).to.have.been.calledWith(event, sinon.match.func);
68 69 70
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
71
    it("should not subscribe to unknown device events", () => {
72
      NotificationIOS.addEventListener("someUnsupportedEvent", someHandler);
73

74
      expect(nativeAddEventListener).to.not.have.been.called;
75 76 77
    });
  });

Lidan Hifi's avatar
Lidan Hifi committed
78 79 80
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
81 82
        NotificationIOS.addEventListener(event, someHandler);
        NotificationIOS.removeEventListener(event, someHandler);
83

84
        expect(nativeRemoveEventListener).to.have.been.calledOnce;
85 86 87
      });
    });

Lidan Hifi's avatar
Lidan Hifi committed
88
    it("should not unsubscribe to unknown device events", () => {
89
      let someUnsupportedEvent = "someUnsupportedEvent";
90 91
      NotificationIOS.addEventListener(someUnsupportedEvent, someHandler);
      NotificationIOS.removeEventListener(someUnsupportedEvent, someHandler);
92

93
      expect(nativeRemoveEventListener).to.not.have.been.called;
94
    });
Lidan Hifi's avatar
Lidan Hifi committed
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
  describe("Update notification categories", () => {
    let someAction, someCategory;

    let actionOpts = {
      activationMode: "foreground",
      title: "someAction",
      behavior: "default",
      identifier: "SOME_ACTION"
    };

    beforeEach(() => {
      someAction = new NotificationAction(actionOpts, () => {});

      someCategory = new NotificationCategory({
        identifier: "SOME_CATEGORY",
        actions: [someAction],
        context: "default"
      });
    });

    it("should call native update categories with array of categories", () => {
      NotificationIOS.setCategories([someCategory]);

      expect(nativeUpdateNotificationCategories).to.have.been.calledWith([{
        identifier: "SOME_CATEGORY",
        actions: [actionOpts],
        context: "default"
      }]);
    });

    it("should call native update categories with empty array if no categories specified", () => {
      NotificationIOS.setCategories();

      expect(nativeUpdateNotificationCategories).to.have.been.calledWith([]);
    });

    it("should subscribe to 'notificationActionReceived' event for each action identifier", () => {
      NotificationIOS.setCategories([someCategory]);

      expect(nativeAddEventListener).to.have.been.calledOnce;
      expect(nativeAddEventListener).to.have.been.calledWith("notificationActionReceived", sinon.match.func);
    });
  });
Lidan Hifi's avatar
Lidan Hifi committed
140
});