|
| 1 | +import React from "react"; |
| 2 | +import { act, render, screen } from "@testing-library/react"; |
| 3 | +import { describe, test, expect, beforeEach, vi } from "vitest"; |
| 4 | +import { Color, DType } from "../../../types"; |
| 5 | +import { DataBrowserComponent } from "./dataBrowser"; |
| 6 | +import { Trace } from "../../../types/trace"; |
| 7 | +import { Axis } from "../../../types/axis"; |
| 8 | +import { Plt } from "../../../types/plt"; |
| 9 | + |
| 10 | +declare global { |
| 11 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
| 12 | + namespace NodeJS { |
| 13 | + // eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 14 | + interface Global {} |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +interface GlobalFetch extends NodeJS.Global { |
| 19 | + fetch: any; |
| 20 | +} |
| 21 | +const globalWithFetch = global as GlobalFetch; |
| 22 | + |
| 23 | +// Mock the MUI X-Charts components |
| 24 | +vi.mock("@mui/x-charts", () => ({ |
| 25 | + LineChart: vi.fn(({ series, xAxis, yAxis, sx }) => ( |
| 26 | + <div |
| 27 | + data-testid="line-chart" |
| 28 | + data-series={JSON.stringify(series)} |
| 29 | + data-xaxis={JSON.stringify(xAxis)} |
| 30 | + data-yaxis={JSON.stringify(yAxis)} |
| 31 | + style={sx} |
| 32 | + /> |
| 33 | + )) |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("@mui/material", () => ({ |
| 37 | + Box: vi.fn(({ children }) => <div data-testid="mui-box">{children}</div>), |
| 38 | + Typography: vi.fn(({ children }) => ( |
| 39 | + <div data-testid="mui-typography">{children}</div> |
| 40 | + )) |
| 41 | +})); |
| 42 | + |
| 43 | +describe("DataBrowserComponent", () => { |
| 44 | + // Basic test setup |
| 45 | + const defaultProps = { |
| 46 | + value: { |
| 47 | + getDoubleValue: () => 50, |
| 48 | + getTime: () => { |
| 49 | + new Date(Date.now()); |
| 50 | + } |
| 51 | + } as Partial<DType> as DType, |
| 52 | + connected: true, |
| 53 | + readonly: true, |
| 54 | + pvName: "TEST:PV", |
| 55 | + plt: new Plt({ |
| 56 | + pvlist: [ |
| 57 | + new Trace({ |
| 58 | + archive: { |
| 59 | + name: "Primary", |
| 60 | + url: "http://archiver.diamond.ac.uk/retrieval" |
| 61 | + }, |
| 62 | + yPv: "TEST:PV" |
| 63 | + }) |
| 64 | + ], |
| 65 | + axes: [new Axis()] |
| 66 | + }) |
| 67 | + }; |
| 68 | + |
| 69 | + const mockSuccessResponse: any = JSON.stringify([ |
| 70 | + { |
| 71 | + secs: new Date().getTime() - 250000, |
| 72 | + val: 52 |
| 73 | + }, |
| 74 | + { |
| 75 | + secs: new Date().getTime() - 200000, |
| 76 | + val: 45 |
| 77 | + }, |
| 78 | + { |
| 79 | + secs: new Date().getTime() - 70000, |
| 80 | + val: 60 |
| 81 | + } |
| 82 | + ]); |
| 83 | + console.log(mockSuccessResponse); |
| 84 | + const mockJsonPromise = Promise.resolve( |
| 85 | + JSON.parse(`[{"data": ${mockSuccessResponse}}]`) |
| 86 | + ); |
| 87 | + const mockFetchPromise = Promise.resolve({ |
| 88 | + json: (): Promise<unknown> => mockJsonPromise |
| 89 | + }); |
| 90 | + const mockFetch = (): Promise<unknown> => mockFetchPromise; |
| 91 | + vi.spyOn(globalWithFetch, "fetch").mockImplementation(mockFetch); |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + vi.clearAllMocks(); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("Rendering", () => { |
| 98 | + test("renders with default props", () => { |
| 99 | + render(<DataBrowserComponent {...defaultProps} />); |
| 100 | + |
| 101 | + const lineChart = screen.getByTestId("line-chart"); |
| 102 | + expect(lineChart).toBeDefined(); |
| 103 | + |
| 104 | + const yAxisData = JSON.parse(lineChart.getAttribute("data-yaxis") ?? ""); |
| 105 | + const xAxisData = JSON.parse(lineChart.getAttribute("data-xaxis") ?? ""); |
| 106 | + expect(yAxisData[0].position).toBe("left"); |
| 107 | + expect(xAxisData[0].scaleType).toBe("time"); |
| 108 | + }); |
| 109 | + |
| 110 | + test("renders with 1 y axis on either side", () => { |
| 111 | + const axes = [ |
| 112 | + new Axis({ color: Color.RED }), |
| 113 | + new Axis({ color: Color.BLUE, onRight: true }) |
| 114 | + ]; |
| 115 | + const newProps = { |
| 116 | + ...defaultProps, |
| 117 | + plt: new Plt({ axes: axes }) |
| 118 | + }; |
| 119 | + render(<DataBrowserComponent {...newProps} />); |
| 120 | + |
| 121 | + const lineChart = screen.getByTestId("line-chart"); |
| 122 | + const yAxisData = JSON.parse(lineChart.getAttribute("data-yaxis") ?? ""); |
| 123 | + |
| 124 | + expect(yAxisData[0].color).toBe(Color.RED.toString()); |
| 125 | + expect(yAxisData[1].color).toBe(Color.BLUE.toString()); |
| 126 | + expect(yAxisData[1].position).toBe("right"); |
| 127 | + }); |
| 128 | + |
| 129 | + test("renders with 5 minute archived data", () => { |
| 130 | + const newProps = { |
| 131 | + ...defaultProps, |
| 132 | + plt: new Plt({ start: "5 min", end: "now" }) |
| 133 | + }; |
| 134 | + act(() => { |
| 135 | + render(<DataBrowserComponent {...newProps} />); |
| 136 | + }); |
| 137 | + const lineChart = screen.getByTestId("line-chart"); |
| 138 | + const xAxisData = JSON.parse(lineChart.getAttribute("data-xaxis") ?? ""); |
| 139 | + |
| 140 | + const expectedDiff = 300000; // 5 * 60 * 1000 |
| 141 | + const actualDiff = |
| 142 | + new Date(xAxisData[0].max).getTime() - |
| 143 | + new Date(xAxisData[0].min).getTime(); |
| 144 | + |
| 145 | + const seriesData = JSON.parse( |
| 146 | + lineChart.getAttribute("data-series") ?? "" |
| 147 | + ); |
| 148 | + |
| 149 | + expect(actualDiff).toBe(expectedDiff); |
| 150 | + //expect(seriesData[0].data).toBe([52, 45, 60, 50]); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments