Add tests for Alpaca Historical Bars API

- Implemented tests for fetching historical bars for AAPL with different timeframes (1D, 5Min, 1H).
- Verified response structure and data integrity for each timeframe.
- Ensured that the API returns valid data and appropriate status for the requests.
This commit is contained in:
2026-05-14 12:50:14 +02:00
parent d1a84325ae
commit cc22174b78
13 changed files with 384 additions and 59 deletions
@@ -1,20 +1,35 @@
/// <reference types="vitest" />
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import TradingViewChart from "../TradingViewChart";
// Mock lightweight-charts
vi.mock("lightweight-charts", () => ({
createChart: vi.fn(() => ({
// Use vi.hoisted to define mock functions that will be available during hoisting
const { mockSetData, mockCreateChart } = vi.hoisted(() => ({
mockSetData: vi.fn(),
mockCreateChart: vi.fn(() => ({
addSeries: vi.fn(() => ({
setData: vi.fn(),
})),
remove: vi.fn(),
})),
}));
vi.mock("lightweight-charts", () => ({
createChart: mockCreateChart,
CandlestickSeries: {},
}));
describe("TradingViewChart", () => {
beforeEach(() => {
vi.clearAllMocks();
// Update the mock's setData to track calls
const mockSeries = { setData: mockSetData };
mockCreateChart.mockReturnValue({
addSeries: vi.fn(() => mockSeries),
remove: vi.fn(),
});
});
it("renders the ticker symbol as heading", () => {
render(<TradingViewChart ticker="AAPL" />);
expect(screen.getByText("AAPL Price Chart")).toBeInTheDocument();
@@ -23,14 +38,53 @@ describe("TradingViewChart", () => {
it("renders without data prop", () => {
render(<TradingViewChart ticker="MSFT" />);
expect(screen.getByText("MSFT Price Chart")).toBeInTheDocument();
expect(mockSetData).not.toHaveBeenCalled();
});
it("renders with data prop", () => {
it("calls setData with correct data format when data is provided", () => {
const data = [
{ time: "2024-01-01", open: 100, high: 110, low: 95, close: 105 },
{ time: "2024-01-02", open: 105, high: 115, low: 100, close: 110 },
];
render(<TradingViewChart ticker="GOOGL" data={data} />);
expect(screen.getByText("GOOGL Price Chart")).toBeInTheDocument();
expect(mockSetData).toHaveBeenCalledWith(data);
});
it("does not call setData when data array is empty", () => {
render(<TradingViewChart ticker="TSLA" data={[]} />);
expect(screen.getByText("TSLA Price Chart")).toBeInTheDocument();
expect(mockSetData).not.toHaveBeenCalled();
});
it("creates chart with autoSize option for responsive sizing", () => {
render(<TradingViewChart ticker="TEST" />);
expect(mockCreateChart).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
autoSize: true,
})
);
});
it("creates candlestick series with explicit colors", () => {
const mockAddSeries = vi.fn();
mockCreateChart.mockReturnValue({
addSeries: mockAddSeries,
remove: vi.fn(),
});
render(<TradingViewChart ticker="TEST" />);
expect(mockAddSeries).toHaveBeenCalledWith(
{},
expect.objectContaining({
upColor: "#26a69a",
downColor: "#ef5350",
})
);
});
});