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

describe("NotificationsIOS", function () {
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
  let deviceEvents = [
    "notificationReceivedForeground",
    "notificationReceivedBackground",
    "notificationOpened"
  ];

  let addEventListenerSpy, removeEventListenerSpy;
  let notificationIOS;
  let someHandler = () => {};

  before(() => {
    addEventListenerSpy = sinon.spy();
    removeEventListenerSpy = sinon.spy();
    notificationIOS = proxyquire("../index.ios", {
      "react-native": {
        NativeModules: {
          RNNotifications: { }
        },
        DeviceEventEmitter: {
          addListener: (...args) => {
            addEventListenerSpy(...args);

            return { remove: removeEventListenerSpy };
          }
        },
        "@noCallThru": true
      }
    }).default;
  });

  afterEach(() => {
    addEventListenerSpy.reset();
    removeEventListenerSpy.reset();
  });

  after(() => {
    addEventListenerSpy = null;
    removeEventListenerSpy = null;
    notificationIOS = null;
  });

  describe("Add Event Listener", function () {
    deviceEvents.forEach(function(event) {
      it(`should subscribe the given handler to device event: ${event}`, function () {
        notificationIOS.addEventListener(event, someHandler);

        expect(addEventListenerSpy).to.have.been.calledWith(event, sinon.match.func);
      });
    });

    it("should not subscribe to unknown device events", function () {
      notificationIOS.addEventListener("someUnsupportedEvent", someHandler);

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

  describe("Remove Event Listener", function () {
    deviceEvents.forEach(function(event) {
      it(`should unsubscribe the given handler from device event: ${event}`, function () {
        notificationIOS.addEventListener(event, someHandler);
        notificationIOS.removeEventListener(event, someHandler);

        expect(removeEventListenerSpy).to.have.been.calledOnce;
      });
    });

    it("should not unsubscribe to unknown device events", function () {
      let someUnsupportedEvent = "someUnsupportedEvent";
      notificationIOS.addEventListener(someUnsupportedEvent, someHandler);
      notificationIOS.removeEventListener(someUnsupportedEvent, someHandler);

      expect(removeEventListenerSpy).to.not.have.been.called;
    });
Lidan Hifi's avatar
Lidan Hifi committed
80 81
  });
});