|
1 |
| -/* eslint react/prop-types: 0 */ |
2 |
| - |
3 |
| -import React, { createRef } from 'react' |
4 |
| -import { renderIntoDocument } from 'react-dom/test-utils' |
5 |
| -import { shallow } from 'enzyme' |
6 |
| -import toJson from 'enzyme-to-json' |
7 |
| - |
| 1 | +import React, { FC, ReactNode, RefObject, createRef } from 'react' |
| 2 | +import { render } from '@testing-library/react' |
8 | 3 | import withForwardedRef from '../source'
|
9 | 4 |
|
| 5 | +interface Props<A = any> { |
| 6 | + children: ReactNode |
| 7 | + className: string |
| 8 | + forwardedRef?: RefObject<A> |
| 9 | +} |
| 10 | + |
10 | 11 | describe('withForwardedRef', () => {
|
11 | 12 | it('renders', () => {
|
12 |
| - const Comp = ({ children, className, forwardedRef }) => ( |
| 13 | + const Comp: FC<Props> = ({ children, className, forwardedRef }) => ( |
13 | 14 | <div className={className} ref={forwardedRef}>
|
14 | 15 | {children}
|
15 | 16 | </div>
|
16 | 17 | )
|
17 | 18 | const WrappedComp = withForwardedRef(Comp)
|
18 | 19 | const ref = createRef()
|
19 |
| - const wrapper = shallow( |
| 20 | + const { asFragment } = render( |
20 | 21 | <WrappedComp className="foo" ref={ref}>
|
21 | 22 | Testing 123
|
22 | 23 | </WrappedComp>
|
23 | 24 | )
|
24 |
| - const tree = toJson(wrapper) |
25 |
| - expect(tree).toMatchSnapshot() |
| 25 | + |
| 26 | + expect(asFragment()).toMatchSnapshot() |
26 | 27 | })
|
27 | 28 |
|
28 | 29 | it('renders, custom displayName', () => {
|
29 |
| - const Comp = ({ children, className, forwardedRef }) => ( |
| 30 | + const Comp: FC<Props> = ({ children, className, forwardedRef }) => ( |
30 | 31 | <div className={className} ref={forwardedRef}>
|
31 | 32 | {children}
|
32 | 33 | </div>
|
33 | 34 | )
|
34 | 35 | Comp.displayName = 'FooBar'
|
35 | 36 | const WrappedComp = withForwardedRef(Comp)
|
36 | 37 | const ref = createRef()
|
37 |
| - const wrapper = shallow( |
| 38 | + const { asFragment } = render( |
38 | 39 | <WrappedComp className="foo" ref={ref}>
|
39 | 40 | Testing 123
|
40 | 41 | </WrappedComp>
|
41 | 42 | )
|
42 |
| - const tree = toJson(wrapper) |
43 |
| - expect(tree).toMatchSnapshot() |
| 43 | + |
| 44 | + expect(asFragment()).toMatchSnapshot() |
44 | 45 | })
|
45 | 46 |
|
46 | 47 | it('ref points to HTMLDivElement', () => {
|
47 |
| - const Comp = ({ children, className, forwardedRef }) => ( |
| 48 | + const Comp: FC<Props> = ({ children, className, forwardedRef }) => ( |
48 | 49 | <div className={className} ref={forwardedRef}>
|
49 | 50 | {children}
|
50 | 51 | </div>
|
51 | 52 | )
|
52 | 53 | const WrappedComp = withForwardedRef(Comp)
|
53 | 54 | const ref = createRef()
|
54 |
| - renderIntoDocument( |
| 55 | + |
| 56 | + render( |
55 | 57 | <WrappedComp className="foo" ref={ref}>
|
56 | 58 | Testing 123
|
57 | 59 | </WrappedComp>
|
58 | 60 | )
|
| 61 | + |
59 | 62 | expect(ref.current).toBeInstanceOf(HTMLDivElement)
|
60 | 63 | })
|
61 | 64 | })
|
0 commit comments