Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dataEndIndex can be -1 #3988

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/chart/generateCategoricalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,23 @@ const tooltipTicksGenerator = (axisMap: AxisMap) => {
* @param {Object} props Props object to use when creating the default state
* @return {Object} Whole new state
*/
const createDefaultState = (props: CategoricalChartProps): CategoricalChartState => {
export const createDefaultState = (props: CategoricalChartProps): CategoricalChartState => {
const { children, defaultShowTooltip } = props;
const brushItem = findChildByType(children, Brush);
const startIndex = (brushItem && brushItem.props && brushItem.props.startIndex) || 0;
const endIndex =
brushItem?.props?.endIndex !== undefined ? brushItem?.props?.endIndex : (props.data && props.data.length - 1) || 0;
let startIndex = 0;
let endIndex = 0;
if (props.data && props.data.length !== 0) {
endIndex = props.data.length - 1;
}

if (brushItem && brushItem.props) {
if (brushItem.props.startIndex >= 0) {
startIndex = brushItem.props.startIndex;
}
if (brushItem.props.endIndex >= 0) {
endIndex = brushItem.props.endIndex;
}
}

return {
chartX: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactElement } from 'react';
import { getAxisMapByAxes, CategoricalChartProps } from '../../src/chart/generateCategoricalChart';
import React, { ReactElement } from 'react';
import { getAxisMapByAxes, CategoricalChartProps, createDefaultState } from '../../src/chart/generateCategoricalChart';
import { AxisStackGroups } from '../../src/util/ChartUtils';
import { Brush } from '../../src';

const data = [
{
Expand Down Expand Up @@ -230,4 +231,36 @@ describe('generateCategoricalChart', () => {
);
});
});

describe('createDefaultState', () => {
it('Should have the correct dataIndex', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you combine these four separate test cases into a single test? Is there any interaction that I am missing? If not, please split it up into 4 tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was just ctrl+c,v 😅

it('even when invalid brush index(no data)', () => {
const state = createDefaultState({ children: [<Brush endIndex={-1} startIndex={-1} />] });

expect(state.dataStartIndex).toEqual(0);
expect(state.dataEndIndex).toEqual(0);
});

it('even when invalid brush index', () => {
const state = createDefaultState({ children: [<Brush endIndex={-1} startIndex={-1} />], data: [1, 2, 3] });

expect(state.dataStartIndex).toEqual(0);
expect(state.dataEndIndex).toEqual(2);
});

it('even when data length 0', () => {
const state = createDefaultState({ children: [<Brush endIndex={-1} startIndex={-1} />], data: [] });

expect(state.dataStartIndex).toEqual(0);
expect(state.dataEndIndex).toEqual(0);
});

it('when valid brush index', () => {
const state = createDefaultState({ children: [<Brush endIndex={1} startIndex={0} />], data: [1, 2, 3] });

expect(state.dataStartIndex).toEqual(0);
expect(state.dataEndIndex).toEqual(1);
});
});
});
});