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

57 lines
1.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { shallow } from 'enzyme';
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';
describe('<SearchBar />', () => {
let wrapper;
2019-04-19 13:41:59 +03:00
const listShortUrlsMock = jest.fn();
const SearchBar = searchBarCreator({});
afterEach(() => {
2019-04-19 13:41:59 +03:00
listShortUrlsMock.mockReset();
wrapper && wrapper.unmount();
});
it('renders a SearchField', () => {
2018-12-18 00:32:51 +03:00
wrapper = shallow(<SearchBar shortUrlsListParams={{}} />);
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={{}} />);
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 }} />);
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(
2018-12-18 00:32:51 +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);
});
});