Skip to content

Instantly share code, notes, and snippets.

@StanleySathler
Created January 9, 2019 04:12
Show Gist options
  • Save StanleySathler/d79f651ccc152b49bb27d290d086ba9f to your computer and use it in GitHub Desktop.
Save StanleySathler/d79f651ccc152b49bb27d290d086ba9f to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
/**
* Creates the component that will be tested with
* Jest and Enzyme
*/
class MessageDisplay extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.state = {
index: 0,
texts: ['Jest', 'I ❤ React', 'Enzyme'],
};
}
handleClick() {
const index = this.state.index;
const updatedIndex = (index < 2) ? (index + 1) : 0;
this.setState({
index: updatedIndex,
});
}
render() {
const { index, texts } = this.state;
return (
<div>
<button onClick={this.handleClick}>
Click to change the text
</button>
<span>
{ texts[index] }
</span>
</div>
);
}
}
/**
* Renders the component on the DOM
*/
ReactDOM.render(<MessageDisplay />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment