Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"prettier": "^3.4.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-test-renderer": "^18.3.1",
"rimraf": "^6.0.1",
"typescript": "^5.7.2",
"webpack": "^5.96.1",
Expand Down
175 changes: 78 additions & 97 deletions packages/react-jsx-highcharts/test/components/XAxis/XAxis.spec.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,99 @@
import * as React from 'react';
import TestRenderer from 'react-test-renderer';
import Highcharts from 'highcharts/highstock';
import addAccessibility from 'highcharts/modules/accessibility';

import XAxis from '../../../src/components/XAxis/XAxis';
import Axis from '../../../src/components/Axis';
import ChartContext from '../../../src/components/ChartContext';
import { createMockProvidedChart } from '../../test-utils';
import { render } from '@testing-library/react';

describe('<XAxis />', () => {
let testContext;
let ProvidedAxis;
beforeEach(() => {
testContext = {};
const { chartStubs } = createMockProvidedChart({
type: 'chart'
});
testContext.chartStubs = chartStubs;
import { HighchartsChart, HighchartsProvider, XAxis } from '../../../src';
import ContextSpy from '../../ContextSpy';

ProvidedAxis = props => (
<ChartContext.Provider value={chartStubs}>
<XAxis {...props} />
</ChartContext.Provider>
);
});
addAccessibility(Highcharts);

it('renders an <Axis />', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);

expect(axis).toBeDefined();
});
describe('<XAxis />', () => {
it('creates an chart xaxis', () => {
let chartRef = {};
const Component = () => {
return (
<HighchartsProvider Highcharts={Highcharts}>
<HighchartsChart>
<XAxis id="myAxis">
<ContextSpy chartRef={chartRef} />
</XAxis>
</HighchartsChart>
</HighchartsProvider>
);
};

it('renders an <Axis isX />', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);
render(<Component />);

expect(axis.props).toHaveProperty('isX', true);
const axis = chartRef.current.object.get('myAxis');
expect(axis).toBeDefined();
expect(axis.isXAxis).toBeDefined();
});

it('passes other props through to <Axis />', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis tickLength={1337} />);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('tickLength', 1337);
});

describe('Highcharts chart', () => {
beforeEach(() => {
testContext.chartStubs.type = 'chart';
});

it('renders the <Axis /> type if provided', () => {
const wrapper = new TestRenderer.create(
<ProvidedAxis type="logarithmic" />
let chartRef = {};
const Component = () => {
return (
<HighchartsProvider Highcharts={Highcharts}>
<HighchartsChart>
<XAxis id="myAxis" tickLength={1337}>
<ContextSpy chartRef={chartRef} />
</XAxis>
</HighchartsChart>
</HighchartsProvider>
);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('type', 'logarithmic');
});

it('renders the an <Axis type="linear" /> if no type specified', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);
};

expect(axis.props).toHaveProperty('type', 'linear');
});

it('uses the id prop if provided', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis id="myXAxisId" />);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('id', 'myXAxisId');
});

it('does not create an id if id prop not provided', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);
render(<Component />);

expect(axis.props.id).not.toBeDefined();
});
const axis = chartRef.current.object.get('myAxis');
expect(axis).toBeDefined();
expect(axis.userOptions.tickLength).toBe(1337);
});

describe('Highstock chart', () => {
beforeEach(() => {
testContext.chartStubs.type = 'stockChart';
});

describe('Highcharts chart', () => {
it('renders the <Axis /> type if provided', () => {
const wrapper = new TestRenderer.create(
<ProvidedAxis type="logarithmic" />
);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('type', 'logarithmic');
});

it('renders the an <Axis type="datetime" /> if no type specified', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('type', 'datetime');
let chartRef = {};
const Component = () => {
return (
<HighchartsProvider Highcharts={Highcharts}>
<HighchartsChart>
<XAxis id="myAxis" type="logarithmic">
<ContextSpy chartRef={chartRef} />
</XAxis>
</HighchartsChart>
</HighchartsProvider>
);
};

render(<Component />);

const axis = chartRef.current.object.get('myAxis');
expect(axis).toBeDefined();
expect(axis.isXAxis).toBe(true);
expect(axis.userOptions.type).toBe('logarithmic');
});

it('uses the id `xAxis` even if an id prop is provided', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis id="myXAxisId" />);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('id', 'xAxis');
});

it('uses the id `xAxis` if id prop not provided', () => {
const wrapper = new TestRenderer.create(<ProvidedAxis />);
const axis = wrapper.root.findByType(Axis);

expect(axis.props).toHaveProperty('id', 'xAxis');
it('renders the an <Axis type="linear" /> if no type specified', () => {
let chartRef = {};
const Component = () => {
return (
<HighchartsProvider Highcharts={Highcharts}>
<HighchartsChart>
<XAxis id="myAxis">
<ContextSpy chartRef={chartRef} />
</XAxis>
</HighchartsChart>
</HighchartsProvider>
);
};

render(<Component />);

const axis = chartRef.current.object.get('myAxis');
expect(axis).toBeDefined();
expect(axis.userOptions.type).toBe('linear');
});
});
});
94 changes: 94 additions & 0 deletions packages/react-jsx-highstock/test/ContextSpy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { useEffect } from 'react';
import {
useAxis,
useChart,
useHighcharts,
useSeries,
usePlotBandLine
} from '../src';

const ContextSpy = ({
axisId,
axisRef,
chartRef,
highchartsRef,
seriesRef,
plotBandLineRef
}) => {
const axis = useAxis(axisId);
const chart = useChart();
const Highcharts = useHighcharts();
const series = useSeries();
const plotbandline = usePlotBandLine();

useEffect(() => {
if (highchartsRef) {
highchartsRef.current = Highcharts;
}

return () => {
if (highchartsRef) {
highchartsRef.current = null;
}
};
}, [Highcharts]);

useEffect(() => {
if (chartRef) {
chartRef.current = chart;
}

return () => {
if (chartRef) {
chartRef.current = null;
}
};
}, [chart]);

useEffect(() => {
if (axisRef) {
axisRef.current = axis;
axisRef.addPlotBandOrLineSpy = jest.spyOn(axis, 'addPlotBandOrLine');
axisRef.removePlotBandOrLineSpy = jest.spyOn(
axis,
'removePlotBandOrLine'
);
}

return () => {
if (axisRef) {
axisRef.addPlotBandOrLineSpy.mockRestore();
axisRef.removePlotBandOrLineSpy.mockRestore();
axisRef.current = null;
}
};
}, [axis]);

useEffect(() => {
if (seriesRef) {
seriesRef.current = series;
}

return () => {
if (seriesRef) {
seriesRef.current = null;
}
};
}, [series]);

useEffect(() => {
if (plotBandLineRef) {
plotBandLineRef.current = plotbandline;
}

return () => {
if (plotBandLineRef) {
plotBandLineRef.current = null;
}
};
}, [plotbandline]);

return null;
};

export default ContextSpy;
Loading