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

import { fireEvent, render, waitFor, screen, getByText } from "@testing-library/react";
import { setToken } from "../Contexts/UserContext";
import { TEST_AUTH_OBJECT } from "../../utils/testing";
import addSnapshotSerializer from "../../utils/snapshot";
import { VehiclePopUp } from "./popup";

const renderPopup = async (online) => {
  const { container } = render(
    <VehiclePopUp vin={"F1SKER123456"} online={online} />
  );
  await waitFor(() => { });
  return container;
};

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

  it("Render", async () => {
    setToken(TEST_AUTH_OBJECT);
    const container = await renderPopup(false);
    expect(container).toMatchSnapshot();
  });

  it("Online: false", async () => {
    setToken(TEST_AUTH_OBJECT);
    const container = await renderPopup(false);
    const el = screen.getAllByText('Connected', { exact: false })[0]
    
    expect(el.parentElement.textContent).toEqual('Connected: false');
  });

  it("Online: true", async () => {
    setToken(TEST_AUTH_OBJECT);
    const container = await renderPopup(true);
    const el = screen.getAllByText('Connected', { exact: false })[0]
    
    expect(el.parentElement.textContent).toEqual('Connected: true');
  });
})
