import { callAllWith, getNetworkHooks } from "../utils";

describe("utils", function () {
  describe("getNetworkHooks", () => {
    it("should return object of lists containing callbacks", () => {
      const onConnectionLost = jest.fn();
      const onConnectionRestored = jest.fn();
      const onConnectionTypeChanged = jest.fn();

      const plugins = [
        { module: "test" },
        {
          module: {
            networkHooks: {
              onConnectionLost,
              onConnectionRestored,
              onConnectionTypeChanged,
            },
          },
        },
      ];

      const currentResult = getNetworkHooks(plugins);

      expect(currentResult.onConnectionLost[0]).toBe(onConnectionLost);
      expect(currentResult.onConnectionRestored[0]).toBe(onConnectionRestored);

      expect(currentResult.onConnectionTypeChanged[0]).toBe(
        onConnectionTypeChanged
      );
    });

    it("should skip empty methods", function () {
      const onConnectionRestored = jest.fn();
      const onConnectionTypeChanged = jest.fn();

      const plugins = [
        { module: "test" },
        {
          module: {
            networkHooks: { onConnectionRestored, onConnectionTypeChanged },
          },
        },
      ];

      const currentResult = getNetworkHooks(plugins);

      expect(currentResult.onConnectionLost).not.toBeDefined();
      expect(currentResult.onConnectionRestored[0]).toBe(onConnectionRestored);

      expect(currentResult.onConnectionTypeChanged[0]).toBe(
        onConnectionTypeChanged
      );
    });
  });

  describe("callAllWith", function () {
    it("should call all methods with passed argument", function () {
      const method1 = jest.fn();
      const method2 = jest.fn();

      const arg = { foo: "bar" };

      const methods = [method1, method2];

      callAllWith(methods, arg);

      expect(method2).toBeCalledWith(arg);
      expect(method1).toBeCalledWith(arg);
    });
  });
});
