/// <reference types="vitest" />
import { describe, it, expect, vi } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import StockViewer from "../StockViewer";

describe("StockViewer", () => {
  it("fetches and displays indicators", async () => {
    const mockData = {
      symbol: "AAPL",
      indicators: { sma: 155.5, ema: 157.2, rsi: 62.3, macd: 1.8 },
    };
    globalThis.fetch = vi.fn().mockResolvedValue({
      ok: true,
      json: async () => mockData,
    }) as any;

    render(<StockViewer />);

    const input = screen.getByPlaceholderText(/enter stock symbol/i);
    const button = screen.getByRole("button");

    await userEvent.type(input, "AAPL");
    await userEvent.click(button);

    await waitFor(() => {
      expect(screen.getByText(/results for aapl/i)).toBeInTheDocument();
    });
    // Accept either locale format for decimal separator
    const bodyText = screen.getByText(/155.5/);
    expect(bodyText).toBeInTheDocument();
  });
});