2024-02-03 20:23:31 -07:00
|
|
|
import React from "react";
|
|
|
|
import { render } from "@testing-library/react-native";
|
2024-01-10 13:30:46 -07:00
|
|
|
import "@testing-library/jest-native/extend-expect";
|
2024-02-03 20:23:31 -07:00
|
|
|
import * as scale from "d3-scale";
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
import { CustomBars } from "../../component/charts/custom-bars";
|
|
|
|
import {
|
|
|
|
calculateBarOrigin,
|
|
|
|
drawBarPath,
|
|
|
|
} from "../../component/charts/custom-bar-utils";
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-01-10 17:34:27 -07:00
|
|
|
const mockYScaleFunction = scale.scaleLinear();
|
|
|
|
const mockXScaleFunction = scale.scaleBand();
|
|
|
|
const mockBandwidth = 100;
|
|
|
|
const mockCombinedData = [
|
2024-02-03 20:23:31 -07:00
|
|
|
{
|
|
|
|
data: [{ value: 10 }, { value: 20 }],
|
|
|
|
svg: { fill: "red" },
|
|
|
|
},
|
2024-01-10 17:34:27 -07:00
|
|
|
];
|
|
|
|
const mockRawData = [
|
2024-02-03 20:23:31 -07:00
|
|
|
{ label: "Full ball", shotsTaken: 10, makePercentage: 20 },
|
2024-01-10 17:34:27 -07:00
|
|
|
];
|
2024-02-03 20:23:31 -07:00
|
|
|
const mockBarColors = ["red", "blue"];
|
2024-01-10 17:34:27 -07:00
|
|
|
const mockGap = 2;
|
|
|
|
const mockRoundedRadius = 4;
|
2024-02-03 20:23:31 -07:00
|
|
|
describe("CustomBars Component Tests", () => {
|
|
|
|
it("should render correct number of Svg and Bar components", () => {
|
|
|
|
const { getAllByTestId } = render(
|
|
|
|
<CustomBars
|
|
|
|
x={mockXScaleFunction}
|
|
|
|
y={mockYScaleFunction}
|
|
|
|
bandwidth={mockBandwidth}
|
|
|
|
barData={mockCombinedData}
|
|
|
|
xValues={mockRawData}
|
|
|
|
barColors={mockBarColors}
|
|
|
|
gap={mockGap}
|
|
|
|
roundedRadius={mockRoundedRadius}
|
|
|
|
/>,
|
|
|
|
);
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const svgs = getAllByTestId(/svg-/);
|
|
|
|
const bars = getAllByTestId(/bar-/);
|
2024-01-10 13:30:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
expect(svgs.length).toBe(mockRawData.length);
|
|
|
|
expect(bars.length).toBe(mockCombinedData.length * mockRawData.length);
|
|
|
|
});
|
2024-01-10 17:34:27 -07:00
|
|
|
});
|
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
describe("Bar utility functions", () => {
|
|
|
|
describe("calculateBarOrigin", () => {
|
|
|
|
it("calculates properties correctly", () => {
|
|
|
|
const mockData = { value: 10 };
|
|
|
|
const mockIndex = 1;
|
|
|
|
const mockBarNumber = 2;
|
|
|
|
const mockBarWidth = 20;
|
|
|
|
const mockGap = 5;
|
2024-01-10 17:34:27 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const result = calculateBarOrigin({
|
|
|
|
scaleX: mockXScaleFunction,
|
|
|
|
scaleY: mockYScaleFunction,
|
|
|
|
data: mockData,
|
|
|
|
index: mockIndex,
|
|
|
|
barNumber: mockBarNumber,
|
|
|
|
barWidth: mockBarWidth,
|
|
|
|
gap: mockGap,
|
|
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
|
|
xOrigin: expect.any(Number),
|
|
|
|
yOrigin: expect.any(Number),
|
|
|
|
height: expect.any(Number),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-01-10 17:34:27 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
describe("drawBarPath", () => {
|
|
|
|
it("generates a correct SVG path string", () => {
|
|
|
|
const xOrigin = 50;
|
|
|
|
const yOrigin = 100;
|
|
|
|
const barWidth = 20;
|
|
|
|
const height = 60;
|
|
|
|
const roundedRadius = 10;
|
2024-01-10 17:34:27 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const path = drawBarPath(
|
|
|
|
xOrigin,
|
|
|
|
yOrigin,
|
|
|
|
barWidth,
|
|
|
|
height,
|
|
|
|
roundedRadius,
|
|
|
|
);
|
|
|
|
const expectedPath =
|
|
|
|
"M50,160L50,110A10,10,0,0,1,60,100L60,100A10,10,0,0,1,70,110L70,160L50,160Z";
|
|
|
|
|
|
|
|
expect(path).toBe(expectedPath);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|