|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +describe('forwardRef', () => { |
| 13 | + let PropTypes; |
| 14 | + let React; |
| 15 | + let ReactNoop; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + jest.resetModules(); |
| 19 | + PropTypes = require('prop-types'); |
| 20 | + React = require('react'); |
| 21 | + ReactNoop = require('react-noop-renderer'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should update refs when switching between children', () => { |
| 25 | + function FunctionalComponent({forwardedRef, setRefOnDiv}) { |
| 26 | + return ( |
| 27 | + <section> |
| 28 | + <div ref={setRefOnDiv ? forwardedRef : null}>First</div> |
| 29 | + <span ref={setRefOnDiv ? null : forwardedRef}>Second</span> |
| 30 | + </section> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + const RefForwardingComponent = React.forwardRef((props, ref) => ( |
| 35 | + <FunctionalComponent {...props} forwardedRef={ref} /> |
| 36 | + )); |
| 37 | + |
| 38 | + const ref = React.createRef(); |
| 39 | + |
| 40 | + ReactNoop.render(<RefForwardingComponent ref={ref} setRefOnDiv={true} />); |
| 41 | + ReactNoop.flush(); |
| 42 | + expect(ref.current.type).toBe('div'); |
| 43 | + |
| 44 | + ReactNoop.render(<RefForwardingComponent ref={ref} setRefOnDiv={false} />); |
| 45 | + ReactNoop.flush(); |
| 46 | + expect(ref.current.type).toBe('span'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should support rendering null', () => { |
| 50 | + const RefForwardingComponent = React.forwardRef((props, ref) => null); |
| 51 | + |
| 52 | + const ref = React.createRef(); |
| 53 | + |
| 54 | + ReactNoop.render(<RefForwardingComponent ref={ref} />); |
| 55 | + ReactNoop.flush(); |
| 56 | + expect(ref.current).toBe(null); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should support rendering null for multiple children', () => { |
| 60 | + const RefForwardingComponent = React.forwardRef((props, ref) => null); |
| 61 | + |
| 62 | + const ref = React.createRef(); |
| 63 | + |
| 64 | + ReactNoop.render( |
| 65 | + <div> |
| 66 | + <div /> |
| 67 | + <RefForwardingComponent ref={ref} /> |
| 68 | + <div /> |
| 69 | + </div>, |
| 70 | + ); |
| 71 | + ReactNoop.flush(); |
| 72 | + expect(ref.current).toBe(null); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should support propTypes and defaultProps', () => { |
| 76 | + function FunctionalComponent({forwardedRef, optional, required}) { |
| 77 | + return ( |
| 78 | + <div ref={forwardedRef}> |
| 79 | + {optional} |
| 80 | + {required} |
| 81 | + </div> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const RefForwardingComponent = React.forwardRef(function NamedFunction( |
| 86 | + props, |
| 87 | + ref, |
| 88 | + ) { |
| 89 | + return <FunctionalComponent {...props} forwardedRef={ref} />; |
| 90 | + }); |
| 91 | + RefForwardingComponent.propTypes = { |
| 92 | + optional: PropTypes.string, |
| 93 | + required: PropTypes.string.isRequired, |
| 94 | + }; |
| 95 | + RefForwardingComponent.defaultProps = { |
| 96 | + optional: 'default', |
| 97 | + }; |
| 98 | + |
| 99 | + const ref = React.createRef(); |
| 100 | + |
| 101 | + ReactNoop.render( |
| 102 | + <RefForwardingComponent ref={ref} optional="foo" required="bar" />, |
| 103 | + ); |
| 104 | + ReactNoop.flush(); |
| 105 | + expect(ref.current.children).toEqual([{text: 'foo'}, {text: 'bar'}]); |
| 106 | + |
| 107 | + ReactNoop.render(<RefForwardingComponent ref={ref} required="foo" />); |
| 108 | + ReactNoop.flush(); |
| 109 | + expect(ref.current.children).toEqual([{text: 'default'}, {text: 'foo'}]); |
| 110 | + |
| 111 | + expect(() => |
| 112 | + ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />), |
| 113 | + ).toWarnDev( |
| 114 | + 'Warning: Failed prop type: The prop `required` is marked as required in ' + |
| 115 | + '`ForwardRef(NamedFunction)`, but its value is `undefined`.\n' + |
| 116 | + ' in ForwardRef(NamedFunction) (at **)', |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should warn if not provided a callback during creation', () => { |
| 121 | + expect(() => React.forwardRef(undefined)).toWarnDev( |
| 122 | + 'forwardRef requires a render function but was given undefined.', |
| 123 | + ); |
| 124 | + expect(() => React.forwardRef(null)).toWarnDev( |
| 125 | + 'forwardRef requires a render function but was given null.', |
| 126 | + ); |
| 127 | + expect(() => React.forwardRef('foo')).toWarnDev( |
| 128 | + 'forwardRef requires a render function but was given string.', |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should warn if no render function is provided', () => { |
| 133 | + expect(React.forwardRef).toWarnDev( |
| 134 | + 'forwardRef requires a render function but was given undefined.', |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should warn if the render function provided has propTypes or defaultProps attributes', () => { |
| 139 | + function renderWithPropTypes() { |
| 140 | + return null; |
| 141 | + } |
| 142 | + renderWithPropTypes.propTypes = {}; |
| 143 | + |
| 144 | + function renderWithDefaultProps() { |
| 145 | + return null; |
| 146 | + } |
| 147 | + renderWithDefaultProps.defaultProps = {}; |
| 148 | + |
| 149 | + expect(() => React.forwardRef(renderWithPropTypes)).toWarnDev( |
| 150 | + 'forwardRef render functions do not support propTypes or defaultProps. ' + |
| 151 | + 'Did you accidentally pass a React component?', |
| 152 | + ); |
| 153 | + expect(() => React.forwardRef(renderWithDefaultProps)).toWarnDev( |
| 154 | + 'forwardRef render functions do not support propTypes or defaultProps. ' + |
| 155 | + 'Did you accidentally pass a React component?', |
| 156 | + ); |
| 157 | + }); |
| 158 | +}); |
0 commit comments