|
| 1 | +import React from "react"; |
| 2 | +import { EuiProvider } from "@elastic/eui"; |
| 3 | +import { QueryClient, QueryClientProvider } from "react-query"; |
| 4 | +import { MemoryRouter, Route, Routes } from "react-router-dom"; |
| 5 | +import { http, HttpResponse } from "msw"; |
| 6 | +import { setupServer } from "msw/node"; |
| 7 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 8 | + |
| 9 | +import DataModeContext from "../../contexts/DataModeContext"; |
| 10 | +import FeatureMetricsDetail from "./FeatureMetricsDetail"; |
| 11 | +import type { FeatureMetric } from "../../queries/useMonitoringApi"; |
| 12 | + |
| 13 | +const row = ( |
| 14 | + metric_date: string, |
| 15 | + granularity: string, |
| 16 | + mean: number, |
| 17 | +): FeatureMetric => ({ |
| 18 | + project_id: "demo", |
| 19 | + feature_view_name: "driver_stats", |
| 20 | + feature_name: "conv_rate", |
| 21 | + metric_date, |
| 22 | + granularity, |
| 23 | + data_source_type: "batch", |
| 24 | + computed_at: "2026-10-31T00:00:00Z", |
| 25 | + max_event_timestamp: null, |
| 26 | + is_baseline: granularity === "baseline", |
| 27 | + feature_type: "numeric", |
| 28 | + row_count: 100, |
| 29 | + null_count: 0, |
| 30 | + null_rate: 0, |
| 31 | + mean, |
| 32 | + stddev: 0.1, |
| 33 | + min_val: 0, |
| 34 | + max_val: 1, |
| 35 | + p50: 0.5, |
| 36 | + p75: null, |
| 37 | + p90: null, |
| 38 | + p95: 0.9, |
| 39 | + p99: null, |
| 40 | + histogram: null, |
| 41 | +}); |
| 42 | + |
| 43 | +const daily = Array.from({ length: 30 }, (_, i) => |
| 44 | + row(`2026-10-${String(i + 1).padStart(2, "0")}`, "daily", (i + 1) / 100), |
| 45 | +); |
| 46 | +const monthly = row("2026-09-01", "monthly", 0.3); |
| 47 | +const baseline: FeatureMetric = { |
| 48 | + ...row("2026-10-31", "baseline", 0.47), |
| 49 | + histogram: { |
| 50 | + bins: [0, 0.1, 0.2, 0.3, 0.4], |
| 51 | + counts: [60, 30, 10, 4, 1], |
| 52 | + bin_width: 0.1, |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +const server = setupServer( |
| 57 | + http.get("/api/v1/monitoring/metrics/features", () => |
| 58 | + HttpResponse.json([...daily, monthly, baseline]), |
| 59 | + ), |
| 60 | + http.get("/api/v1/monitoring/metrics/baseline", () => |
| 61 | + HttpResponse.json([baseline]), |
| 62 | + ), |
| 63 | +); |
| 64 | + |
| 65 | +beforeAll(() => server.listen()); |
| 66 | +afterAll(() => server.close()); |
| 67 | + |
| 68 | +test("monitoring charts show values on hover and zoom", async () => { |
| 69 | + render( |
| 70 | + <QueryClientProvider |
| 71 | + client={ |
| 72 | + new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 73 | + } |
| 74 | + > |
| 75 | + <DataModeContext.Provider value={{}}> |
| 76 | + <EuiProvider> |
| 77 | + <MemoryRouter |
| 78 | + initialEntries={[ |
| 79 | + "/p/demo/monitoring/feature/driver_stats/conv_rate", |
| 80 | + ]} |
| 81 | + > |
| 82 | + <Routes> |
| 83 | + <Route |
| 84 | + path="/p/:projectName/monitoring/feature/:featureViewName/:featureName" |
| 85 | + element={<FeatureMetricsDetail />} |
| 86 | + /> |
| 87 | + </Routes> |
| 88 | + </MemoryRouter> |
| 89 | + </EuiProvider> |
| 90 | + </DataModeContext.Provider> |
| 91 | + </QueryClientProvider>, |
| 92 | + ); |
| 93 | + |
| 94 | + // Only daily windows: the monthly row (09-01) and the baseline (10-31) |
| 95 | + // would stretch the range if they were plotted. |
| 96 | + expect( |
| 97 | + await screen.findByText(/Showing daily windows 2026-10-01 – 2026-10-30\./), |
| 98 | + ).toBeInTheDocument(); |
| 99 | + // Axis labels are calendar days, with no time-zone hour added. |
| 100 | + expect(screen.getAllByText("10-01").length).toBeGreaterThan(0); |
| 101 | + expect(screen.queryByText(/\d{2}-\d{2} \d{2}:\d{2}/)).toBeNull(); |
| 102 | + |
| 103 | + // Hovering a distribution bar shows its range and count. The compact |
| 104 | + // chart puts 5 bars of 92 px from x 54, so x 340 is the fourth bar. |
| 105 | + const histogram = screen.getByRole("img", { name: "Histogram" }); |
| 106 | + fireEvent.mouseMove(histogram, { clientX: 340 }); |
| 107 | + const tooltip = screen.getByRole("tooltip"); |
| 108 | + expect(tooltip).toHaveTextContent("0.30 – 0.40"); |
| 109 | + expect(tooltip).toHaveTextContent("Count4"); |
| 110 | + fireEvent.mouseLeave(histogram); |
| 111 | + |
| 112 | + // Drag across the left half of the plot (x 60 to 450 of 780 wide). |
| 113 | + const [drift] = screen.getAllByTestId("time-series-chart"); |
| 114 | + fireEvent.mouseDown(drift, { clientX: 60, button: 0 }); |
| 115 | + fireEvent.mouseMove(drift, { clientX: 450 }); |
| 116 | + fireEvent.mouseUp(drift, { clientX: 450 }); |
| 117 | + expect( |
| 118 | + screen.getByText(/Showing daily windows 2026-10-01 – 2026-10-15\./), |
| 119 | + ).toBeInTheDocument(); |
| 120 | + |
| 121 | + // Hovering the first point shows its date and mean. |
| 122 | + fireEvent.mouseMove(drift, { clientX: 60 }); |
| 123 | + expect(screen.getByText("2026-10-01")).toBeInTheDocument(); |
| 124 | + expect(screen.getByText("0.01")).toBeInTheDocument(); |
| 125 | + |
| 126 | + // Zoomed in, scrolling sideways moves through the rest of the data. |
| 127 | + // 780 px is one plot width, so the window moves on by its own length. |
| 128 | + const [scroller] = screen.getAllByTestId("time-series-scroll"); |
| 129 | + scroller.scrollLeft = 780; |
| 130 | + fireEvent.scroll(scroller); |
| 131 | + expect( |
| 132 | + screen.getByText(/Showing daily windows 2026-10-15 – 2026-10-30\./), |
| 133 | + ).toBeInTheDocument(); |
| 134 | + |
| 135 | + fireEvent.doubleClick(drift); |
| 136 | + expect( |
| 137 | + screen.getByText(/Showing daily windows 2026-10-01 – 2026-10-30\./), |
| 138 | + ).toBeInTheDocument(); |
| 139 | +}); |
0 commit comments