|
| 1 | +import React from "react"; |
| 2 | +import { VictoryChart } from "victory-chart"; |
| 3 | +import { VictoryAxis } from "victory-axis"; |
| 4 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 5 | + |
| 6 | +describe("components/victory-chart", () => { |
| 7 | + describe("default component rendering", () => { |
| 8 | + it("renders an svg with the correct width and height", () => { |
| 9 | + const { container } = render(<VictoryChart />); |
| 10 | + const svg = container.querySelector("svg"); |
| 11 | + |
| 12 | + expect(svg.getAttribute("style")).toContain("width: 100%; height: 100%"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("renders an svg with the correct viewBox", () => { |
| 16 | + const { container } = render(<VictoryChart />); |
| 17 | + const svg = container.querySelector("svg"); |
| 18 | + const viewBoxValue = `0 0 ${450} ${300}`; |
| 19 | + |
| 20 | + expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("axis rendering", () => { |
| 25 | + it("renders two axes by default", () => { |
| 26 | + const props = { |
| 27 | + defaultAxes: { |
| 28 | + independent: <VictoryAxis data-testid="axis" />, |
| 29 | + dependent: <VictoryAxis data-testid="axis" dependentAxis /> |
| 30 | + } |
| 31 | + }; |
| 32 | + render(<VictoryChart {...props} />); |
| 33 | + |
| 34 | + const axes = screen.getAllByTestId("axis"); |
| 35 | + |
| 36 | + expect(axes).toHaveLength(2); |
| 37 | + }); |
| 38 | + |
| 39 | + it("renders one axis if one axis is given", () => { |
| 40 | + render( |
| 41 | + <VictoryChart> |
| 42 | + <VictoryAxis data-testid="axis" /> |
| 43 | + </VictoryChart> |
| 44 | + ); |
| 45 | + const axes = screen.getAllByTestId("axis"); |
| 46 | + |
| 47 | + expect(axes).toHaveLength(1); |
| 48 | + }); |
| 49 | + |
| 50 | + // TODO: Is this test useful? It's hard to test this with react testing library, which |
| 51 | + // may mean it should be removed. |
| 52 | + it("allows axis to control the crossAxis, and offset props", () => { |
| 53 | + render( |
| 54 | + <VictoryChart> |
| 55 | + <VictoryAxis |
| 56 | + data-testid="axis" |
| 57 | + crossAxis={false} |
| 58 | + offsetX={50} |
| 59 | + offsetY={50} |
| 60 | + data-cross-axis={(props) => props.crossAxis} |
| 61 | + data-offset-x={(props) => props.offsetX} |
| 62 | + data-offset-y={(props) => props.offsetY} |
| 63 | + /> |
| 64 | + </VictoryChart> |
| 65 | + ); |
| 66 | + const axis = screen.getByTestId("axis"); |
| 67 | + |
| 68 | + expect(axis.getAttribute("data-cross-axis")).toEqual("false"); |
| 69 | + expect(axis.getAttribute("data-offset-x")).toEqual("50"); |
| 70 | + expect(axis.getAttribute("data-offset-y")).toEqual("50"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("accepts user props", () => { |
| 74 | + render(<VictoryChart data-testid="victory-chart" aria-label="Chart" />); |
| 75 | + |
| 76 | + expect(screen.getByTestId("victory-chart")).toBeDefined(); |
| 77 | + expect(screen.getByLabelText("Chart")).toBeDefined(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("event handling", () => { |
| 82 | + it("attaches an event to the parent svg", () => { |
| 83 | + const clickHandler = jest.fn(); |
| 84 | + const { container } = render( |
| 85 | + <VictoryChart |
| 86 | + events={[ |
| 87 | + { |
| 88 | + target: "parent", |
| 89 | + eventHandlers: { onClick: clickHandler } |
| 90 | + } |
| 91 | + ]} |
| 92 | + /> |
| 93 | + ); |
| 94 | + const svg = container.querySelector("svg"); |
| 95 | + |
| 96 | + fireEvent.click(svg); |
| 97 | + |
| 98 | + expect(clickHandler).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments