2020-09-03 21:34:22 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { Bar } from 'react-chartjs-2';
|
2020-09-17 19:05:26 +03:00
|
|
|
import { prettify } from '../../../src/utils/helpers/numbers';
|
2020-12-20 14:17:12 +03:00
|
|
|
import { MAIN_COLOR, MAIN_COLOR_ALPHA } from '../../../src/utils/theme';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { HorizontalBarChart } from '../../../src/visits/charts/HorizontalBarChart';
|
2018-09-08 10:06:18 +03:00
|
|
|
|
2021-09-19 11:57:36 +03:00
|
|
|
describe('<HorizontalBarChart />', () => {
|
2020-09-03 21:34:22 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2018-09-08 10:06:18 +03:00
|
|
|
const stats = {
|
|
|
|
foo: 123,
|
|
|
|
bar: 456,
|
|
|
|
};
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-09-08 10:06:18 +03:00
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
it('renders Bar with expected properties', () => {
|
|
|
|
wrapper = shallow(<HorizontalBarChart stats={stats} />);
|
2021-09-18 14:17:04 +03:00
|
|
|
const horizontal = wrapper.find(Bar);
|
2018-09-08 10:06:18 +03:00
|
|
|
|
|
|
|
expect(horizontal).toHaveLength(1);
|
|
|
|
|
2022-03-07 20:06:28 +03:00
|
|
|
const { datasets: [{ backgroundColor, borderColor }] } = horizontal.prop('data') as any;
|
|
|
|
const { plugins, scales } = (horizontal.prop('options') ?? {}) as any;
|
2018-09-08 10:06:18 +03:00
|
|
|
|
2020-12-20 14:17:12 +03:00
|
|
|
expect(backgroundColor).toEqual(MAIN_COLOR_ALPHA);
|
|
|
|
expect(borderColor).toEqual(MAIN_COLOR);
|
2021-09-18 14:17:04 +03:00
|
|
|
expect(plugins.legend).toEqual({ display: false });
|
2018-09-08 21:34:04 +03:00
|
|
|
expect(scales).toEqual({
|
2021-09-18 14:17:04 +03:00
|
|
|
x: {
|
|
|
|
beginAtZero: true,
|
|
|
|
stacked: true,
|
|
|
|
ticks: {
|
|
|
|
precision: 0,
|
|
|
|
callback: prettify,
|
2018-09-08 21:34:04 +03:00
|
|
|
},
|
2021-09-18 14:17:04 +03:00
|
|
|
},
|
|
|
|
y: { stacked: true },
|
2018-09-08 21:34:04 +03:00
|
|
|
});
|
2018-09-08 10:06:18 +03:00
|
|
|
});
|
2020-04-09 10:44:14 +03:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
[{ foo: 23 }, [ 100, 456 ], [ 23, 0 ]],
|
|
|
|
[{ foo: 50 }, [ 73, 456 ], [ 50, 0 ]],
|
|
|
|
[{ bar: 45 }, [ 123, 411 ], [ 0, 45 ]],
|
|
|
|
[{ bar: 20, foo: 13 }, [ 110, 436 ], [ 13, 20 ]],
|
|
|
|
[ undefined, [ 123, 456 ], undefined ],
|
|
|
|
])('splits highlighted data from regular data', (highlightedStats, expectedData, expectedHighlightedData) => {
|
2021-09-18 20:05:28 +03:00
|
|
|
wrapper = shallow(<HorizontalBarChart stats={stats} highlightedStats={highlightedStats} />);
|
2021-09-18 14:17:04 +03:00
|
|
|
const horizontal = wrapper.find(Bar);
|
2020-04-09 10:44:14 +03:00
|
|
|
|
2022-03-07 20:06:28 +03:00
|
|
|
const { datasets: [{ data, label }, highlightedData ] } = horizontal.prop('data') as any;
|
2020-04-09 10:44:14 +03:00
|
|
|
|
2021-09-18 14:17:04 +03:00
|
|
|
expect(label).toEqual('Visits');
|
2020-04-09 10:44:14 +03:00
|
|
|
expect(data).toEqual(expectedData);
|
|
|
|
expectedHighlightedData && expect(highlightedData.data).toEqual(expectedHighlightedData);
|
|
|
|
!expectedHighlightedData && expect(highlightedData).toBeUndefined();
|
|
|
|
});
|
2018-09-08 10:06:18 +03:00
|
|
|
});
|