2020-05-30 11:41:46 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import { CardHeader, DropdownItem } from 'reactstrap';
|
|
|
|
import { Line } from 'react-chartjs-2';
|
2020-05-31 21:03:59 +03:00
|
|
|
import moment from 'moment';
|
2020-05-30 11:41:46 +03:00
|
|
|
import LineChartCard from '../../../src/visits/helpers/LineChartCard';
|
2020-07-14 17:05:00 +03:00
|
|
|
import ToggleSwitch from '../../../src/utils/ToggleSwitch';
|
2020-05-30 11:41:46 +03:00
|
|
|
|
2020-05-30 18:39:08 +03:00
|
|
|
describe('<LineChartCard />', () => {
|
2020-05-30 11:41:46 +03:00
|
|
|
let wrapper;
|
|
|
|
const createWrapper = (visits = [], highlightedVisits = []) => {
|
|
|
|
wrapper = shallow(<LineChartCard title="Cool title" visits={visits} highlightedVisits={highlightedVisits} />);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => wrapper && wrapper.unmount());
|
|
|
|
|
|
|
|
it('renders provided title', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
const header = wrapper.find(CardHeader);
|
|
|
|
|
|
|
|
expect(header.html()).toContain('Cool title');
|
|
|
|
});
|
|
|
|
|
2020-05-31 21:03:59 +03:00
|
|
|
it.each([
|
|
|
|
[[], 'monthly' ],
|
|
|
|
[[{ date: moment().subtract(1, 'day').format() }], 'hourly' ],
|
|
|
|
[[{ date: moment().subtract(3, 'day').format() }], 'daily' ],
|
|
|
|
[[{ date: moment().subtract(2, 'month').format() }], 'weekly' ],
|
|
|
|
[[{ date: moment().subtract(6, 'month').format() }], 'weekly' ],
|
|
|
|
[[{ date: moment().subtract(7, 'month').format() }], 'monthly' ],
|
|
|
|
[[{ date: moment().subtract(1, 'year').format() }], 'monthly' ],
|
|
|
|
])('renders group menu and selects proper grouping item based on visits dates', (visits, expectedActiveItem) => {
|
|
|
|
const wrapper = createWrapper(visits);
|
2020-05-30 11:41:46 +03:00
|
|
|
const items = wrapper.find(DropdownItem);
|
|
|
|
|
|
|
|
expect(items).toHaveLength(4);
|
|
|
|
expect(items.at(0).prop('children')).toEqual('Month');
|
2020-05-31 21:03:59 +03:00
|
|
|
expect(items.at(0).prop('active')).toEqual(expectedActiveItem === 'monthly');
|
2020-05-30 11:41:46 +03:00
|
|
|
expect(items.at(1).prop('children')).toEqual('Week');
|
2020-05-31 21:03:59 +03:00
|
|
|
expect(items.at(1).prop('active')).toEqual(expectedActiveItem === 'weekly');
|
2020-05-30 11:41:46 +03:00
|
|
|
expect(items.at(2).prop('children')).toEqual('Day');
|
2020-05-31 21:03:59 +03:00
|
|
|
expect(items.at(2).prop('active')).toEqual(expectedActiveItem === 'daily');
|
2020-05-30 11:41:46 +03:00
|
|
|
expect(items.at(3).prop('children')).toEqual('Hour');
|
2020-05-31 21:03:59 +03:00
|
|
|
expect(items.at(3).prop('active')).toEqual(expectedActiveItem === 'hourly');
|
2020-05-30 11:41:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders chart with expected options', () => {
|
|
|
|
const wrapper = createWrapper();
|
|
|
|
const chart = wrapper.find(Line);
|
|
|
|
|
|
|
|
expect(chart.prop('options')).toEqual({
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
legend: { display: false },
|
|
|
|
scales: {
|
|
|
|
yAxes: [
|
|
|
|
{
|
|
|
|
ticks: { beginAtZero: true, precision: 0 },
|
|
|
|
},
|
|
|
|
],
|
2020-05-30 18:39:08 +03:00
|
|
|
xAxes: [
|
|
|
|
{
|
|
|
|
scaleLabel: { display: true, labelString: 'Month' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
intersect: false,
|
|
|
|
axis: 'x',
|
2020-05-30 11:41:46 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[[{}], [], 1 ],
|
|
|
|
[[{}], [{}], 2 ],
|
|
|
|
])('renders chart with expected data', (visits, highlightedVisits, expectedLines) => {
|
|
|
|
const wrapper = createWrapper(visits, highlightedVisits);
|
|
|
|
const chart = wrapper.find(Line);
|
|
|
|
const { datasets } = chart.prop('data');
|
|
|
|
|
|
|
|
expect(datasets).toHaveLength(expectedLines);
|
|
|
|
});
|
2020-05-30 18:39:08 +03:00
|
|
|
|
|
|
|
it('includes stats for visits with no dates if selected', () => {
|
|
|
|
const wrapper = createWrapper([
|
|
|
|
{ date: '2016-04-01' },
|
|
|
|
{ date: '2016-01-01' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(wrapper.find(Line).prop('data').labels).toHaveLength(2);
|
2020-07-14 17:05:00 +03:00
|
|
|
wrapper.find(ToggleSwitch).simulate('change');
|
2020-05-30 18:39:08 +03:00
|
|
|
expect(wrapper.find(Line).prop('data').labels).toHaveLength(4);
|
|
|
|
});
|
2020-05-30 11:41:46 +03:00
|
|
|
});
|