2022-07-17 11:24:34 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-07-17 11:24:34 +03:00
|
|
|
import { Router } from 'react-router-dom';
|
|
|
|
import { createMemoryHistory } from 'history';
|
2022-05-28 11:34:12 +03:00
|
|
|
import { VisitsStats } from '../../src/visits/VisitsStats';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { Visit, VisitsInfo } from '../../src/visits/types';
|
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';
|
2022-07-17 11:24:34 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
|
|
|
import { rangeOf } from '../../src/utils/utils';
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
describe('<VisitsStats />', () => {
|
2022-07-17 11:24:34 +03:00
|
|
|
const visits = rangeOf(3, () => Mock.of<Visit>({ date: '2020-01-01' }));
|
2020-09-05 09:49:18 +03:00
|
|
|
const getVisitsMock = jest.fn();
|
2021-03-14 14:49:12 +03:00
|
|
|
const exportCsv = jest.fn();
|
2022-07-17 11:24:34 +03:00
|
|
|
const setUp = (visitsInfo: Partial<VisitsInfo>, activeRoute = '/by-time') => {
|
|
|
|
const history = createMemoryHistory();
|
|
|
|
history.push(activeRoute);
|
|
|
|
|
|
|
|
return renderWithEvents(
|
|
|
|
<Router location={history.location} navigator={history}>
|
|
|
|
<VisitsStats
|
|
|
|
getVisits={getVisitsMock}
|
|
|
|
visitsInfo={Mock.of<VisitsInfo>(visitsInfo)}
|
|
|
|
cancelGetVisits={() => {}}
|
|
|
|
settings={Mock.all<Settings>()}
|
|
|
|
exportCsv={exportCsv}
|
|
|
|
selectedServer={Mock.all<SelectedServer>()}
|
|
|
|
/>
|
|
|
|
</Router>,
|
2020-05-10 18:49:55 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
it('renders a preloader when visits are loading', () => {
|
2022-07-17 11:24:34 +03:00
|
|
|
setUp({ loading: true, visits: [] });
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText(/^This is going to take a while/)).not.toBeInTheDocument();
|
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', () => {
|
2022-07-17 11:24:34 +03:00
|
|
|
setUp({ loading: true, loadingLarge: true, visits: [], progress: 25 });
|
2020-05-10 18:49:55 +03:00
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText(/^This is going to take a while/)).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('progressbar')).toHaveAttribute('aria-valuenow', '25');
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders an error message when visits could not be loaded', () => {
|
2022-07-17 11:24:34 +03:00
|
|
|
setUp({ loading: false, error: true, visits: [] });
|
|
|
|
expect(screen.getByText('An error occurred while loading visits :(')).toBeInTheDocument();
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a message when visits are loaded but the list is empty', () => {
|
2022-07-17 11:24:34 +03:00
|
|
|
setUp({ loading: false, error: false, visits: [] });
|
|
|
|
expect(screen.getByText('There are no visits matching current filter')).toBeInTheDocument();
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
it.each([
|
|
|
|
['/by-time', 2],
|
|
|
|
['/by-context', 4],
|
|
|
|
['/by-location', 3],
|
|
|
|
['/list', 1],
|
|
|
|
])('renders expected amount of charts', (route, expectedCharts) => {
|
|
|
|
const { container } = setUp({ loading: false, error: false, visits }, route);
|
|
|
|
expect(container.querySelectorAll('.card')).toHaveLength(expectedCharts);
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
it('holds the map button on cities chart header', () => {
|
|
|
|
setUp({ loading: false, error: false, visits }, '/by-location');
|
|
|
|
expect(
|
|
|
|
screen.getAllByRole('img', { hidden: true }).some((icon) => icon.classList.contains('fa-map-location-dot')),
|
|
|
|
).toEqual(true);
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|
2021-03-14 14:49:12 +03:00
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
it('exports CSV when export btn is clicked', async () => {
|
|
|
|
const { user } = setUp({ visits });
|
2021-03-14 14:49:12 +03:00
|
|
|
|
2022-07-17 11:24:34 +03:00
|
|
|
expect(exportCsv).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole('button', { name: /Export/ }));
|
2021-03-14 14:49:12 +03:00
|
|
|
expect(exportCsv).toHaveBeenCalled();
|
|
|
|
});
|
2020-05-10 18:49:55 +03:00
|
|
|
});
|