From 4a9530a01e76ec3b09758a9ac1927d5f0e120405 Mon Sep 17 00:00:00 2001 From: samat Date: Tue, 8 Jun 2021 02:20:19 +0600 Subject: [PATCH 1/2] avoiding call for deprecated findDOMNode as much as possible; removed some excessive refs --- src/CSSMotion.tsx | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/CSSMotion.tsx b/src/CSSMotion.tsx index 22e7999..99de2ae 100644 --- a/src/CSSMotion.tsx +++ b/src/CSSMotion.tsx @@ -9,12 +9,9 @@ import type { MotionStatus, MotionEventHandler, MotionEndEventHandler, - MotionPrepareEventHandler} from './interface'; -import { - STATUS_NONE, - STEP_PREPARE, - STEP_START, + MotionPrepareEventHandler, } from './interface'; +import { STATUS_NONE, STEP_PREPARE, STEP_START } from './interface'; import useStatus from './hooks/useStatus'; import DomWrapper from './DomWrapper'; import { isActive } from './hooks/useStepQueue'; @@ -135,15 +132,19 @@ export function genCSSMotion( const supportMotion = isSupportTransition(props); // Ref to the react node, it may be a HTMLElement - const nodeRef = useRef(); + const nodeRef = useRef(); // Ref to the dom wrapper in case ref can not pass to HTMLElement const wrapperNodeRef = useRef(); function getDomElement() { try { - return findDOMNode( - nodeRef.current || wrapperNodeRef.current, - ); + // Here we're avoiding call for findDOMNode since it's deprecated + // in strict mode. We're calling it only when node ref is not + // an instance of DOM HTMLElement. Otherwise use + // findDOMNode as a final resort + return nodeRef.current instanceof HTMLElement + ? nodeRef.current + : findDOMNode(wrapperNodeRef.current); } catch (e) { // Only happen when `motionDeadline` trigger but element removed. return null; @@ -157,7 +158,7 @@ export function genCSSMotion( props, ); - // Record whether content has rended + // Record whether content has rendered // Will return null for un-rendered even when `removeOnLeave={false}` const renderedRef = React.useRef(mergedVisible); if (mergedVisible) { @@ -165,13 +166,9 @@ export function genCSSMotion( } // ====================== Refs ====================== - const originRef = useRef(ref); - originRef.current = ref; - const setNodeRef = React.useCallback((node: any) => { nodeRef.current = node; - - fillRef(originRef.current, node); + fillRef(ref, node); }, []); // ===================== Render ===================== @@ -213,10 +210,8 @@ export function genCSSMotion( { ...mergedProps, className: classNames(getTransitionName(motionName, status), { - [getTransitionName( - motionName, - `${status}-${statusSuffix}`, - )]: statusSuffix, + [getTransitionName(motionName, `${status}-${statusSuffix}`)]: + statusSuffix, [motionName as string]: typeof motionName === 'string', }), style: statusStyle, From 1745908cb87051dd7f00f95c858fd2d1db1a8838 Mon Sep 17 00:00:00 2001 From: samat Date: Tue, 8 Jun 2021 16:56:47 +0600 Subject: [PATCH 2/2] added tests for strict mode deprecated findDOMNode call checks --- tests/CSSMotion.spec.tsx | 81 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/tests/CSSMotion.spec.tsx b/tests/CSSMotion.spec.tsx index 4508123..07ca240 100644 --- a/tests/CSSMotion.spec.tsx +++ b/tests/CSSMotion.spec.tsx @@ -8,6 +8,7 @@ import classNames from 'classnames'; import { mount } from './wrapper'; import type { CSSMotionProps } from '../src/CSSMotion'; import RefCSSMotion, { genCSSMotion } from '../src/CSSMotion'; +import ReactDOM from 'react-dom'; describe('CSSMotion', () => { const CSSMotion = genCSSMotion({ @@ -638,9 +639,9 @@ describe('CSSMotion', () => { jest.runAllTimers(); const transitionEndEvent = new Event('transitionend'); - ((wrapper - .find('.motion-box') - .instance() as any) as HTMLElement).dispatchEvent(transitionEndEvent); + ( + wrapper.find('.motion-box').instance() as any as HTMLElement + ).dispatchEvent(transitionEndEvent); jest.runAllTimers(); @@ -649,4 +650,78 @@ describe('CSSMotion', () => { expect(wrapper.find('.motion-box')).toHaveLength(1); expect(wrapper.find('.motion-box').hasClass('removed')).toBeTruthy(); }); + + describe('strict mode', () => { + beforeEach(() => { + jest.spyOn(ReactDOM, 'findDOMNode'); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('calls findDOMNode when no refs are passed', () => { + const wrapper = mount( + + {() =>
} + , + ); + + act(() => { + jest.runAllTimers(); + wrapper.update(); + }); + + expect(ReactDOM.findDOMNode).toHaveBeenCalled(); + }); + + it('does not call findDOMNode when ref is passed internally', () => { + const wrapper = mount( + + {(props, ref) =>
} + , + ); + + act(() => { + jest.runAllTimers(); + wrapper.update(); + }); + + expect(ReactDOM.findDOMNode).not.toHaveBeenCalled(); + }); + + it('calls findDOMNode when refs are forwarded but not assigned', () => { + const domRef = React.createRef(); + + const wrapper = mount( + + {() =>
} + , + ); + + act(() => { + jest.runAllTimers(); + wrapper.update(); + }); + + expect(ReactDOM.findDOMNode).toHaveBeenCalled(); + }); + + it('does not call findDOMNode when refs are forwarded and assigned', () => { + const domRef = React.createRef(); + + const wrapper = mount( + + {(props, ref) =>
} + , + ); + + act(() => { + jest.runAllTimers(); + wrapper.update(); + }); + + expect(ReactDOM.findDOMNode).not.toHaveBeenCalled(); + }); + }); });