|
1
|
|
|
import React from 'react'; |
|
2
|
|
|
import { shallow } from 'enzyme'; |
|
3
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
|
4
|
|
|
import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawesome/free-solid-svg-icons'; |
|
5
|
|
|
import shortUrlsListCreator, { SORTABLE_FIELDS } from '../../src/short-urls/ShortUrlsList'; |
|
6
|
|
|
|
|
7
|
|
|
describe('<ShortUrlsList />', () => { |
|
8
|
|
|
let wrapper; |
|
9
|
|
|
const ShortUrlsRow = () => ''; |
|
10
|
|
|
const listShortUrlsMock = jest.fn(); |
|
11
|
|
|
const resetShortUrlParamsMock = jest.fn(); |
|
12
|
|
|
|
|
13
|
|
|
const ShortUrlsList = shortUrlsListCreator(ShortUrlsRow); |
|
14
|
|
|
|
|
15
|
|
|
beforeEach(() => { |
|
16
|
|
|
wrapper = shallow( |
|
17
|
|
|
<ShortUrlsList |
|
18
|
|
|
listShortUrls={listShortUrlsMock} |
|
19
|
|
|
resetShortUrlParams={resetShortUrlParamsMock} |
|
20
|
|
|
shortUrlsListParams={{ |
|
21
|
|
|
page: '1', |
|
22
|
|
|
tags: [ 'test tag' ], |
|
23
|
|
|
searchTerm: 'example.com', |
|
24
|
|
|
}} |
|
25
|
|
|
match={{ params: {} }} |
|
26
|
|
|
location={{}} |
|
27
|
|
|
loading={false} |
|
28
|
|
|
error={false} |
|
29
|
|
|
shortUrlsList={ |
|
30
|
|
|
[ |
|
31
|
|
|
{ |
|
32
|
|
|
shortCode: 'testShortCode', |
|
33
|
|
|
shortUrl: 'https://www.example.com/testShortUrl', |
|
34
|
|
|
longUrl: 'https://www.example.com/testLongUrl', |
|
35
|
|
|
tags: [ 'test tag' ], |
|
36
|
|
|
}, |
|
37
|
|
|
] |
|
38
|
|
|
} |
|
39
|
|
|
/> |
|
40
|
|
|
); |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
afterEach(() => { |
|
44
|
|
|
listShortUrlsMock.mockReset(); |
|
45
|
|
|
resetShortUrlParamsMock.mockReset(); |
|
46
|
|
|
wrapper && wrapper.unmount(); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
it('wraps a SearchBar, ShortUrlsList as Paginator', () => { |
|
50
|
|
|
expect(wrapper.find(ShortUrlsRow)).toHaveLength(1); |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
it('should render inner table by default', () => { |
|
54
|
|
|
expect(wrapper.find('table')).toHaveLength(1); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('should render table header by default', () => { |
|
58
|
|
|
expect(wrapper.find('table').shallow().find('thead')).toHaveLength(1); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
it('should render 6 table header cells by default', () => { |
|
62
|
|
|
expect(wrapper.find('table').shallow() |
|
63
|
|
|
.find('thead').shallow() |
|
64
|
|
|
.find('tr').shallow() |
|
65
|
|
|
.find('th')).toHaveLength(6); |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
it('should render 6 table header cells without order by icon by default', () => { |
|
69
|
|
|
const thElements = wrapper.find('table').shallow() |
|
70
|
|
|
.find('thead').shallow() |
|
71
|
|
|
.find('tr').shallow() |
|
72
|
|
|
.find('th').map((e) => e.shallow()); |
|
73
|
|
|
|
|
74
|
|
|
for (const thElement of thElements) { |
|
75
|
|
|
expect(thElement.find(FontAwesomeIcon)).toHaveLength(0); |
|
76
|
|
|
} |
|
77
|
|
|
}); |
|
78
|
|
|
|
|
79
|
|
|
it('should render 6 table header cells with conditional order by icon by default', () => { |
|
80
|
|
|
const orderDirOptionToIconMap = { |
|
81
|
|
|
ASC: caretUpIcon, |
|
82
|
|
|
DESC: caretDownIcon, |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
for (const sortableField of Object.getOwnPropertyNames(SORTABLE_FIELDS)) { |
|
86
|
|
|
wrapper.setState({ orderField: sortableField, orderDir: undefined }); |
|
87
|
|
|
const [ dateCreatedThElement ] = wrapper.find('table').shallow() |
|
88
|
|
|
.find('thead').shallow() |
|
89
|
|
|
.find('tr').shallow() |
|
90
|
|
|
.find('th') |
|
91
|
|
|
.filterWhere( |
|
92
|
|
|
(e) => |
|
93
|
|
|
e.text().includes(SORTABLE_FIELDS[sortableField]) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
const dateCreatedThElementWrapper = shallow(dateCreatedThElement); |
|
97
|
|
|
|
|
98
|
|
|
expect(dateCreatedThElementWrapper.find(FontAwesomeIcon)).toHaveLength(0); |
|
99
|
|
|
|
|
100
|
|
|
for (const orderDir of Object.getOwnPropertyNames(orderDirOptionToIconMap)) { |
|
101
|
|
|
wrapper.setState({ orderField: sortableField, orderDir }); |
|
102
|
|
|
const [ dateCreatedThElement ] = wrapper.find('table').shallow() |
|
103
|
|
|
.find('thead').shallow() |
|
104
|
|
|
.find('tr').shallow() |
|
105
|
|
|
.find('th') |
|
106
|
|
|
.filterWhere( |
|
107
|
|
|
(e) => |
|
108
|
|
|
e.text().includes(SORTABLE_FIELDS[sortableField]) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
const dateCreatedThElementWrapper = shallow(dateCreatedThElement); |
|
112
|
|
|
|
|
113
|
|
|
expect(dateCreatedThElementWrapper.find(FontAwesomeIcon)).toHaveLength(1); |
|
114
|
|
|
expect( |
|
115
|
|
|
dateCreatedThElementWrapper.find(FontAwesomeIcon).prop('icon') |
|
116
|
|
|
).toEqual(orderDirOptionToIconMap[orderDir]); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
}); |
|
120
|
|
|
}); |
|
121
|
|
|
|