|
2 | 2 | // but we still want to have tested. |
3 | 3 |
|
4 | 4 | import * as React from 'react' |
5 | | -import * as ReactDOM from 'react-dom' |
| 5 | +import * as ReactDOM from 'react-dom/client' |
6 | 6 | import {act, render} from '@testing-library/react' |
7 | 7 | import Downshift from '../' |
8 | 8 |
|
@@ -68,21 +68,38 @@ test('toggleMenu can take no arguments at all', () => { |
68 | 68 | ) |
69 | 69 | }) |
70 | 70 |
|
71 | | -test('clearItems clears the all items', () => { |
| 71 | +test('clearItems clears all items', () => { |
72 | 72 | const item = 'Chess' |
| 73 | + |
73 | 74 | const children = ({getItemProps}) => ( |
74 | 75 | <div> |
75 | 76 | <div key={item} {...getItemProps({item})}> |
76 | 77 | {item} |
77 | 78 | </div> |
78 | 79 | </div> |
79 | 80 | ) |
80 | | - // IMPLEMENTATION DETAIL TEST :-( |
81 | | - // eslint-disable-next-line react/no-render-return-value |
82 | | - const downshiftInstance = ReactDOM.render( |
83 | | - <Downshift>{children}</Downshift>, |
84 | | - document.createElement('div'), |
85 | | - ) |
| 81 | + |
| 82 | + // Wrap Downshift to expose its instance methods through a ref |
| 83 | + const DownshiftWrapper = React.forwardRef((_props, ref) => { |
| 84 | + const innerRef = React.useRef(null) |
| 85 | + |
| 86 | + React.useImperativeHandle(ref, () => innerRef.current) |
| 87 | + |
| 88 | + return <Downshift ref={innerRef}>{children}</Downshift> |
| 89 | + }) |
| 90 | + |
| 91 | + const container = document.createElement('div') |
| 92 | + const root = ReactDOM.createRoot(container) |
| 93 | + |
| 94 | + const dsRef = React.createRef() |
| 95 | + |
| 96 | + // eslint-disable-next-line testing-library/no-unnecessary-act |
| 97 | + act(() => { |
| 98 | + root.render(<DownshiftWrapper ref={dsRef} />) |
| 99 | + }) |
| 100 | + |
| 101 | + const downshiftInstance = dsRef.current |
| 102 | + |
86 | 103 | expect(downshiftInstance.items).toEqual([item]) |
87 | 104 | downshiftInstance.clearItems() |
88 | 105 | expect(downshiftInstance.items).toEqual([]) |
|
0 commit comments