Skip to content

Commit 4566580

Browse files
authored
fix(useCombobox): use environment instead of document (#1478)
1 parent 3af2064 commit 4566580

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

src/hooks/__tests__/utils.test.js

+75
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import {renderHook} from '@testing-library/react-hooks'
12
import {
23
getItemIndex,
34
defaultProps,
45
getInitialValue,
56
getDefaultValue,
7+
useMouseAndTouchTracker,
68
} from '../utils'
79

810
describe('utils', () => {
@@ -63,4 +65,77 @@ describe('utils', () => {
6365

6466
expect(value).toEqual(defaults.bogusValue)
6567
})
68+
69+
describe('useMouseAndTouchTracker', () => {
70+
test('renders without error', () => {
71+
expect(() => {
72+
renderHook(() =>
73+
useMouseAndTouchTracker(false, [], undefined, jest.fn()),
74+
)
75+
}).not.toThrowError()
76+
})
77+
78+
test('adds and removes listeners to environment', () => {
79+
const environment = {
80+
addEventListener: jest.fn(),
81+
removeEventListener: jest.fn(),
82+
}
83+
84+
const {unmount, result} = renderHook(() =>
85+
useMouseAndTouchTracker(false, [], environment, jest.fn()),
86+
)
87+
88+
expect(environment.addEventListener).toHaveBeenCalledTimes(5)
89+
expect(environment.addEventListener).toHaveBeenCalledWith(
90+
'mousedown',
91+
expect.any(Function),
92+
)
93+
expect(environment.addEventListener).toHaveBeenCalledWith(
94+
'mouseup',
95+
expect.any(Function),
96+
)
97+
expect(environment.addEventListener).toHaveBeenCalledWith(
98+
'touchstart',
99+
expect.any(Function),
100+
)
101+
expect(environment.addEventListener).toHaveBeenCalledWith(
102+
'touchmove',
103+
expect.any(Function),
104+
)
105+
expect(environment.addEventListener).toHaveBeenCalledWith(
106+
'touchend',
107+
expect.any(Function),
108+
)
109+
expect(environment.removeEventListener).not.toHaveBeenCalled()
110+
111+
unmount()
112+
113+
expect(environment.addEventListener).toHaveBeenCalledTimes(5)
114+
expect(environment.removeEventListener).toHaveBeenCalledTimes(5)
115+
expect(environment.removeEventListener).toHaveBeenCalledWith(
116+
'mousedown',
117+
expect.any(Function),
118+
)
119+
expect(environment.removeEventListener).toHaveBeenCalledWith(
120+
'mouseup',
121+
expect.any(Function),
122+
)
123+
expect(environment.removeEventListener).toHaveBeenCalledWith(
124+
'touchstart',
125+
expect.any(Function),
126+
)
127+
expect(environment.removeEventListener).toHaveBeenCalledWith(
128+
'touchmove',
129+
expect.any(Function),
130+
)
131+
expect(environment.removeEventListener).toHaveBeenCalledWith(
132+
'touchend',
133+
expect.any(Function),
134+
)
135+
136+
expect(result.current).toEqual({
137+
current: {isMouseDown: false, isTouchMove: false},
138+
})
139+
})
140+
})
66141
})

src/hooks/useCombobox/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ function useCombobox(userProps = {}) {
148148
useEffect(() => {
149149
if (!isOpen) {
150150
itemRefs.current = {}
151-
} else if (document.activeElement !== inputRef.current) {
151+
} else if (environment.document?.activeElement !== inputRef.current) {
152152
inputRef?.current?.focus()
153153
}
154-
}, [isOpen])
154+
}, [isOpen, environment])
155155

156156
/* Event handler functions */
157157
const inputKeyDownHandlers = useMemo(

src/hooks/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ function useMouseAndTouchTracker(
317317
})
318318

319319
useEffect(() => {
320-
/* istanbul ignore if (react-native) */
321-
if (isReactNative) {
320+
if (environment?.addEventListener == null) {
322321
return
323322
}
324323

325-
// The same strategy for checking if a click occurred inside or outside downsift
324+
// The same strategy for checking if a click occurred inside or outside downshift
326325
// as in downshift.js.
327326
const onMouseDown = () => {
328327
mouseAndTouchTrackersRef.current.isMouseDown = true

0 commit comments

Comments
 (0)