1
|
|
|
import { shallow, ShallowWrapper } from 'enzyme'; |
2
|
|
|
import { Mock } from 'ts-mockery'; |
3
|
|
|
import searchBarCreator from '../../src/short-urls/SearchBar'; |
4
|
|
|
import SearchField from '../../src/utils/SearchField'; |
5
|
|
|
import Tag from '../../src/tags/helpers/Tag'; |
6
|
|
|
import DateRangeRow from '../../src/utils/DateRangeRow'; |
7
|
|
|
import ColorGenerator from '../../src/utils/services/ColorGenerator'; |
8
|
|
|
|
9
|
|
|
describe('<SearchBar />', () => { |
10
|
|
|
let wrapper: ShallowWrapper; |
11
|
|
|
const listShortUrlsMock = jest.fn(); |
12
|
|
|
const SearchBar = searchBarCreator(Mock.all<ColorGenerator>(), () => null); |
13
|
|
|
|
14
|
|
|
afterEach(jest.clearAllMocks); |
15
|
|
|
afterEach(() => wrapper?.unmount()); |
16
|
|
|
|
17
|
|
|
it('renders a SearchField', () => { |
18
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />); |
19
|
|
|
|
20
|
|
|
expect(wrapper.find(SearchField)).toHaveLength(1); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
it('renders a DateRangeRow', () => { |
24
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />); |
25
|
|
|
|
26
|
|
|
expect(wrapper.find(DateRangeRow)).toHaveLength(1); |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
it('renders no tags when the list of tags is empty', () => { |
30
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />); |
31
|
|
|
|
32
|
|
|
expect(wrapper.find(Tag)).toHaveLength(0); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
it('renders the proper amount of tags', () => { |
36
|
|
|
const tags = [ 'foo', 'bar', 'baz' ]; |
37
|
|
|
|
38
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{ tags }} listShortUrls={listShortUrlsMock} />); |
39
|
|
|
|
40
|
|
|
expect(wrapper.find(Tag)).toHaveLength(tags.length); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
it('updates short URLs list when search field changes', () => { |
44
|
|
|
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />); |
45
|
|
|
const searchField = wrapper.find(SearchField); |
46
|
|
|
|
47
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled(); |
48
|
|
|
searchField.simulate('change'); |
49
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
it('updates short URLs list when a tag is removed', () => { |
53
|
|
|
wrapper = shallow( |
54
|
|
|
<SearchBar shortUrlsListParams={{ tags: [ 'foo' ] }} listShortUrls={listShortUrlsMock} />, |
55
|
|
|
); |
56
|
|
|
const tag = wrapper.find(Tag).first(); |
57
|
|
|
|
58
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled(); |
59
|
|
|
tag.simulate('close'); |
60
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
it.each([ 'startDateChange', 'endDateChange' ])('updates short URLs list when date range changes', (event) => { |
64
|
|
|
wrapper = shallow( |
65
|
|
|
<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />, |
66
|
|
|
); |
67
|
|
|
const dateRange = wrapper.find(DateRangeRow); |
68
|
|
|
|
69
|
|
|
expect(listShortUrlsMock).not.toHaveBeenCalled(); |
70
|
|
|
dateRange.simulate(event); |
71
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1); |
72
|
|
|
}); |
73
|
|
|
}); |
74
|
|
|
|