Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,19 @@ Transition.propTypes = {
* [test/CSSTransition-test.js](https://github.com/reactjs/react-transition-group/blob/13435f897b3ab71f6e19d724f145596f5910581c/test/CSSTransition-test.js#L362-L437)).
*/
nodeRef: PropTypes.shape({
current: typeof Element === 'undefined'
? PropTypes.any
: PropTypes.instanceOf(Element)
current: (propValue, key, componentName) => {
const instance = propValue[key];
if (instance === undefined || instance === null) {
return null
}
// Element.prototype.nodeType === 1
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#Node_type_constants
if (instance.nodeType === 1) {
return null;
}
const actualClassName = !instance.constructor || !instance.constructor.name ? '<<anonymous>>' : instance.constructor.name;
return new Error('Invalid props `nodeRef.current` of type `' + actualClassName + '` supplied to `' + componentName + '`, expected instance of `Element`.')
}
}),

/**
Expand Down
32 changes: 32 additions & 0 deletions test/Transition-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import * as PropTypes from 'prop-types'

import { mount } from 'enzyme'

Expand Down Expand Up @@ -510,4 +511,35 @@ describe('Transition', () => {
instance.setState({ in: false })
})
})

describe('propTypes', () => {
beforeEach(() => {
PropTypes.resetWarningCache()
jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
console.error.mockRestore()
})

it('does not error if nodeRef.current is nullish | Element', () => {
PropTypes.checkPropTypes(Transition.propTypes, { nodeRef: { current: undefined }, children: <div />, timeout: 0 }, 'props', 'Transition')

expect(console.error).toHaveBeenCalledTimes(0);

PropTypes.checkPropTypes(Transition.propTypes, { nodeRef: { current: null }, children: <div />, timeout: 0 }, 'props', 'Transition')

expect(console.error).toHaveBeenCalledTimes(0);

PropTypes.checkPropTypes(Transition.propTypes, { nodeRef: { current: document.createElement('div') }, children: <div />, timeout: 0 }, 'props', 'Transition')

expect(console.error).toHaveBeenCalledTimes(0);
})

it('errors if nodeRef.current is not an Element', () => {
PropTypes.checkPropTypes(Transition.propTypes, { nodeRef: { current: document }, children: <div />, timeout: 0 }, 'props', 'Transition')

expect(console.error).toHaveBeenCalledWith('Warning: Failed props type: Invalid props `nodeRef.current` of type `Document` supplied to `Transition`, expected instance of `Element`.')
})
})
})