-
Notifications
You must be signed in to change notification settings - Fork 932
/
downshift.misc.js
219 lines (193 loc) · 6.27 KB
/
downshift.misc.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// this is stuff that I couldn't think fit anywhere else
// but we still want to have tested.
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import {act, render} from '@testing-library/react'
import Downshift from '../'
beforeEach(() => jest.spyOn(console, 'error').mockImplementation(() => {}))
afterEach(() => console.error.mockRestore())
test('closeMenu closes the menu', () => {
const {openMenu, closeMenu, childrenSpy} = setup()
openMenu()
closeMenu()
expect(childrenSpy).toHaveBeenLastCalledWith(
expect.objectContaining({isOpen: false}),
)
})
test('clearSelection clears an existing selection', () => {
const {openMenu, selectItem, childrenSpy, clearSelection} = setup()
openMenu()
selectItem('foo')
clearSelection()
expect(childrenSpy).toHaveBeenLastCalledWith(
expect.objectContaining({selectedItem: null}),
)
})
test('selectItemAtIndex does nothing if there is no item at that index', () => {
const {openMenu, selectItemAtIndex, childrenSpy} = setup()
openMenu()
childrenSpy.mockClear()
selectItemAtIndex(100)
expect(childrenSpy).not.toHaveBeenCalled()
})
test('selectItemAtIndex can select item that is an empty string', () => {
const items = ['Chess', '']
const children = ({getItemProps}) => (
<div>
{items.map((item, index) => (
<div key={index} {...getItemProps({item})}>
{item}
</div>
))}
</div>
)
const {selectItemAtIndex, childrenSpy} = setup({children})
act(() => {
selectItemAtIndex(1)
})
expect(childrenSpy).toHaveBeenLastCalledWith(
expect.objectContaining({selectedItem: ''}),
)
})
test('toggleMenu can take no arguments at all', () => {
const {toggleMenu, childrenSpy} = setup()
act(() => {
toggleMenu()
})
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: true,
}),
)
})
test('clearItems clears the all items', () => {
const item = 'Chess'
const children = ({getItemProps}) => (
<div>
<div key={item} {...getItemProps({item})}>
{item}
</div>
</div>
)
// IMPLEMENTATION DETAIL TEST :-(
// eslint-disable-next-line react/no-render-return-value
const downshiftInstance = ReactDOM.render(
<Downshift>{children}</Downshift>,
document.createElement('div'),
)
expect(downshiftInstance.items).toEqual([item])
downshiftInstance.clearItems()
expect(downshiftInstance.items).toEqual([])
})
test('reset can take no arguments at all', () => {
const {reset, childrenSpy} = setup()
reset()
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: false,
}),
)
})
test('setHighlightedIndex can take no arguments at all', () => {
const defaultHighlightedIndex = 2
const {setHighlightedIndex, childrenSpy} = setup({
defaultHighlightedIndex,
})
setHighlightedIndex()
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
highlightedIndex: defaultHighlightedIndex,
}),
)
})
test('can specify a custom ID which is used in item IDs (good for SSR)', () => {
const id = 'my-custom-id'
const {getItemProps} = setup({id})
expect(getItemProps({item: 'blah'}).id).toContain(id)
})
test('can use children instead of render prop', () => {
const childrenSpy = jest.fn()
render(<Downshift>{childrenSpy}</Downshift>)
expect(childrenSpy).toHaveBeenCalledTimes(1)
})
test('should not log error during strict mode during reset', () => {
const children = () => <div />
render(
<React.StrictMode>
<Downshift>{children}</Downshift>
</React.StrictMode>,
)
expect(console.error.mock.calls).toHaveLength(0)
})
test('can use setState for ultimate power', () => {
const {childrenSpy, setState} = setup()
childrenSpy.mockClear()
act(() => {
setState({isOpen: true, selectedItem: 'hi'})
})
expect(childrenSpy).toHaveBeenCalledTimes(1)
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({isOpen: true, selectedItem: 'hi'}),
)
})
test('warns when controlled component becomes uncontrolled', () => {
const children = () => <div />
const {rerender} = render(<Downshift selectedItem="hi">{children}</Downshift>)
rerender(<Downshift selectedItem={undefined}>{children}</Downshift>)
expect(console.error.mock.calls).toHaveLength(1)
expect(console.error.mock.calls).toMatchSnapshot()
})
test('warns when uncontrolled component becomes controlled', () => {
const children = () => <div />
const {rerender} = render(<Downshift>{children}</Downshift>)
rerender(<Downshift selectedItem="hi">{children}</Downshift>)
expect(console.error.mock.calls).toHaveLength(1)
expect(console.error.mock.calls).toMatchSnapshot()
})
describe('expect console.warn to fire—depending on process.env.NODE_ENV value', () => {
const originalEnv = process.env.NODE_ENV
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
process.env.NODE_ENV = originalEnv
console.warn.mockRestore()
})
test(`it shouldn't log anything when value === 'production'`, () => {
process.env.NODE_ENV = 'production'
setup({selectedItem: {label: 'test', value: 'any'}})
expect(console.warn).toHaveBeenCalledTimes(0)
})
test('it should warn exactly one time when value !== production', () => {
process.env.NODE_ENV = 'development'
setup({selectedItem: {label: 'test', value: 'any'}})
expect(console.warn).toHaveBeenCalledTimes(1)
expect(console.warn.mock.calls[0]).toMatchSnapshot()
})
})
test('initializes with the initial* props', () => {
const initialState = {
initialIsOpen: true,
initialHighlightedIndex: 2,
initialInputValue: 'hey',
initialSelectedItem: 'sup',
}
const {childrenSpy} = setup(initialState)
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: initialState.initialIsOpen,
highlightedIndex: initialState.initialHighlightedIndex,
inputValue: initialState.initialInputValue,
selectedItem: initialState.initialSelectedItem,
}),
)
})
function setup({children = () => <div />, ...props} = {}) {
let renderArg
const childrenSpy = jest.fn(controllerArg => {
renderArg = controllerArg
return children(controllerArg)
})
const renderUtils = render(<Downshift {...props}>{childrenSpy}</Downshift>)
return {childrenSpy, ...renderUtils, ...renderArg}
}