2020-09-04 19:43:26 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2020-05-30 11:41:46 +03:00
|
|
|
import { CardHeader, DropdownItem } from 'reactstrap';
|
|
|
|
import { Line } from 'react-chartjs-2';
|
2021-06-24 21:13:06 +03:00
|
|
|
import { formatISO, subDays, subMonths, subYears } from 'date-fns';
|
2020-09-04 19:43:26 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-09-18 20:07:50 +03:00
|
|
|
import LineChartCard from '../../../src/visits/charts/LineChartCard';
|
2020-07-14 17:05:00 +03:00
|
|
|
import ToggleSwitch from '../../../src/utils/ToggleSwitch';
|
2020-09-17 19:05:26 +03:00
|
|
|
import { NormalizedVisit } from '../../../src/visits/types';
|
|
|
|
import { prettify } from '../../../src/utils/helpers/numbers';
|
2021-09-18 14:17:04 +03:00
|
|
|
import { pointerOnHover, renderChartLabel } from '../../../src/utils/helpers/charts';
|
2020-05-30 11:41:46 +03:00
|
|
|
|
2020-05-30 18:39:08 +03:00
|
|
|
describe('<LineChartCard />', () => {
|
2020-09-04 19:43:26 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2020-09-17 19:05:26 +03:00
|
|
|
const createWrapper = (visits: NormalizedVisit[] = [], highlightedVisits: NormalizedVisit[] = []) => {
|
2020-05-30 11:41:46 +03:00
|
|
|
wrapper = shallow(<LineChartCard title="Cool title" visits={visits} highlightedVisits={highlightedVisits} />);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2020-05-30 11:41:46 +03:00
|
|
|
|
|
|
|
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([
|
2022-03-26 14:17:42 +03:00
|
|
|
[[], 'monthly'],
|
|
|
|
[[{ date: formatISO(subDays(new Date(), 1)) }], 'hourly'],
|
|
|
|
[[{ date: formatISO(subDays(new Date(), 3)) }], 'daily'],
|
|
|
|
[[{ date: formatISO(subMonths(new Date(), 2)) }], 'weekly'],
|
|
|
|
[[{ date: formatISO(subMonths(new Date(), 6)) }], 'weekly'],
|
|
|
|
[[{ date: formatISO(subMonths(new Date(), 7)) }], 'monthly'],
|
|
|
|
[[{ date: formatISO(subYears(new Date(), 1)) }], 'monthly'],
|
2020-05-31 21:03:59 +03:00
|
|
|
])('renders group menu and selects proper grouping item based on visits dates', (visits, expectedActiveItem) => {
|
2020-09-17 19:05:26 +03:00
|
|
|
const wrapper = createWrapper(visits.map((visit) => Mock.of<NormalizedVisit>(visit)));
|
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);
|
|
|
|
|
2020-09-20 12:43:24 +03:00
|
|
|
expect(chart.prop('options')).toEqual(expect.objectContaining({
|
2020-05-30 11:41:46 +03:00
|
|
|
maintainAspectRatio: false,
|
2021-09-18 14:17:04 +03:00
|
|
|
plugins: {
|
|
|
|
legend: { display: false },
|
|
|
|
tooltip: {
|
|
|
|
intersect: false,
|
|
|
|
axis: 'x',
|
|
|
|
callbacks: { label: renderChartLabel },
|
|
|
|
},
|
|
|
|
},
|
2020-05-30 11:41:46 +03:00
|
|
|
scales: {
|
2021-09-18 14:17:04 +03:00
|
|
|
y: {
|
|
|
|
beginAtZero: true,
|
|
|
|
ticks: {
|
|
|
|
precision: 0,
|
|
|
|
callback: prettify,
|
2020-05-30 18:39:08 +03:00
|
|
|
},
|
2021-09-18 14:17:04 +03:00
|
|
|
},
|
|
|
|
x: {
|
|
|
|
title: { display: true, text: 'Month' },
|
|
|
|
},
|
2020-05-30 18:39:08 +03:00
|
|
|
},
|
2021-09-18 14:17:04 +03:00
|
|
|
onHover: pointerOnHover,
|
2020-09-20 12:43:24 +03:00
|
|
|
}));
|
2020-05-30 11:41:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[[Mock.of<NormalizedVisit>({ date: '2016-04-01' })], [], 1],
|
|
|
|
[[Mock.of<NormalizedVisit>({ date: '2016-04-01' })], [Mock.of<NormalizedVisit>({ date: '2016-04-01' })], 2],
|
2020-05-30 11:41:46 +03:00
|
|
|
])('renders chart with expected data', (visits, highlightedVisits, expectedLines) => {
|
|
|
|
const wrapper = createWrapper(visits, highlightedVisits);
|
|
|
|
const chart = wrapper.find(Line);
|
2022-03-07 20:06:28 +03:00
|
|
|
const { datasets } = chart.prop('data') as any;
|
2020-05-30 11:41:46 +03:00
|
|
|
|
|
|
|
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([
|
2020-09-17 19:05:26 +03:00
|
|
|
Mock.of<NormalizedVisit>({ date: '2016-04-01' }),
|
|
|
|
Mock.of<NormalizedVisit>({ date: '2016-01-01' }),
|
2020-05-30 18:39:08 +03:00
|
|
|
]);
|
|
|
|
|
2022-03-07 20:06:28 +03:00
|
|
|
expect((wrapper.find(Line).prop('data') as any).labels).toHaveLength(2);
|
2020-07-14 17:05:00 +03:00
|
|
|
wrapper.find(ToggleSwitch).simulate('change');
|
2022-03-07 20:06:28 +03:00
|
|
|
expect((wrapper.find(Line).prop('data') as any).labels).toHaveLength(4);
|
2020-05-30 18:39:08 +03:00
|
|
|
});
|
2020-05-30 11:41:46 +03:00
|
|
|
});
|