2020-08-30 20:45:17 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2021-11-11 00:25:56 +03:00
|
|
|
import { formatISO } from 'date-fns';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
|
|
import filteringBarCreator from '../../src/short-urls/ShortUrlsFilteringBar';
|
2018-09-16 13:18:02 +03:00
|
|
|
import SearchField from '../../src/utils/SearchField';
|
2018-12-18 13:28:15 +03:00
|
|
|
import Tag from '../../src/tags/helpers/Tag';
|
2020-12-15 01:35:31 +03:00
|
|
|
import { DateRangeSelector } from '../../src/utils/dates/DateRangeSelector';
|
2020-08-30 20:45:17 +03:00
|
|
|
import ColorGenerator from '../../src/utils/services/ColorGenerator';
|
2022-02-22 21:23:57 +03:00
|
|
|
import { SelectedServer } from '../../src/servers/data';
|
2022-02-08 00:17:57 +03:00
|
|
|
|
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useNavigate: jest.fn(),
|
|
|
|
useParams: jest.fn().mockReturnValue({ serverId: '1' }),
|
|
|
|
useLocation: jest.fn().mockReturnValue({}),
|
|
|
|
}));
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2021-12-25 21:51:25 +03:00
|
|
|
describe('<ShortUrlsFilteringBar />', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2021-12-25 21:51:25 +03:00
|
|
|
const ShortUrlsFilteringBar = filteringBarCreator(Mock.all<ColorGenerator>());
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2021-11-11 00:25:56 +03:00
|
|
|
const now = new Date();
|
2022-02-08 00:17:57 +03:00
|
|
|
const createWrapper = (search = '') => {
|
|
|
|
(useLocation as any).mockReturnValue({ search });
|
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2022-02-22 21:23:57 +03:00
|
|
|
wrapper = shallow(<ShortUrlsFilteringBar selectedServer={Mock.all<SelectedServer>()} />);
|
2021-11-09 01:23:45 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2020-08-30 20:45:17 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2021-11-09 01:23:45 +03:00
|
|
|
it('renders some children components SearchField', () => {
|
|
|
|
const wrapper = createWrapper();
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
expect(wrapper.find(SearchField)).toHaveLength(1);
|
2020-12-15 01:35:31 +03:00
|
|
|
expect(wrapper.find(DateRangeSelector)).toHaveLength(1);
|
2020-01-14 22:12:30 +03:00
|
|
|
});
|
|
|
|
|
2021-11-09 01:23:45 +03:00
|
|
|
it.each([
|
|
|
|
[ 'tags=foo,bar,baz', 3 ],
|
|
|
|
[ 'tags=foo,baz', 2 ],
|
|
|
|
[ '', 0 ],
|
|
|
|
[ 'foo=bar', 0 ],
|
|
|
|
])('renders the proper amount of tags', (search, expectedTagComps) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
const wrapper = createWrapper(search);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2021-11-09 01:23:45 +03:00
|
|
|
expect(wrapper.find(Tag)).toHaveLength(expectedTagComps);
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
|
|
|
|
2021-11-11 00:25:56 +03:00
|
|
|
it('redirects to first page when search field changes', () => {
|
2021-11-09 01:23:45 +03:00
|
|
|
const wrapper = createWrapper();
|
2018-09-16 13:18:02 +03:00
|
|
|
const searchField = wrapper.find(SearchField);
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
2021-11-09 01:23:45 +03:00
|
|
|
searchField.simulate('change', 'search-term');
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith('/server/1/list-short-urls/1?search=search-term');
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
|
|
|
|
2021-11-11 00:25:56 +03:00
|
|
|
it('redirects to first page when a tag is removed', () => {
|
2022-02-08 00:17:57 +03:00
|
|
|
const wrapper = createWrapper('tags=foo,bar');
|
2018-09-16 13:18:02 +03:00
|
|
|
const tag = wrapper.find(Tag).first();
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
2018-09-16 13:18:02 +03:00
|
|
|
tag.simulate('close');
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith('/server/1/list-short-urls/1?tags=bar');
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
2020-01-14 22:12:30 +03:00
|
|
|
|
2021-11-11 00:25:56 +03:00
|
|
|
it.each([
|
|
|
|
[{ startDate: now }, `startDate=${encodeURIComponent(formatISO(now))}` ],
|
|
|
|
[{ endDate: now }, `endDate=${encodeURIComponent(formatISO(now))}` ],
|
|
|
|
[
|
|
|
|
{ startDate: now, endDate: now },
|
|
|
|
`startDate=${encodeURIComponent(formatISO(now))}&endDate=${encodeURIComponent(formatISO(now))}`,
|
|
|
|
],
|
|
|
|
])('redirects to first page when date range changes', (dates, expectedQuery) => {
|
2021-11-09 01:23:45 +03:00
|
|
|
const wrapper = createWrapper();
|
2020-12-15 01:35:31 +03:00
|
|
|
const dateRange = wrapper.find(DateRangeSelector);
|
2020-01-14 22:12:30 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
2021-11-11 00:25:56 +03:00
|
|
|
dateRange.simulate('datesChange', dates);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(`/server/1/list-short-urls/1?${expectedQuery}`);
|
2020-01-14 22:12:30 +03:00
|
|
|
});
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|