2020-09-05 09:49:18 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2021-06-13 12:38:13 +03:00
|
|
|
import VisitsTable, { VisitsTableProps } from '../../src/visits/VisitsTable';
|
2020-04-09 11:21:38 +03:00
|
|
|
import { rangeOf } from '../../src/utils/utils';
|
2022-05-06 22:20:14 +03:00
|
|
|
import { SimplePaginator } from '../../src/common/SimplePaginator';
|
2022-05-14 17:41:09 +03:00
|
|
|
import { SearchField } from '../../src/utils/SearchField';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { NormalizedVisit } from '../../src/visits/types';
|
2021-06-13 12:38:13 +03:00
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
|
|
|
import { SemVer } from '../../src/utils/helpers/version';
|
2020-04-09 11:21:38 +03:00
|
|
|
|
|
|
|
describe('<VisitsTable />', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const matchMedia = () => Mock.of<MediaQueryList>({ matches: false });
|
2020-04-10 12:59:53 +03:00
|
|
|
const setSelectedVisits = jest.fn();
|
2020-09-05 09:49:18 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2021-06-13 12:38:13 +03:00
|
|
|
const wrapperFactory = (props: Partial<VisitsTableProps> = {}) => {
|
2020-04-10 12:59:53 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<VisitsTable
|
2021-06-13 12:38:13 +03:00
|
|
|
visits={[]}
|
2021-06-13 12:07:32 +03:00
|
|
|
selectedServer={Mock.all<SelectedServer>()}
|
2021-06-13 12:38:13 +03:00
|
|
|
{...props}
|
|
|
|
matchMedia={matchMedia}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2020-04-10 12:59:53 +03:00
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
2021-06-13 12:38:13 +03:00
|
|
|
const createWrapper = (visits: NormalizedVisit[], selectedVisits: NormalizedVisit[] = []) => wrapperFactory(
|
|
|
|
{ visits, selectedVisits },
|
|
|
|
);
|
2021-06-13 12:49:53 +03:00
|
|
|
const createOrphanVisitsWrapper = (isOrphanVisits: boolean, version: SemVer) => wrapperFactory({
|
|
|
|
isOrphanVisits,
|
|
|
|
selectedServer: Mock.of<ReachableServer>({ printableVersion: version, version }),
|
|
|
|
});
|
2021-06-13 12:38:13 +03:00
|
|
|
const createServerVersionWrapper = (version: SemVer) => wrapperFactory({
|
|
|
|
selectedServer: Mock.of<ReachableServer>({ printableVersion: version, version }),
|
|
|
|
});
|
2021-06-13 12:49:53 +03:00
|
|
|
const createWrapperWithBots = () => wrapperFactory({
|
|
|
|
selectedServer: Mock.of<ReachableServer>({ printableVersion: '2.7.0', version: '2.7.0' }),
|
|
|
|
visits: [
|
|
|
|
Mock.of<NormalizedVisit>({ potentialBot: false }),
|
|
|
|
Mock.of<NormalizedVisit>({ potentialBot: true }),
|
|
|
|
],
|
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2020-09-05 09:49:18 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2021-06-13 12:38:13 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['2.6.0' as SemVer, ['Date', 'Country', 'City', 'Browser', 'OS', 'Referrer']],
|
|
|
|
['2.7.0' as SemVer, ['fa-robot', 'Date', 'Country', 'City', 'Browser', 'OS', 'Referrer']],
|
2021-06-13 12:38:13 +03:00
|
|
|
])('renders columns as expected', (version, expectedColumns) => {
|
|
|
|
const wrapper = createServerVersionWrapper(version);
|
2020-04-09 11:21:38 +03:00
|
|
|
const th = wrapper.find('thead').find('th');
|
|
|
|
|
2021-06-13 12:38:13 +03:00
|
|
|
expect(th).toHaveLength(expectedColumns.length + 1);
|
|
|
|
expectedColumns.forEach((column, index) => {
|
|
|
|
expect(th.at(index + 1).html()).toContain(column);
|
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows warning when no visits are found', () => {
|
|
|
|
const wrapper = createWrapper([]);
|
|
|
|
const td = wrapper.find('tbody').find('td');
|
|
|
|
|
|
|
|
expect(td).toHaveLength(1);
|
|
|
|
expect(td.text()).toContain('No visits found with current filtering');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[50, 3],
|
|
|
|
[21, 2],
|
|
|
|
[30, 2],
|
|
|
|
[60, 3],
|
|
|
|
[115, 6],
|
2020-04-09 11:21:38 +03:00
|
|
|
])('renders the expected amount of pages', (visitsCount, expectedAmountOfPages) => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createWrapper(
|
|
|
|
rangeOf(visitsCount, () => Mock.of<NormalizedVisit>({ browser: '', date: '', referer: '' })),
|
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
const tr = wrapper.find('tbody').find('tr');
|
|
|
|
const paginator = wrapper.find(SimplePaginator);
|
|
|
|
|
|
|
|
expect(tr).toHaveLength(20);
|
|
|
|
expect(paginator.prop('pagesCount')).toEqual(expectedAmountOfPages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
2022-03-26 14:17:42 +03:00
|
|
|
rangeOf(20, (value) => [value]),
|
2020-04-09 11:21:38 +03:00
|
|
|
)('does not render footer when there is only one page to render', (visitsCount) => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createWrapper(
|
|
|
|
rangeOf(visitsCount, () => Mock.of<NormalizedVisit>({ browser: '', date: '', referer: '' })),
|
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
const tr = wrapper.find('tbody').find('tr');
|
|
|
|
const paginator = wrapper.find(SimplePaginator);
|
|
|
|
|
|
|
|
expect(tr).toHaveLength(visitsCount);
|
|
|
|
expect(paginator).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2020-04-09 11:56:54 +03:00
|
|
|
it('selected rows are highlighted', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const visits = rangeOf(10, () => Mock.of<NormalizedVisit>({ browser: '', date: '', referer: '' }));
|
2020-04-10 12:59:53 +03:00
|
|
|
const wrapper = createWrapper(
|
|
|
|
visits,
|
2022-03-26 14:17:42 +03:00
|
|
|
[visits[1], visits[2]],
|
2020-04-10 12:59:53 +03:00
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2020-04-09 11:56:54 +03:00
|
|
|
expect(wrapper.find('.text-primary')).toHaveLength(3);
|
2021-02-19 22:18:02 +03:00
|
|
|
expect(wrapper.find('.table-active')).toHaveLength(2);
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
// Select one extra
|
|
|
|
wrapper.find('tr').at(5).simulate('click');
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith([visits[1], visits[2], visits[4]]);
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
// Deselect one
|
2020-04-09 11:56:54 +03:00
|
|
|
wrapper.find('tr').at(3).simulate('click');
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith([visits[1]]);
|
2020-04-09 11:56:54 +03:00
|
|
|
|
|
|
|
// Select all
|
|
|
|
wrapper.find('thead').find('th').at(0).simulate('click');
|
2020-04-10 12:59:53 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith(visits);
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('orders visits when column is clicked', () => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const wrapper = createWrapper(rangeOf(9, (index) => Mock.of<NormalizedVisit>({
|
2020-04-10 12:59:53 +03:00
|
|
|
browser: '',
|
2020-04-09 11:21:38 +03:00
|
|
|
date: `${9 - index}`,
|
|
|
|
referer: `${index}`,
|
2020-04-10 12:59:53 +03:00
|
|
|
country: `Country_${index}`,
|
2020-04-09 11:21:38 +03:00
|
|
|
})));
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td')
|
|
|
|
.at(2)
|
|
|
|
.text()).toContain('Country_1');
|
2020-04-09 11:21:38 +03:00
|
|
|
wrapper.find('thead').find('th').at(1).simulate('click'); // Date column ASC
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td')
|
|
|
|
.at(2)
|
|
|
|
.text()).toContain('Country_9');
|
2020-04-09 11:21:38 +03:00
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - ASC
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td')
|
|
|
|
.at(2)
|
|
|
|
.text()).toContain('Country_1');
|
2020-04-09 11:21:38 +03:00
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - DESC
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td')
|
|
|
|
.at(2)
|
|
|
|
.text()).toContain('Country_9');
|
2020-04-09 11:21:38 +03:00
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - reset
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td')
|
|
|
|
.at(2)
|
|
|
|
.text()).toContain('Country_1');
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('filters list when writing in search box', () => {
|
|
|
|
const wrapper = createWrapper([
|
2020-09-05 09:49:18 +03:00
|
|
|
...rangeOf(7, () => Mock.of<NormalizedVisit>({ browser: 'aaa', date: 'aaa', referer: 'aaa' })),
|
|
|
|
...rangeOf(2, () => Mock.of<NormalizedVisit>({ browser: 'bbb', date: 'bbb', referer: 'bbb' })),
|
2020-04-09 11:21:38 +03:00
|
|
|
]);
|
|
|
|
const searchField = wrapper.find(SearchField);
|
|
|
|
|
|
|
|
expect(wrapper.find('tbody').find('tr')).toHaveLength(7 + 2);
|
|
|
|
searchField.simulate('change', 'aa');
|
|
|
|
expect(wrapper.find('tbody').find('tr')).toHaveLength(7);
|
|
|
|
searchField.simulate('change', 'bb');
|
|
|
|
expect(wrapper.find('tbody').find('tr')).toHaveLength(2);
|
|
|
|
searchField.simulate('change', '');
|
|
|
|
expect(wrapper.find('tbody').find('tr')).toHaveLength(7 + 2);
|
|
|
|
});
|
2021-03-28 17:06:37 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[true, '2.6.0' as SemVer, 8],
|
|
|
|
[false, '2.6.0' as SemVer, 7],
|
|
|
|
[true, '2.7.0' as SemVer, 9],
|
|
|
|
[false, '2.7.0' as SemVer, 8],
|
2021-06-13 12:49:53 +03:00
|
|
|
])('displays proper amount of columns for orphan and non-orphan visits', (isOrphanVisits, version, expectedCols) => {
|
|
|
|
const wrapper = createOrphanVisitsWrapper(isOrphanVisits, version);
|
2021-03-28 17:06:37 +03:00
|
|
|
const rowsWithColspan = wrapper.find('[colSpan]');
|
|
|
|
const cols = wrapper.find('th');
|
|
|
|
|
|
|
|
expect(cols).toHaveLength(expectedCols);
|
|
|
|
expect(rowsWithColspan).toHaveLength(2);
|
|
|
|
rowsWithColspan.forEach((row) => expect(row.prop('colSpan')).toEqual(expectedCols));
|
|
|
|
});
|
2021-06-13 12:49:53 +03:00
|
|
|
|
|
|
|
it('displays bots icon when a visit is a potential bot', () => {
|
|
|
|
const wrapper = createWrapperWithBots();
|
|
|
|
const rows = wrapper.find('tbody').find('tr');
|
|
|
|
|
|
|
|
expect(rows.at(0).find('td').at(1).text()).not.toContain('FontAwesomeIcon');
|
|
|
|
expect(rows.at(1).find('td').at(1).text()).toContain('FontAwesomeIcon');
|
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|