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

import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import userEvent from '@testing-library/user-event';
import { BrowserRouter } from "react-router-dom";

import { UserProvider, setToken } from "../../Contexts/UserContext";
import { StatusProvider } from "../../Contexts/StatusContext";
import ManifestList from ".";
import { TEST_AUTH_OBJECT_FISKER } from "../../../utils/testing";

const Page = (
  <UserProvider>
    <BrowserRouter>
      <StatusProvider>
        <ManifestList />
      </StatusProvider>
    </BrowserRouter>
  </UserProvider>
);

describe("Manifest List Component", () => {
  beforeAll(() => {
    setToken(TEST_AUTH_OBJECT_FISKER);
  });

  it("adjusts the active state on switch to archived tab", async () => {
    render(Page);

    const archiveActionEl = screen.getByText("Archive");
    fireEvent.click(screen.getByText("Archived"));
    expect(archiveActionEl.innerHTML).toBe("Activate");
  });

  it("properly selects and deselects a manifest", () => {
    const { queryAllByRole } = render(Page);
    const checkbox = queryAllByRole("checkbox")[0];
    expect(checkbox).not.toBeChecked();
    userEvent.click(checkbox);
    expect(checkbox).toBeChecked();
    userEvent.click(checkbox);
    expect(checkbox).not.toBeChecked();
  })
});
