2020-04-09 11:21:38 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import VisitsTable from '../../src/visits/VisitsTable';
|
|
|
|
import { rangeOf } from '../../src/utils/utils';
|
|
|
|
import SimplePaginator from '../../src/common/SimplePaginator';
|
|
|
|
import SearchField from '../../src/utils/SearchField';
|
|
|
|
|
|
|
|
describe('<VisitsTable />', () => {
|
|
|
|
const matchMedia = () => ({ matches: false });
|
2020-04-10 12:59:53 +03:00
|
|
|
const setSelectedVisits = jest.fn();
|
2020-04-09 11:21:38 +03:00
|
|
|
let wrapper;
|
2020-04-10 12:59:53 +03:00
|
|
|
const createWrapper = (visits, selectedVisits = []) => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<VisitsTable
|
|
|
|
visits={visits}
|
|
|
|
selectedVisits={selectedVisits}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
|
|
|
matchMedia={matchMedia}
|
|
|
|
/>
|
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-04-10 12:59:53 +03:00
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
wrapper && wrapper.unmount();
|
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
|
|
|
|
it('renders columns as expected', () => {
|
|
|
|
const wrapper = createWrapper([]);
|
|
|
|
const th = wrapper.find('thead').find('th');
|
|
|
|
|
|
|
|
expect(th).toHaveLength(7);
|
|
|
|
expect(th.at(1).text()).toContain('Date');
|
|
|
|
expect(th.at(2).text()).toContain('Country');
|
|
|
|
expect(th.at(3).text()).toContain('City');
|
|
|
|
expect(th.at(4).text()).toContain('Browser');
|
|
|
|
expect(th.at(5).text()).toContain('OS');
|
|
|
|
expect(th.at(6).text()).toContain('Referrer');
|
|
|
|
});
|
|
|
|
|
|
|
|
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([
|
|
|
|
[ 50, 3 ],
|
|
|
|
[ 21, 2 ],
|
|
|
|
[ 30, 2 ],
|
|
|
|
[ 60, 3 ],
|
|
|
|
[ 115, 6 ],
|
|
|
|
])('renders the expected amount of pages', (visitsCount, expectedAmountOfPages) => {
|
2020-04-10 12:59:53 +03:00
|
|
|
const wrapper = createWrapper(rangeOf(visitsCount, () => ({ 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(
|
|
|
|
rangeOf(20, (value) => [ value ])
|
|
|
|
)('does not render footer when there is only one page to render', (visitsCount) => {
|
2020-04-10 12:59:53 +03:00
|
|
|
const wrapper = createWrapper(rangeOf(visitsCount, () => ({ 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-04-10 12:59:53 +03:00
|
|
|
const visits = rangeOf(10, () => ({ browser: '', date: '', referer: '' }));
|
|
|
|
const wrapper = createWrapper(
|
|
|
|
visits,
|
|
|
|
[ visits[1], visits[2] ],
|
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2020-04-09 11:56:54 +03:00
|
|
|
expect(wrapper.find('.text-primary')).toHaveLength(3);
|
|
|
|
expect(wrapper.find('.table-primary')).toHaveLength(2);
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
// Select one extra
|
|
|
|
wrapper.find('tr').at(5).simulate('click');
|
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith([ visits[1], visits[2], visits[4] ]);
|
|
|
|
|
|
|
|
// Deselect one
|
2020-04-09 11:56:54 +03:00
|
|
|
wrapper.find('tr').at(3).simulate('click');
|
2020-04-10 12:59:53 +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', () => {
|
|
|
|
const wrapper = createWrapper(rangeOf(9, (index) => ({
|
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
|
|
|
})));
|
|
|
|
|
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td').at(2).text()).toContain('Country_1');
|
|
|
|
wrapper.find('thead').find('th').at(1).simulate('click'); // Date column ASC
|
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td').at(2).text()).toContain('Country_9');
|
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - ASC
|
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td').at(2).text()).toContain('Country_1');
|
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - DESC
|
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td').at(2).text()).toContain('Country_9');
|
|
|
|
wrapper.find('thead').find('th').at(6).simulate('click'); // Referer column - reset
|
|
|
|
expect(wrapper.find('tbody').find('tr').at(0).find('td').at(2).text()).toContain('Country_1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters list when writing in search box', () => {
|
|
|
|
const wrapper = createWrapper([
|
2020-04-10 12:59:53 +03:00
|
|
|
...rangeOf(7, () => ({ browser: 'aaa', date: 'aaa', referer: 'aaa' })),
|
|
|
|
...rangeOf(2, () => ({ 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);
|
|
|
|
});
|
|
|
|
});
|