2020-08-30 20:45:17 +03:00
|
|
|
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';
|
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-01-14 22:12:30 +03:00
|
|
|
import DateRangeRow from '../../src/utils/DateRangeRow';
|
2020-08-30 20:45:17 +03:00
|
|
|
import ColorGenerator from '../../src/utils/services/ColorGenerator';
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
describe('<SearchBar />', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2019-04-19 13:41:59 +03:00
|
|
|
const listShortUrlsMock = jest.fn();
|
2020-08-30 20:45:17 +03:00
|
|
|
const SearchBar = searchBarCreator(Mock.all<ColorGenerator>(), () => null);
|
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
|
|
|
|
|
|
|
it('renders a SearchField', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
expect(wrapper.find(SearchField)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
2020-01-28 21:46:36 +03:00
|
|
|
it('renders a DateRangeRow', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
|
2020-01-28 21:46:36 +03:00
|
|
|
|
|
|
|
expect(wrapper.find(DateRangeRow)).toHaveLength(1);
|
2020-01-14 22:12:30 +03:00
|
|
|
});
|
|
|
|
|
2018-09-16 13:18:02 +03:00
|
|
|
it('renders no tags when the list of tags is empty', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
expect(wrapper.find(Tag)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the proper amount of tags', () => {
|
|
|
|
const tags = [ 'foo', 'bar', 'baz' ];
|
|
|
|
|
2020-08-30 20:45:17 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{ tags }} listShortUrls={listShortUrlsMock} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
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} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
const searchField = wrapper.find(SearchField);
|
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled();
|
2018-09-16 13:18:02 +03:00
|
|
|
searchField.simulate('change');
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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} />,
|
2018-09-16 13:18:02 +03:00
|
|
|
);
|
|
|
|
const tag = wrapper.find(Tag).first();
|
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled();
|
2018-09-16 13:18:02 +03:00
|
|
|
tag.simulate('close');
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
2020-01-14 22:12:30 +03:00
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each([ 'startDateChange', 'endDateChange' ])('updates short URLs list when date range changes', (event) => {
|
2020-01-19 23:25:45 +03:00
|
|
|
wrapper = shallow(
|
2020-08-22 09:10:31 +03:00
|
|
|
<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />,
|
2020-01-19 23:25:45 +03:00
|
|
|
);
|
2020-01-14 22:12:30 +03:00
|
|
|
const dateRange = wrapper.find(DateRangeRow);
|
|
|
|
|
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled();
|
|
|
|
dateRange.simulate(event);
|
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|