2020-09-03 21:34:22 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2018-09-08 10:06:18 +03:00
|
|
|
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
|
|
|
import { keys, values } from 'ramda';
|
2020-06-06 11:35:13 +03:00
|
|
|
import DefaultChart from '../../../src/visits/helpers/DefaultChart';
|
2020-09-17 19:05:26 +03:00
|
|
|
import { prettify } from '../../../src/utils/helpers/numbers';
|
2018-09-08 10:06:18 +03:00
|
|
|
|
2020-06-06 11:35:13 +03:00
|
|
|
describe('<DefaultChart />', () => {
|
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
|
|
|
|
|
|
|
it('renders Doughnut when is not a bar chart', () => {
|
2020-06-06 11:35:13 +03:00
|
|
|
wrapper = shallow(<DefaultChart title="The chart" stats={stats} />);
|
2018-09-08 10:06:18 +03:00
|
|
|
const doughnut = wrapper.find(Doughnut);
|
|
|
|
const horizontal = wrapper.find(HorizontalBar);
|
2020-06-06 12:58:25 +03:00
|
|
|
const cols = wrapper.find('.col-sm-12');
|
2018-09-08 10:06:18 +03:00
|
|
|
|
|
|
|
expect(doughnut).toHaveLength(1);
|
|
|
|
expect(horizontal).toHaveLength(0);
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const { labels, datasets } = doughnut.prop('data') as any;
|
2020-04-09 10:44:14 +03:00
|
|
|
const [{ title, data, backgroundColor, borderColor }] = datasets;
|
2020-09-03 21:34:22 +03:00
|
|
|
const { legend, legendCallback, scales } = doughnut.prop('options') ?? {};
|
2018-09-08 10:06:18 +03:00
|
|
|
|
|
|
|
expect(title).toEqual('The chart');
|
|
|
|
expect(labels).toEqual(keys(stats));
|
|
|
|
expect(data).toEqual(values(stats));
|
2020-04-09 10:44:14 +03:00
|
|
|
expect(datasets).toHaveLength(1);
|
2018-09-08 10:06:18 +03:00
|
|
|
expect(backgroundColor).toEqual([
|
|
|
|
'#97BBCD',
|
|
|
|
'#F7464A',
|
|
|
|
'#46BFBD',
|
|
|
|
'#FDB45C',
|
|
|
|
'#949FB1',
|
2020-04-10 20:16:44 +03:00
|
|
|
'#57A773',
|
|
|
|
'#414066',
|
|
|
|
'#08B2E3',
|
|
|
|
'#B6C454',
|
|
|
|
'#DCDCDC',
|
|
|
|
'#463730',
|
2018-09-08 10:06:18 +03:00
|
|
|
]);
|
|
|
|
expect(borderColor).toEqual('white');
|
2020-06-06 12:58:25 +03:00
|
|
|
expect(legend).toEqual({ display: false });
|
|
|
|
expect(typeof legendCallback).toEqual('function');
|
2019-03-16 11:02:10 +03:00
|
|
|
expect(scales).toBeUndefined();
|
2020-06-06 12:58:25 +03:00
|
|
|
expect(cols).toHaveLength(2);
|
2018-09-08 10:06:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders HorizontalBar when is not a bar chart', () => {
|
2020-06-06 11:35:13 +03:00
|
|
|
wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} />);
|
2018-09-08 10:06:18 +03:00
|
|
|
const doughnut = wrapper.find(Doughnut);
|
|
|
|
const horizontal = wrapper.find(HorizontalBar);
|
2020-06-06 12:58:25 +03:00
|
|
|
const cols = wrapper.find('.col-sm-12');
|
2018-09-08 10:06:18 +03:00
|
|
|
|
|
|
|
expect(doughnut).toHaveLength(0);
|
|
|
|
expect(horizontal).toHaveLength(1);
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const { datasets: [{ backgroundColor, borderColor }] } = horizontal.prop('data') as any;
|
|
|
|
const { legend, legendCallback, scales } = horizontal.prop('options') ?? {};
|
2018-09-08 10:06:18 +03:00
|
|
|
|
|
|
|
expect(backgroundColor).toEqual('rgba(70, 150, 229, 0.4)');
|
|
|
|
expect(borderColor).toEqual('rgba(70, 150, 229, 1)');
|
2018-09-08 21:34:04 +03:00
|
|
|
expect(legend).toEqual({ display: false });
|
2020-06-06 12:58:25 +03:00
|
|
|
expect(legendCallback).toEqual(false);
|
2018-09-08 21:34:04 +03:00
|
|
|
expect(scales).toEqual({
|
|
|
|
xAxes: [
|
|
|
|
{
|
2020-09-17 19:05:26 +03:00
|
|
|
ticks: { beginAtZero: true, precision: 0, callback: prettify },
|
2020-04-04 21:16:20 +03:00
|
|
|
stacked: true,
|
2018-09-08 21:34:04 +03:00
|
|
|
},
|
|
|
|
],
|
2020-04-04 21:16:20 +03:00
|
|
|
yAxes: [{ stacked: true }],
|
2018-09-08 21:34:04 +03:00
|
|
|
});
|
2020-06-06 12:58:25 +03:00
|
|
|
expect(cols).toHaveLength(1);
|
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) => {
|
2020-06-06 11:35:13 +03:00
|
|
|
wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} highlightedStats={highlightedStats} />);
|
2020-04-09 10:44:14 +03:00
|
|
|
const horizontal = wrapper.find(HorizontalBar);
|
|
|
|
|
2020-09-03 21:34:22 +03:00
|
|
|
const { datasets: [{ data, label }, highlightedData ] } = horizontal.prop('data') as any;
|
2020-04-09 10:44:14 +03:00
|
|
|
|
2020-05-30 10:25:15 +03:00
|
|
|
expect(label).toEqual(highlightedStats ? 'Non-selected' : '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
|
|
|
});
|