2018-09-16 13:18:02 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import sinon from 'sinon';
|
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';
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
describe('<SearchBar />', () => {
|
|
|
|
let wrapper;
|
|
|
|
const listShortUrlsMock = sinon.spy();
|
2018-12-18 13:28:15 +03:00
|
|
|
const SearchBar = searchBarCreator({});
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
listShortUrlsMock.resetHistory();
|
|
|
|
|
|
|
|
if (wrapper) {
|
|
|
|
wrapper.unmount();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a SearchField', () => {
|
2018-12-18 00:32:51 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
expect(wrapper.find(SearchField)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders no tags when the list of tags is empty', () => {
|
2018-12-18 00:32:51 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} />);
|
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' ];
|
|
|
|
|
2018-12-18 00:32:51 +03:00
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{ tags }} />);
|
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);
|
|
|
|
|
|
|
|
expect(listShortUrlsMock.callCount).toEqual(0);
|
|
|
|
searchField.simulate('change');
|
|
|
|
expect(listShortUrlsMock.callCount).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates short URLs list when a tag is removed', () => {
|
|
|
|
wrapper = shallow(
|
2018-12-18 00:32:51 +03:00
|
|
|
<SearchBar shortUrlsListParams={{ tags: [ 'foo' ] }} listShortUrls={listShortUrlsMock} />
|
2018-09-16 13:18:02 +03:00
|
|
|
);
|
|
|
|
const tag = wrapper.find(Tag).first();
|
|
|
|
|
|
|
|
expect(listShortUrlsMock.callCount).toEqual(0);
|
|
|
|
tag.simulate('close');
|
|
|
|
expect(listShortUrlsMock.callCount).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|