Fixed tests

This commit is contained in:
Alejandro Celaya
2021-09-18 13:17:04 +02:00
parent 7e5397dd38
commit a15917b1ae
3 changed files with 52 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
import { Doughnut, Bar } from 'react-chartjs-2';
import { keys, values } from 'ramda';
import DefaultChart from '../../../src/visits/helpers/DefaultChart';
import { prettify } from '../../../src/utils/helpers/numbers';
@@ -15,19 +15,18 @@ describe('<DefaultChart />', () => {
afterEach(() => wrapper?.unmount());
it('renders Doughnut when is not a bar chart', () => {
wrapper = shallow(<DefaultChart title="The chart" stats={stats} />);
wrapper = shallow(<DefaultChart stats={stats} />);
const doughnut = wrapper.find(Doughnut);
const horizontal = wrapper.find(HorizontalBar);
const horizontal = wrapper.find(Bar);
const cols = wrapper.find('.col-sm-12');
expect(doughnut).toHaveLength(1);
expect(horizontal).toHaveLength(0);
const { labels, datasets } = doughnut.prop('data') as any;
const [{ title, data, backgroundColor, borderColor }] = datasets;
const { legend, scales, ...options } = doughnut.prop('options') ?? {};
const { labels, datasets } = doughnut.prop('data');
const [{ data, backgroundColor, borderColor }] = datasets;
const { plugins, scales } = doughnut.prop('options') ?? {};
expect(title).toEqual('The chart');
expect(labels).toEqual(keys(stats));
expect(data).toEqual(values(stats));
expect(datasets).toHaveLength(1);
@@ -45,36 +44,36 @@ describe('<DefaultChart />', () => {
'#463730',
]);
expect(borderColor).toEqual('white');
expect(legend).toEqual({ display: false });
expect(typeof options.legendCallback).toEqual('function');
expect(plugins.legend).toEqual({ display: false });
expect(scales).toBeUndefined();
expect(cols).toHaveLength(2);
});
it('renders HorizontalBar when is not a bar chart', () => {
wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} />);
wrapper = shallow(<DefaultChart isBarChart stats={stats} />);
const doughnut = wrapper.find(Doughnut);
const horizontal = wrapper.find(HorizontalBar);
const horizontal = wrapper.find(Bar);
const cols = wrapper.find('.col-sm-12');
expect(doughnut).toHaveLength(0);
expect(horizontal).toHaveLength(1);
const { datasets: [{ backgroundColor, borderColor }] } = horizontal.prop('data') as any;
const { legend, scales, ...options } = horizontal.prop('options') ?? {};
const { datasets: [{ backgroundColor, borderColor }] } = horizontal.prop('data');
const { plugins, scales } = horizontal.prop('options') ?? {};
expect(backgroundColor).toEqual(MAIN_COLOR_ALPHA);
expect(borderColor).toEqual(MAIN_COLOR);
expect(legend).toEqual({ display: false });
expect(typeof options.legendCallback).toEqual('boolean');
expect(plugins.legend).toEqual({ display: false });
expect(scales).toEqual({
xAxes: [
{
ticks: { beginAtZero: true, precision: 0, callback: prettify },
stacked: true,
x: {
beginAtZero: true,
stacked: true,
ticks: {
precision: 0,
callback: prettify,
},
],
yAxes: [{ stacked: true }],
},
y: { stacked: true },
});
expect(cols).toHaveLength(1);
});
@@ -86,12 +85,12 @@ describe('<DefaultChart />', () => {
[{ bar: 20, foo: 13 }, [ 110, 436 ], [ 13, 20 ]],
[ undefined, [ 123, 456 ], undefined ],
])('splits highlighted data from regular data', (highlightedStats, expectedData, expectedHighlightedData) => {
wrapper = shallow(<DefaultChart isBarChart title="The chart" stats={stats} highlightedStats={highlightedStats} />);
const horizontal = wrapper.find(HorizontalBar);
wrapper = shallow(<DefaultChart isBarChart stats={stats} highlightedStats={highlightedStats} />);
const horizontal = wrapper.find(Bar);
const { datasets: [{ data, label }, highlightedData ] } = horizontal.prop('data') as any;
const { datasets: [{ data, label }, highlightedData ] } = horizontal.prop('data');
expect(label).toEqual(highlightedStats ? 'Non-selected' : 'Visits');
expect(label).toEqual('Visits');
expect(data).toEqual(expectedData);
expectedHighlightedData && expect(highlightedData.data).toEqual(expectedHighlightedData);
!expectedHighlightedData && expect(highlightedData).toBeUndefined();