2020-05-10 18:49:55 +03:00
|
|
|
import React from 'react';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2020-05-11 20:32:42 +03:00
|
|
|
import { Card, Progress } from 'reactstrap';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import VisitStats from '../../src/visits/VisitsStats';
|
2020-05-10 18:49:55 +03:00
|
|
|
import Message from '../../src/utils/Message';
|
2020-05-31 18:51:52 +03:00
|
|
|
import GraphCard from '../../src/visits/helpers/GraphCard';
|
|
|
|
import SortableBarGraph from '../../src/visits/helpers/SortableBarGraph';
|
2020-05-10 18:49:55 +03:00
|
|
|
import DateRangeRow from '../../src/utils/DateRangeRow';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Visit, VisitsInfo } from '../../src/visits/types';
|
2020-05-10 18:49:55 +03:00
|
|
|
|
|
|
|
describe('<VisitStats />', () => {
|
2020-09-05 09:49:18 +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();
|
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={() => {}}
|
|
|
|
matchMedia={() => Mock.of<MediaQueryList>({ matches: false })}
|
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: [] });
|
|
|
|
const errorMessage = wrapper.find(Card);
|
|
|
|
|
|
|
|
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 :(');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders all graphics when visits are properly loaded', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits });
|
2020-05-10 18:49:55 +03:00
|
|
|
const graphs = wrapper.find(GraphCard);
|
|
|
|
const sortableBarGraphs = wrapper.find(SortableBarGraph);
|
|
|
|
|
|
|
|
expect(graphs.length + sortableBarGraphs.length).toEqual(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reloads visits when selected dates change', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits });
|
2020-05-10 18:49:55 +03:00
|
|
|
const dateRange = wrapper.find(DateRangeRow);
|
|
|
|
|
|
|
|
dateRange.simulate('startDateChange', '2016-01-01T00:00:00+01:00');
|
|
|
|
dateRange.simulate('endDateChange', '2016-01-02T00:00:00+01:00');
|
|
|
|
dateRange.simulate('endDateChange', '2016-01-03T00:00:00+01:00');
|
|
|
|
|
|
|
|
expect(wrapper.find(DateRangeRow).prop('startDate')).toEqual('2016-01-01T00:00:00+01:00');
|
|
|
|
expect(wrapper.find(DateRangeRow).prop('endDate')).toEqual('2016-01-03T00:00:00+01:00');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('holds the map button content generator on cities graph extraHeaderContent', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createComponent({ loading: false, error: false, visits });
|
2020-05-10 18:49:55 +03:00
|
|
|
const citiesGraph = wrapper.find(SortableBarGraph).find('[title="Cities"]');
|
|
|
|
const extraHeaderContent = citiesGraph.prop('extraHeaderContent');
|
|
|
|
|
|
|
|
expect(extraHeaderContent).toHaveLength(1);
|
|
|
|
expect(typeof extraHeaderContent).toEqual('function');
|
|
|
|
});
|
|
|
|
});
|