jest.mock("../Contexts/UserContext");
jest.mock("../Contexts/StatusContext");

import React from "react";
import {
  fireEvent,
  render,
  screen,
  waitFor,
} from "@testing-library/react";
import { UserProvider, setToken } from "../Contexts/UserContext";
import { StatusProvider } from "../Contexts/StatusContext";
import { TEST_AUTH_OBJECT_FISKER } from "../../utils/testing";
import BulkActions from ".";
import addSnapshotSerializer from "../../utils/snapshot";

describe("BulkActions", () => {
  beforeAll(() => {
    setToken(TEST_AUTH_OBJECT_FISKER);
    global.URL.createObjectURL = jest.fn();
    global.URL.revokeObjectURL = jest.fn();
    addSnapshotSerializer(expect);
  });

  it("Render", async () => {
    const { container } = render(
      <StatusProvider>
        <UserProvider>
          <BulkActions
            actions={["addTags"]}
            ids={["TESTVIN1234567890"]}
          />
        </UserProvider>
      </StatusProvider>
    );
    await waitFor(() => {
      /* render */
    });
    expect(container).toMatchSnapshot();
  });

  it("opens a modal", async () => {
    render(
      <StatusProvider>
        <UserProvider>
          <BulkActions
            actions={["addTags"]}
            ids={["TESTVIN1234567890"]}
          />
        </UserProvider>
      </StatusProvider>
    );

    const buttonEl = screen.getByText("Add Tags");
    fireEvent.click(buttonEl);
    const submitEl = screen.getByText("Submit");
    expect(submitEl).toBeTruthy();
  });

  it("filters valid actions", async () => {
    render(
      <StatusProvider>
        <UserProvider>
          <BulkActions
            actions={["addTags", "someInvalidAction", "updateConfig"]}
            ids={["TESTVIN1234567890"]}
          />
        </UserProvider>
      </StatusProvider>
    );

    const dropdownBtn = screen.getByTestId("dropdown-button-expand");
    fireEvent.click(dropdownBtn);
    const dropdownOptions = screen.getAllByRole("menuitem");
    expect(dropdownOptions.length).toBe(2);
  });
});
