import * as ReactNative from "react-native";

ReactNative.NativeModules.QuickBrickCommunicationModule = {
  quickBrickEvent: jest.fn(),
  accountId: 123,
  accountsAccountId: "accountsAccountIDString1234",
  broadcasterId: 145,
  bundleIdentifier: "com.apptestname",
  initialProps: {
    foo: "bar",
  },
};

const {
  sendQuickBrickEvent,
  getAppData,
  getLegacyInitialProps,
} = require("..");

const eventName = "event";
const payload = {};

const accountId = 123;
const accountsAccountId = "accountsAccountIDString1234";
const broadcasterId = 145;
const bundleIdentifier = "com.apptestname";

const { NativeModules } = require("react-native");

describe("QuickBrickModule", () => {
  describe("sendQuickBrickEvent", () => {
    it("invokes the native module", () => {
      sendQuickBrickEvent(eventName, payload);

      expect(
        NativeModules.QuickBrickCommunicationModule.quickBrickEvent
      ).toHaveBeenCalledWith(eventName, payload);
    });

    it("passes an empty object as payload if not provided", () => {
      sendQuickBrickEvent(eventName);

      expect(
        NativeModules.QuickBrickCommunicationModule.quickBrickEvent
      ).toHaveBeenCalledWith(eventName, {});
    });

    afterEach(() => {
      NativeModules.QuickBrickCommunicationModule.quickBrickEvent.mockRestore();
    });
  });

  describe("getAppData", () => {
    it("returns the app data", () => {
      const appData = getAppData();
      expect(appData).toHaveProperty("accountId", accountId);
      expect(appData).toHaveProperty("accountsAccountId", accountsAccountId);
      expect(appData).toHaveProperty("broadcasterId", broadcasterId);
      expect(appData).toHaveProperty("bundleIdentifier", bundleIdentifier);
    });
  });

  describe("getLegacyInitialProps", () => {
    it("returns the initial props", () => {
      const initialProps = getLegacyInitialProps();

      expect(initialProps).toEqual(
        NativeModules.QuickBrickCommunicationModule.initialProps
      );
    });
  });
});
