Switching to use forwardRef

This commit is contained in:
Jeff Avallone 2018-05-28 12:28:08 -04:00
parent b9f6766a66
commit 3916d63f2d
4 changed files with 4 additions and 20 deletions

View File

@ -146,7 +146,7 @@ class App extends React.PureComponent {
<p>Sample warning message</p>
</Message>
{ image && <div className={ style.render }>
<SVG data={ image } imageRef={ this.image }/>
<SVG data={ image } ref={ this.image }/>
</div> }
</React.Fragment>;
}

View File

@ -4,12 +4,10 @@ import React from 'react';
import { shallow } from 'enzyme';
import { App } from 'components/App';
import renderImage from 'components/SVG';
import { translate } from '__mocks__/i18n';
describe('App', () => {
test('rendering', () => {
renderImage.mockReturnValue('Testing image');
const component = shallow(
<App t={ translate }/>
);

View File

@ -34,15 +34,14 @@ const render = (data, extraProps) => {
children.length === 1 ? children[0] : children);
};
const SVG = ({ data, imageRef: ref }) => render(data, { ref });
const SVG = React.forwardRef(({ data }, ref) => render(data, { ref }));
SVG.propTypes = {
data: PropTypes.shape({
type: PropTypes.oneOf(Object.keys(nodeTypes)).isRequired,
props: PropTypes.object,
children: PropTypes.array
}).isRequired,
imageRef: PropTypes.func
}).isRequired
};
export default SVG;

View File

@ -1,8 +1,7 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import { shallow } from 'enzyme';
import SVG from 'components/SVG';
import Pin from './Pin';
describe('SVG', () => {
test('rendering', async () => {
@ -94,16 +93,4 @@ describe('SVG', () => {
);
expect(component).toMatchSnapshot();
});
test('imageRef prop', () => {
let image;
const imageRef = element => image = element;
const data = {
type: 'Pin'
};
mount(
<SVG data={ data } imageRef={ imageRef }/>
);
expect(image).toBeInstanceOf(Pin);
});
});