railbird-gql/test/component/custom-bars.test.tsx

89 lines
2.5 KiB
TypeScript
Raw Normal View History

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