/
NestedSelect.spec.jsx
162 lines (143 loc) · 4.44 KB
/
NestedSelect.spec.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import NestedSelect, { ItemRow } from './NestedSelect'
import ListItem from '../ListItem'
import { BreakpointsProvider } from '../providers/Breakpoints'
describe('NestedSelect', () => {
const options = {
children: [
{ title: 'A' },
{ title: 'B', children: [{ title: 'B1' }, { title: 'B2' }] },
{ title: 'C' }
]
}
const findSelectedRow = root => {
return root.findWhere(n => {
if (n.type() != ItemRow) {
return false
}
return n.props().isSelected
})
}
const simulateClick = row =>
row
.find(ListItem)
.props()
.onClick()
const setup = ({ canSelectParent, itemSelected, searchOptions }) => {
// Very crude notion of parenting
const isParent = (item, childItem) => {
return childItem && childItem.title.includes(item.title)
}
const isSelected = (item, level) => {
if (level === 0 && isParent(item, itemSelected)) {
return true
} else if (itemSelected && itemSelected.title === item.title) {
return true
} else {
return false
}
}
const use = searchOptions ? render : mount
const root = use(
<BreakpointsProvider>
<NestedSelect
canSelectParent={canSelectParent}
options={options}
isSelected={isSelected}
onSelect={jest.fn()}
onCancel={jest.fn()}
searchOptions={searchOptions}
/>
</BreakpointsProvider>
)
return { root }
}
describe('when selecting a normal category', () => {
it('should show only one selected item', () => {
const { root } = setup({
itemSelected: { title: 'B1' }
})
const selectedRow = findSelectedRow(root)
expect(selectedRow.length).toBe(1)
expect(selectedRow.text()).toBe('B')
simulateClick(selectedRow)
root.update()
const selectedRow2 = findSelectedRow(root)
expect(selectedRow2.length).toBe(1)
expect(selectedRow2.text()).toBe('B1')
})
})
describe('when allowing parent to be selected', () => {
it('should show a line for the parent inside the category', () => {
const { root } = setup({
itemSelected: { title: 'B1' },
canSelectParent: true
})
const selectedRow = findSelectedRow(root)
expect(selectedRow.length).toBe(1)
simulateClick(selectedRow)
root.update()
expect(root.find(ItemRow).length).toBe(3)
})
})
describe('when there is a search options defined', () => {
it('should not display search input when we are in a subcategory', () => {
const searchOptions = {
placeholderSearch: 'Placeholder Search',
noDataLabel: 'No Data Found',
onSearch: () => {
return []
}
}
const { root } = setup({
searchOptions
})
fireEvent.click(root.getByText('B'))
expect(root.queryByPlaceholderText('Placeholder Search')).toBeFalsy()
})
it('should return no data (onSearch return [])', () => {
const searchOptions = {
placeholderSearch: 'Placeholder Search',
noDataLabel: 'No Data Found',
onSearch: () => {
return []
}
}
const { root } = setup({
searchOptions
})
const searchInput = root.getByPlaceholderText('Placeholder Search')
expect(searchInput).toBeTruthy()
fireEvent.change(searchInput, { target: { value: 'cozy' } })
const noData = root.getByText('No Data Found')
expect(noData).toBeTruthy()
})
it('should show search results', () => {
const data = [
{ title: 'cozy 1' },
{ title: 'cozy 2' },
{ title: 'anything' }
]
const searchOptions = {
placeholderSearch: 'Placeholder Search',
noDataLabel: 'No Data Found',
onSearch: value => {
// Your custom search
return data.filter(d => d.title.startsWith(value))
}
}
const { root } = setup({
itemSelected: { title: 'B1' },
canSelectParent: true,
searchOptions
})
const searchInput = root.getByPlaceholderText('Placeholder Search')
expect(searchInput).toBeTruthy()
fireEvent.change(searchInput, { target: { value: 'cozy' } })
expect(root.queryByText('cozy 1')).toBeTruthy()
expect(root.queryByText('cozy 1')).toBeTruthy()
expect(root.queryByText('anything')).toBeFalsy()
})
})
})