2020-09-05 09:49:18 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2022-03-12 22:51:30 +03:00
|
|
|
import { Progress } from 'reactstrap';
|
2022-02-08 00:55:32 +03:00
|
|
|
import { sum } from 'ramda';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:55:32 +03:00
|
|
|
import { Route } from 'react-router-dom';
|
2020-09-05 09:49:18 +03:00
|
|
|
import VisitStats from '../../src/visits/VisitsStats';
|
2020-05-10 18:49:55 +03:00
|
|
|
import Message from '../../src/utils/Message';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Visit, VisitsInfo } from '../../src/visits/types';
|
2021-09-18 20:07:50 +03:00
|
|
|
import LineChartCard from '../../src/visits/charts/LineChartCard';
|
2020-12-12 22:45:23 +03:00
|
|
|
import VisitsTable from '../../src/visits/VisitsTable';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../../src/utils/Result';
|
2021-03-06 12:56:49 +03:00
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
2021-06-13 12:54:51 +03:00
|
|
|
import { SelectedServer } from '../../src/servers/data';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { SortableBarChartCard } from '../../src/visits/charts/SortableBarChartCard';
|
|
|
|
import { DoughnutChartCard } from '../../src/visits/charts/DoughnutChartCard';
|
2022-03-12 22:51:30 +03:00
|
|
|
import { ExportBtn } from '../../src/utils/ExportBtn';
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
describe('<VisitsStats />', () => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const visits = [Mock.all<Visit>(), Mock.all<Visit>(), Mock.all<Visit>()];
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2020-09-05 09:49:18 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const getVisitsMock = jest.fn();
|
2021-03-14 14:49:12 +03:00
|
|
|
const exportCsv = jest.fn();
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2020-09-05 09:49:18 +03:00
|
|
|
const createComponent = (visitsInfo: Partial<VisitsInfo>) => {
|
2020-05-10 18:49:55 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<VisitStats
|
|
|
|
getVisits={getVisitsMock}
|
2020-09-05 09:49:18 +03:00
|
|
|
visitsInfo={Mock.of<VisitsInfo>(visitsInfo)}
|
|
|
|
cancelGetVisits={() => {}}
|
2021-03-06 12:56:49 +03:00
|
|
|
settings={Mock.all<Settings>()}
|
2021-03-14 14:49:12 +03:00
|
|
|
exportCsv={exportCsv}
|
2021-06-13 12:54:51 +03:00
|
|
|
selectedServer={Mock.all<SelectedServer>()}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2020-05-10 18:49:55 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-09-05 09:49:18 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
it('renders a preloader when visits are loading', () => {
|
|
|
|
const wrapper = createComponent({ loading: true, visits: [] });
|
|
|
|
const loadingMessage = wrapper.find(Message);
|
2020-05-11 20:32:42 +03:00
|
|
|
const progress = wrapper.find(Progress);
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
expect(loadingMessage).toHaveLength(1);
|
|
|
|
expect(loadingMessage.html()).toContain('Loading...');
|
2020-05-11 20:32:42 +03:00
|
|
|
expect(progress).toHaveLength(0);
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
2020-05-11 20:32:42 +03:00
|
|
|
it('renders a warning and progress bar when loading large amounts of visits', () => {
|
|
|
|
const wrapper = createComponent({ loading: true, loadingLarge: true, visits: [], progress: 25 });
|
2020-05-10 18:49:55 +03:00
|
|
|
const loadingMessage = wrapper.find(Message);
|
2020-05-11 20:32:42 +03:00
|
|
|
const progress = wrapper.find(Progress);
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
expect(loadingMessage).toHaveLength(1);
|
|
|
|
expect(loadingMessage.html()).toContain('This is going to take a while... :S');
|
2020-05-11 20:32:42 +03:00
|
|
|
expect(progress).toHaveLength(1);
|
|
|
|
expect(progress.prop('value')).toEqual(25);
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders an error message when visits could not be loaded', () => {
|
|
|
|
const wrapper = createComponent({ loading: false, error: true, visits: [] });
|
2020-12-21 19:54:05 +03:00
|
|
|
const errorMessage = wrapper.find(Result).filterWhere((result) => result.prop('type') === 'error');
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
expect(errorMessage).toHaveLength(1);
|
|
|
|
expect(errorMessage.html()).toContain('An error occurred while loading visits :(');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a message when visits are loaded but the list is empty', () => {
|
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits: [] });
|
|
|
|
const message = wrapper.find(Message);
|
|
|
|
|
|
|
|
expect(message).toHaveLength(1);
|
|
|
|
expect(message.html()).toContain('There are no visits matching current filter :(');
|
|
|
|
});
|
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
it('renders expected amount of charts', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits });
|
2022-02-08 00:55:32 +03:00
|
|
|
const total = sum(wrapper.find(Route).map((element) => {
|
|
|
|
const ElementComponents = () => element.prop('element');
|
|
|
|
// @ts-expect-error Wrapped element
|
|
|
|
const wrappedElement = shallow(<ElementComponents />);
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2022-02-08 00:55:32 +03:00
|
|
|
const charts = wrappedElement.find(DoughnutChartCard);
|
|
|
|
const sortableCharts = wrappedElement.find(SortableBarChartCard);
|
|
|
|
const lineChart = wrappedElement.find(LineChartCard);
|
|
|
|
const table = wrappedElement.find(VisitsTable);
|
|
|
|
|
|
|
|
return charts.length + sortableCharts.length + lineChart.length + table.length;
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(total).toEqual(7);
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
2021-09-18 20:09:31 +03:00
|
|
|
it('holds the map button content generator on cities chart extraHeaderContent', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits });
|
2022-02-08 00:55:32 +03:00
|
|
|
const ElementComponent = () => wrapper.find(Route).findWhere((element) => element.prop('path') === 'by-location')
|
|
|
|
.prop('element');
|
|
|
|
const citiesChart = shallow(<ElementComponent />).find(SortableBarChartCard).find('[title="Cities"]');
|
2021-09-18 20:05:28 +03:00
|
|
|
const extraHeaderContent = citiesChart.prop('extraHeaderContent');
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
expect(extraHeaderContent).toHaveLength(1);
|
|
|
|
expect(typeof extraHeaderContent).toEqual('function');
|
|
|
|
});
|
2021-03-14 14:49:12 +03:00
|
|
|
|
|
|
|
it('exports CSV when export btn is clicked', () => {
|
|
|
|
const wrapper = createComponent({ visits });
|
2022-03-12 22:51:30 +03:00
|
|
|
const exportBtn = wrapper.find(ExportBtn).last();
|
2021-03-14 14:49:12 +03:00
|
|
|
|
|
|
|
expect(exportBtn).toHaveLength(1);
|
|
|
|
exportBtn.simulate('click');
|
|
|
|
expect(exportCsv).toHaveBeenCalled();
|
|
|
|
});
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|