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

import { render, screen, waitFor } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import routeData from "react-router";

import { VehicleProvider } from "../../../Contexts/VehicleContext";
import { StatusProvider } from "../../../Contexts/StatusContext";
import { UserProvider, setToken } from "../../../Contexts/UserContext";
import { TEST_AUTH_OBJECT_FISKER } from "../../../../utils/testing";
import MainForm from "./index";
import addSnapshotSerializer from "../../../../utils/snapshot";
import * as Roles from "../../../../utils/roles";

const renderVehicleDetailsTab = async () => {
  const { container } = render(
    <VehicleProvider>
      <StatusProvider>
        <UserProvider >
          <BrowserRouter>
            <MainForm vin="TESTVIN1234567890" />
          </BrowserRouter>
        </UserProvider>
      </StatusProvider>
    </VehicleProvider>
  );
  await waitFor(() => {
    /* render */
  });
  return container;
};

describe("VehicleDetailsTab", () => {
  beforeAll(() => {
    addSnapshotSerializer(expect);
  });

  beforeEach(() => {
    // Does not work if placed in beforeAll
    jest.spyOn(routeData, 'useParams').mockReturnValue({ vin: "TESTVIN1234567890" })
  })

  it("Render", async () => {
    setToken(TEST_AUTH_OBJECT_FISKER);
    const container = await renderVehicleDetailsTab();
    expect(container).toMatchSnapshot();
  });

  it("renders update config control when required permission is present.", () => {
    const hasRole = jest.spyOn(Roles, 'hasRole');
    hasRole.mockReturnValue(true);
    render(
      <VehicleProvider>
        <StatusProvider>
          <UserProvider>
            <BrowserRouter>
              <MainForm vin="TESTVIN1234567890" />
            </BrowserRouter>
          </UserProvider>
        </StatusProvider>
      </VehicleProvider>
    );

    expect(screen.getByLabelText("Force Config Update")).toBeTruthy();
    hasRole.mockRestore();
  })
});
