|
1 | 1 | /* eslint-disable no-eval */ |
| 2 | +import { fireEvent, render } from '@testing-library/react'; |
2 | 3 | import React from 'react'; |
3 | | -import { render } from '@testing-library/react'; |
| 4 | +import useEvent from '../src/hooks/useEvent'; |
4 | 5 | import { composeRef, supportRef, useComposeRef } from '../src/ref'; |
5 | 6 |
|
6 | 7 | describe('ref', () => { |
@@ -35,6 +36,37 @@ describe('ref', () => { |
35 | 36 | expect(ref1.current).toBeTruthy(); |
36 | 37 | expect(ref1.current).toBe(ref2.current); |
37 | 38 | }); |
| 39 | + |
| 40 | + it('useComposeRef not changed', () => { |
| 41 | + let count = 0; |
| 42 | + |
| 43 | + const Demo = () => { |
| 44 | + const [, forceUpdate] = React.useState({}); |
| 45 | + |
| 46 | + const ref1 = React.useRef(); |
| 47 | + const ref2 = React.useRef(); |
| 48 | + const refFn = useEvent(() => { |
| 49 | + count += 1; |
| 50 | + }); |
| 51 | + const mergedRef = useComposeRef(ref1, ref2, refFn); |
| 52 | + return ( |
| 53 | + <button ref={mergedRef} onClick={() => forceUpdate({})}> |
| 54 | + Update |
| 55 | + </button> |
| 56 | + ); |
| 57 | + }; |
| 58 | + |
| 59 | + const { container, unmount } = render(<Demo />); |
| 60 | + expect(count).toEqual(1); |
| 61 | + |
| 62 | + for (let i = 0; i < 10; i += 1) { |
| 63 | + fireEvent.click(container.querySelector('button')); |
| 64 | + expect(count).toEqual(1); |
| 65 | + } |
| 66 | + |
| 67 | + unmount(); |
| 68 | + expect(count).toEqual(2); |
| 69 | + }); |
38 | 70 | }); |
39 | 71 |
|
40 | 72 | describe('supportRef', () => { |
|
0 commit comments