index.ios.spec.js 2.4 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

Lidan Hifi's avatar
Lidan Hifi committed
6
describe("NotificationsIOS", () => {
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
  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;
  });

Lidan Hifi's avatar
Lidan Hifi committed
48 49 50
  describe("Add Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should subscribe the given handler to device event: ${event}`, () => {
51 52 53 54 55 56
        notificationIOS.addEventListener(event, someHandler);

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

Lidan Hifi's avatar
Lidan Hifi committed
57
    it("should not subscribe to unknown device events", () => {
58 59 60 61 62 63
      notificationIOS.addEventListener("someUnsupportedEvent", someHandler);

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

Lidan Hifi's avatar
Lidan Hifi committed
64 65 66
  describe("Remove Event Listener", () => {
    deviceEvents.forEach(event => {
      it(`should unsubscribe the given handler from device event: ${event}`, () => {
67 68 69 70 71 72 73
        notificationIOS.addEventListener(event, someHandler);
        notificationIOS.removeEventListener(event, someHandler);

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

Lidan Hifi's avatar
Lidan Hifi committed
74
    it("should not unsubscribe to unknown device events", () => {
75 76 77 78 79 80
      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
81
  });
82 83

  // TODO: Test handle notification with IOSNotification object
Lidan Hifi's avatar
Lidan Hifi committed
84
});