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

74 lines
2.6 KiB
TypeScript
Raw Normal View History

import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
2018-12-18 00:32:51 +03:00
import searchBarCreator 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';
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>());
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it('renders a SearchField', () => {
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
expect(wrapper.find(SearchField)).toHaveLength(1);
});
it('renders a DateRangeSelector', () => {
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
expect(wrapper.find(DateRangeSelector)).toHaveLength(1);
2020-01-14 22:12:30 +03:00
});
it('renders no tags when the list of tags is empty', () => {
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
expect(wrapper.find(Tag)).toHaveLength(0);
});
it('renders the proper amount of tags', () => {
const tags = [ 'foo', 'bar', 'baz' ];
wrapper = shallow(<SearchBar shortUrlsListParams={{ tags }} listShortUrls={listShortUrlsMock} />);
expect(wrapper.find(Tag)).toHaveLength(tags.length);
});
it('updates short URLs list when search field changes', () => {
2018-12-18 00:32:51 +03:00
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
const searchField = wrapper.find(SearchField);
2019-04-19 13:41:59 +03:00
expect(listShortUrlsMock).not.toHaveBeenCalled();
searchField.simulate('change');
2019-04-19 13:41:59 +03:00
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
it('updates short URLs list when a tag is removed', () => {
wrapper = shallow(
2020-08-22 09:10:31 +03:00
<SearchBar shortUrlsListParams={{ tags: [ 'foo' ] }} listShortUrls={listShortUrlsMock} />,
);
const tag = wrapper.find(Tag).first();
2019-04-19 13:41:59 +03:00
expect(listShortUrlsMock).not.toHaveBeenCalled();
tag.simulate('close');
2019-04-19 13:41:59 +03:00
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
2020-01-14 22:12:30 +03:00
it('updates short URLs list when date range changes', () => {
wrapper = shallow(
2020-08-22 09:10:31 +03:00
<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />,
);
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);
});
});