import {
  bootstrapZappPipes,
  resetZappPipesAdapterCache,
  setZappPipesAdapterCache,
} from "../index";
import * as zappPipesModule from "@applicaster/zapp-pipes-dev-kit/lib/app-pipes";

const zappPipesSpy = jest
  .spyOn(zappPipesModule, "createZappPipesLibrary")
  .mockImplementation(() => jest.fn);

const providers = [
  { name: "provider", handler: () => jest.fn(), manifest: { handlers: [] } },
  { name: "provider2", handler: () => jest.fn(), manifest: { handlers: [] } },
];

describe("bootstrapZappPipes - not cached", () => {
  afterEach(() => {
    zappPipesSpy.mockClear();
    resetZappPipesAdapterCache();
  });

  it("returns an object with a get method", () => {
    const zappPipes = bootstrapZappPipes(providers);
    expect(zappPipes).toHaveProperty("get", expect.any(Function));
  });

  it("calls the createZappPipesLibrary function", () => {
    bootstrapZappPipes(providers);

    expect(zappPipesSpy).toHaveBeenCalledWith({
      providers,
      release: "dev",
      nativeBridge: {
        appData: expect.any(Function),
        getSessionStoreItem: expect.any(Function),
        setSessionStoreItem: expect.any(Function),
        getAllSessionItems: expect.any(Function),
        getLocalStoreItem: expect.any(Function),
        setLocalStoreItem: expect.any(Function),
      },
    });
  });

  it("doesn't invoke createZappPipes Library if a cachedAdapter exists", () => {
    setZappPipesAdapterCache(providers, { get: jest.fn() });
    bootstrapZappPipes(providers);
    expect(zappPipesSpy).not.toHaveBeenCalled();
  });
});
