Skip to content

Commit 59d3498

Browse files
authored
fix(hooks): suppressRefError improvement (#1618)
1 parent 59246b6 commit 59d3498

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/hooks/useCombobox/__tests__/getInputProps.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,7 @@ describe('getInputProps', () => {
20852085

20862086
describe('non production errors', () => {
20872087
beforeEach(() => {
2088+
// usually it is disabled by test utils.
20882089
const {useGetterPropsCalledChecker} = jest.requireActual('../../utils')
20892090
jest
20902091
.spyOn(utils, 'useGetterPropsCalledChecker')
@@ -2138,6 +2139,19 @@ describe('getInputProps', () => {
21382139
)
21392140
})
21402141

2142+
test('will not be displayed if element ref is not set and suppressRefError is true', () => {
2143+
renderHook(() => {
2144+
const {getInputProps, getMenuProps} = useCombobox({
2145+
items,
2146+
})
2147+
2148+
getMenuProps({}, {suppressRefError: true})
2149+
getInputProps({}, {suppressRefError: true})
2150+
})
2151+
2152+
expect(console.error).not.toHaveBeenCalled()
2153+
})
2154+
21412155
test('will not be displayed if called with a correct ref', () => {
21422156
const refFn = jest.fn()
21432157
const inputNode = {}

src/hooks/useCombobox/__tests__/getMenuProps.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ describe('getMenuProps', () => {
135135

136136
describe('non production errors', () => {
137137
beforeEach(() => {
138+
// usally disabled by test utils.
138139
const {useGetterPropsCalledChecker} = jest.requireActual('../../utils')
139140
jest
140141
.spyOn(utils, 'useGetterPropsCalledChecker')
@@ -191,6 +192,19 @@ describe('getMenuProps', () => {
191192
)
192193
})
193194

195+
test('will not be displayed if element ref is not set and suppressRefError is true', () => {
196+
renderHook(() => {
197+
const {getInputProps, getMenuProps} = useCombobox({
198+
items,
199+
})
200+
201+
getMenuProps({}, {suppressRefError: true})
202+
getInputProps({}, {suppressRefError: true})
203+
})
204+
205+
expect(console.error).not.toHaveBeenCalled()
206+
})
207+
194208
test('will not be displayed if called with a correct ref', () => {
195209
const refFn = jest.fn()
196210
const menuNode = {}

src/hooks/useMultipleSelection/__tests__/getDropdownProps.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ describe('getDropdownProps', () => {
244244

245245
describe('non production errors', () => {
246246
beforeEach(() => {
247+
// usually disabled by test utils.
247248
const {useGetterPropsCalledChecker} = jest.requireActual('../../utils')
248249
jest
249250
.spyOn(utils, 'useGetterPropsCalledChecker')
@@ -262,6 +263,23 @@ describe('getDropdownProps', () => {
262263
)
263264
})
264265

266+
test('will not be displayed if getDropdownProps is not called on subsequent renders', () => {
267+
let firstRender = true
268+
const {rerender} = renderHook(() => {
269+
const {getDropdownProps} = useMultipleSelection()
270+
271+
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test
272+
if (firstRender) {
273+
firstRender = false
274+
getDropdownProps({}, {suppressRefError: true})
275+
}
276+
})
277+
278+
rerender()
279+
280+
expect(console.error).not.toHaveBeenCalled()
281+
})
282+
265283
test('will be displayed if element ref is not set and suppressRefError is false', () => {
266284
renderHook(() => {
267285
const {getDropdownProps} = useMultipleSelection()

src/hooks/utils.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -448,39 +448,40 @@ let useGetterPropsCalledChecker = () => noop
448448
/* istanbul ignore next */
449449
if (process.env.NODE_ENV !== 'production') {
450450
useGetterPropsCalledChecker = (...propKeys) => {
451-
const isInitialMountRef = useRef(true)
452451
const getterPropsCalledRef = useRef(
453452
propKeys.reduce((acc, propKey) => {
454453
acc[propKey] = {}
454+
455455
return acc
456456
}, {}),
457457
)
458458

459459
useEffect(() => {
460460
Object.keys(getterPropsCalledRef.current).forEach(propKey => {
461461
const propCallInfo = getterPropsCalledRef.current[propKey]
462-
if (isInitialMountRef.current) {
463-
if (!Object.keys(propCallInfo).length) {
464-
// eslint-disable-next-line no-console
465-
console.error(
466-
`downshift: You forgot to call the ${propKey} getter function on your component / element.`,
467-
)
468-
return
469-
}
462+
463+
if (!Object.keys(propCallInfo).length) {
464+
// eslint-disable-next-line no-console
465+
console.error(
466+
`downshift: You forgot to call the ${propKey} getter function on your component / element.`,
467+
)
468+
return
470469
}
471470

472471
const {suppressRefError, refKey, elementRef} = propCallInfo
473472

474-
if ((!elementRef || !elementRef.current) && !suppressRefError) {
473+
if (suppressRefError) {
474+
return
475+
}
476+
477+
if (!elementRef?.current) {
475478
// eslint-disable-next-line no-console
476479
console.error(
477480
`downshift: The ref prop "${refKey}" from ${propKey} was not applied correctly on your element.`,
478481
)
479482
}
480483
})
481-
482-
isInitialMountRef.current = false
483-
})
484+
}, [])
484485

485486
const setGetterPropCallInfo = useCallback(
486487
(propKey, suppressRefError, refKey, elementRef) => {

0 commit comments

Comments
 (0)