Skip to content

Commit 61bf0ff

Browse files
committed
Migrate tests to React Testing Library + Drop enzyme
1 parent c9e6ea2 commit 61bf0ff

File tree

11 files changed

+645
-1152
lines changed

11 files changed

+645
-1152
lines changed

package-lock.json

Lines changed: 419 additions & 1025 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@
5656
"@babel/preset-react": "^7.13.13",
5757
"@babel/preset-stage-2": "^7.8.3",
5858
"@hot-loader/react-dom": "^16.14.0",
59+
"@testing-library/jest-dom": "^5.11.10",
60+
"@testing-library/react": "^11.2.6",
61+
"@testing-library/user-event": "^13.1.5",
5962
"@types/react": "^16.14.5",
6063
"@types/react-dom": "^16.9.12",
6164
"babel-eslint": "^10.1.0",
6265
"babel-jest": "^26.6.3",
6366
"babel-loader": "^8.2.2",
6467
"babel-plugin-transform-rename-import": "^2.3.0",
6568
"css-loader": "^5.2.1",
66-
"enzyme": "^3.11.0",
67-
"enzyme-adapter-react-16": "^1.15.6",
6869
"eslint": "^7.23.0",
6970
"eslint-config-airbnb": "^18.2.1",
7071
"eslint-plugin-import": "^2.22.1",

src/components/LifecyclePanel.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const LifecyclePanel = (props) => {
99
const lifecycleMethodNames = isLegacy ? constants.lifecycleMethodNamesLegacyNoUnsafe : constants.lifecycleMethodNames;
1010

1111
return (
12-
<div className='lifecycle-panel'>
12+
<div className='lifecycle-panel' data-testid='lifecycle-panel'>
1313
<div className='lifecycle-panel-inner'>
1414
<div className='component-instance'>{componentName + '-' + instanceId}</div>
1515
{ lifecycleMethodNames.map((methodName) => (

src/components/LogEntries.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class LogEntries extends Component {
1818
({componentName, instanceId}) => componentName.length + ('' + instanceId).length + 1)
1919
);
2020
return (
21-
<div className='entries' ref={(elt) => { this.messagesElt = elt; }}>
21+
<div className='entries' data-testid='log-entries' ref={(elt) => { this.messagesElt = elt; }}>
2222
{ this.props.entries.map(({componentName, instanceId, methodName}, i) => (
2323
<div className='entry-wrapper' key={i}>
2424
<div

test/TracedChild.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ class Child extends Component {
2424

2525
render() {
2626
this.props.trace('custom:render');
27-
return <this.props.LifecyclePanel/>;
27+
return (
28+
<div>
29+
<input type='button' data-testid='state-update-button' onClick={this.updateState}/>
30+
<this.props.LifecyclePanel/>
31+
</div>
32+
);
2833
}
2934

3035
componentDidMount() {

test/TracedLegacyChild.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ class LegacyChild extends Component {
2929

3030
render() {
3131
this.props.trace('custom:render');
32-
return <this.props.LifecyclePanel/>;
32+
return (
33+
<div>
34+
<input type='button' data-testid='state-update-button' onClick={this.updateState}/>
35+
<this.props.LifecyclePanel/>
36+
</div>
37+
);
3338
}
3439

3540
componentDidMount() {

test/Wrapper.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, {useState } from 'react';
2+
import { Log, VisualizerProvider } from '../src';
3+
4+
const StateToggle = ({testId, checked, setChecked}) => {
5+
const onChange = ({currentTarget}) => setChecked(currentTarget.checked);
6+
7+
return <input type='checkbox' data-testid={testId} checked={checked} onChange={onChange}/>;
8+
};
9+
10+
// Wrapper to add VisualizerProvider and log, with buttons for showing/hiding child and updating child prop.
11+
export const Wrapper = ({renderChild}) => {
12+
const [isShowingChild, setIsShowingChild] = useState(true);
13+
const [propValue, setPropValue] = useState(false);
14+
return (
15+
<VisualizerProvider>
16+
<div>
17+
<StateToggle testId='show-child-checkbox' checked={isShowingChild} setChecked={setIsShowingChild}/>
18+
<StateToggle testId='prop-value-checkbox' checked={propValue} setChecked={setPropValue}/>
19+
{isShowingChild && renderChild({prop: true})}
20+
<Log/>
21+
</div>
22+
</VisualizerProvider>
23+
);
24+
};

0 commit comments

Comments
 (0)