feat: add stock indicators route and Alpaca account info
- New /stocks route with StockViewer component - New /api/indicators endpoint with SMA, EMA, RSI, MACD - New /api/alpaca/account endpoint - AlpacaAccountInfo component on home page - Indicator calculation utilities - Tests for utilities and components - Vite proxy config for /api
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/// <reference types="vitest" />
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import AlpacaAccountInfo from "../AlpacaAccountInfo";
|
||||
|
||||
describe("AlpacaAccountInfo", () => {
|
||||
it("displays account info after loading", async () => {
|
||||
const mockFetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
cash: 12345.67,
|
||||
buying_power: 8000.0,
|
||||
portfolio_value: 25000.0,
|
||||
}),
|
||||
});
|
||||
globalThis.fetch = mockFetch;
|
||||
|
||||
render(<AlpacaAccountInfo />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Alpaca Account/i)).toBeInTheDocument();
|
||||
});
|
||||
// Use regex to match number regardless of locale decimal separator
|
||||
expect(screen.getByText(/\$12[\.,]345/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/\$8[\.,]000/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/\$25[\.,]000/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("displays error when fetch fails", async () => {
|
||||
const mockFetch = vi.fn().mockRejectedValue(new Error("Network error"));
|
||||
globalThis.fetch = mockFetch;
|
||||
|
||||
render(<AlpacaAccountInfo />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Failed to load account info/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
/// <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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user