shlink-web-client/test/short-urls/SearchBar.test.tsx

80 lines
2.8 KiB
TypeScript
Raw Normal View History

import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
2021-11-09 01:23:45 +03:00
import { History, Location } from 'history';
import { match } from 'react-router';
import searchBarCreator, { SearchBarProps } from '../../src/short-urls/SearchBar';
import SearchField from '../../src/utils/SearchField';
import Tag from '../../src/tags/helpers/Tag';
import { DateRangeSelector } from '../../src/utils/dates/DateRangeSelector';
import ColorGenerator from '../../src/utils/services/ColorGenerator';
2021-11-09 01:23:45 +03:00
import { ShortUrlListRouteParams } from '../../src/short-urls/helpers/hooks';
describe('<SearchBar />', () => {
let wrapper: ShallowWrapper;
2019-04-19 13:41:59 +03:00
const listShortUrlsMock = jest.fn();
2020-12-12 15:33:21 +03:00
const SearchBar = searchBarCreator(Mock.all<ColorGenerator>());
2021-11-09 01:23:45 +03:00
const push = jest.fn();
const createWrapper = (props: Partial<SearchBarProps> = {}) => {
wrapper = shallow(
<SearchBar
listShortUrls={listShortUrlsMock}
shortUrlsListParams={{}}
history={Mock.of<History>({ push })}
location={Mock.of<Location>({ search: '' })}
match={Mock.of<match<ShortUrlListRouteParams>>({ params: { serverId: '1' } })}
{...props}
/>,
);
return wrapper;
};
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
2021-11-09 01:23:45 +03:00
it('renders some children components SearchField', () => {
const wrapper = createWrapper();
expect(wrapper.find(SearchField)).toHaveLength(1);
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) => {
const wrapper = createWrapper({ location: Mock.of<Location>({ search }) });
2021-11-09 01:23:45 +03:00
expect(wrapper.find(Tag)).toHaveLength(expectedTagComps);
});
it('updates short URLs list when search field changes', () => {
2021-11-09 01:23:45 +03:00
const wrapper = createWrapper();
const searchField = wrapper.find(SearchField);
2021-11-09 01:23:45 +03:00
expect(push).not.toHaveBeenCalled();
searchField.simulate('change', 'search-term');
expect(push).toHaveBeenCalledWith('/server/1/list-short-urls/1?search=search-term');
});
it('updates short URLs list when a tag is removed', () => {
2021-11-09 01:23:45 +03:00
const wrapper = createWrapper({ location: Mock.of<Location>({ search: 'tags=foo,bar' }) });
const tag = wrapper.find(Tag).first();
2021-11-09 01:23:45 +03:00
expect(push).not.toHaveBeenCalled();
tag.simulate('close');
2021-11-09 01:23:45 +03:00
expect(push).toHaveBeenCalledWith('/server/1/list-short-urls/1?tags=bar');
});
2020-01-14 22:12:30 +03:00
it('updates short URLs list when date range changes', () => {
2021-11-09 01:23:45 +03:00
const wrapper = createWrapper();
const dateRange = wrapper.find(DateRangeSelector);
2020-01-14 22:12:30 +03:00
expect(listShortUrlsMock).not.toHaveBeenCalled();
dateRange.simulate('datesChange', {});
2020-01-14 22:12:30 +03:00
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
});