Skip to content

Commit

Permalink
refactor: Replaces findDomNode() with refs (#9124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi347 authored and SimenB committed Nov 2, 2019
1 parent 9a9dbf2 commit 220835c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/react/CheckboxWithLabel.js
Expand Up @@ -14,8 +14,9 @@ export default class CheckboxWithLabel extends React.Component {

render() {
return (
<label>
<label ref={this.props.labelRef}>
<input
ref={this.props.inputRef}
type="checkbox"
checked={this.state.isChecked}
onChange={this.onChange}
Expand Down
26 changes: 16 additions & 10 deletions examples/react/__tests__/CheckboxWithLabel-test.js
@@ -1,24 +1,30 @@
// Copyright 2004-present Facebook. All Rights Reserved.

import React from 'react';
import ReactDOM from 'react-dom';
import React, {createRef} from 'react';

import * as TestUtils from 'react-dom/test-utils';
import CheckboxWithLabel from '../CheckboxWithLabel';

it('CheckboxWithLabel changes the text after click', () => {
const checkboxLabelRef = createRef();
const checkboxInputRef = createRef();
// Render a checkbox with label in the document
const checkbox = TestUtils.renderIntoDocument(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
TestUtils.renderIntoDocument(
<CheckboxWithLabel
labelRef={checkboxLabelRef}
inputRef={checkboxInputRef}
labelOn="On"
labelOff="Off"
/>
);

const checkboxNode = ReactDOM.findDOMNode(checkbox);
const labelNode = checkboxLabelRef.current;
const inputNode = checkboxInputRef.current;

// Verify that it's Off by default
expect(checkboxNode.textContent).toEqual('Off');
expect(labelNode.textContent).toEqual('Off');

// Simulate a click and verify that it is now On
TestUtils.Simulate.change(
TestUtils.findRenderedDOMComponentWithTag(checkbox, 'input')
);
expect(checkboxNode.textContent).toEqual('On');
TestUtils.Simulate.change(inputNode);
expect(labelNode.textContent).toEqual('On');
});

0 comments on commit 220835c

Please sign in to comment.