import { act, render } from "@testing-library/react";
import React from "react";
import { describe, test, vi } from "vitest";
import { App } from "./App";
import { config } from "./config";

vi.mock("./pages/MetricDetailsPage", () => ({
  MetricDetailsPage: () => <div>MetricDetailsPage</div>,
}));

describe("App", () => {
  test("Display metrics after the auth process", async () => {
    const { queryByText } = render(<App />);
    expect(queryByText("MetricDetailsPage")).toBeNull();
    const mockEvent = new MessageEvent("message", {
      data: "token",
      origin: config.baseUrl,
    });
    await act(() => window.dispatchEvent(mockEvent));
    expect(queryByText("MetricDetailsPage")).toBeDefined();
  });
  test("Display metrics if user is already authenticated", async () => {
    localStorage.setItem(config.localstorageKeys.token, "token");
    const { queryByText } = render(<App />);
    expect(queryByText("MetricDetailsPage")).toBeDefined();
  });
});
