import { expectSaga } from "redux-saga-test-plan";
import * as matchers from "redux-saga-test-plan/matchers";
import CapCollapsibleLeftNavigationSagas from '@capillarytech/cap-ui-library/CapCollapsibleLeftNavigation/saga';
import * as CapAskAira from '@capillarytech/cap-ui-library/CapAskAira';
import rootSagas from "../saga";

describe("analyticsBotSagaFn", () => {
  const { saga: analyticsBotSagaFn } = rootSagas.find(
    (s) => s.key === "analyticsBotSaga"
  );

  beforeEach(() => {
    // Reset all mocks before each test
    jest.resetModules();
    jest.clearAllMocks();
  });

  test("should handle when analyticsBotSaga is valid array", () => {
    // Setup valid array case
    const mockSagas = [
      { call: jest.fn() },
      { call: jest.fn() },
    ];
    Object.defineProperty(CapAskAira, 'analyticsBotSaga', {
      value: mockSagas,
      configurable: true,
    });

    return expectSaga(analyticsBotSagaFn)
      .provide([
        [matchers.call.fn(mockSagas[0].call), undefined],
        [matchers.call.fn(mockSagas[1].call), undefined],
      ])
      .run();
  });

  test("should handle when analyticsBotSaga is undefined", () => {
    // Setup undefined case
    Object.defineProperty(CapAskAira, 'analyticsBotSaga', {
      value: undefined,
      configurable: true,
    });

    return expectSaga(analyticsBotSagaFn)
      .run();
  });

  test("should handle when analyticsBotSaga is not an array", () => {
    // Setup non-array case
    Object.defineProperty(CapAskAira, 'analyticsBotSaga', {
      value: {},
      configurable: true,
    });

    return expectSaga(analyticsBotSagaFn)
      .run();
  });
});

describe("navigationConfigSaga", () => {
  it.concurrent("should call all analytics bot sagas", () => {
    const { saga: navigationConfigSaga } = rootSagas.find(
      (s) => s.key === "navigation"
    );

    return expectSaga(navigationConfigSaga)
      .provide([
        [matchers.call.fn(CapCollapsibleLeftNavigationSagas[0]), undefined],
        [matchers.call.fn(CapCollapsibleLeftNavigationSagas[1]), undefined],
      ])
      .run();
  });
});
