Skip to content

Instantly share code, notes, and snippets.

View bruceharris's full-sized avatar

Bruce Harris bruceharris

View GitHub Profile
@bruceharris
bruceharris / App.js
Created March 2, 2018 15:52
React Unit Testing Example 4
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
@bruceharris
bruceharris / LoadingIndicator.test.js
Created March 2, 2018 16:06
React Unit Testing Example 5
describe('when isLoading is true', () => {
describe('given 200ms have not yet elapsed', () => {
it('should render nothing', () => {
const wrapper = mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>
);
expect(wrapper.html()).toBe(null);
wrapper.unmount();
@bruceharris
bruceharris / LoadingIndicator.js
Created March 2, 2018 16:09
React Unit Testing Example 6
render() {
if (this.props.isLoading) {
return null;
}
return this.props.children;
}
@bruceharris
bruceharris / LoadingIndicator.test.js
Created March 2, 2018 16:14
React Unit Testing Example 7
describe('when isLoading is true', () => {
describe('given 200ms have elapsed', () => {
it('should render loading indicator', () => {
jest.useFakeTimers();
const wrapper = mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>
);
jest.runAllTimers();
@bruceharris
bruceharris / LoadingIndicator.js
Created March 2, 2018 16:19
React Unit Testing Example 8
state = {
isPastDelay: false
};
@bruceharris
bruceharris / LoadingIndicator.js
Created March 2, 2018 16:20
React Unit Testing Example 9
componentDidMount () {
this._delayTimer = setTimeout(
() => this.setState({ isPastDelay: true }), 200
)
}
@bruceharris
bruceharris / LoadingIndicator.js
Created March 2, 2018 16:22
React Unit Testing Example 10
render() {
if (this.props.isLoading) {
if (!this.state.isPastDelay) {
return null;
}
return <div>loading...</div>;
}
return this.props.children;
}
@bruceharris
bruceharris / LoadingIndicator.js
Last active March 2, 2018 16:30
React Unit Testing Example 11
export default class LoadingIndicator extends Component {
static propTypes = {
isLoading: PropTypes.bool.isRequired,
};
state = {
isPastDelay: false
};
componentDidMount () {
@bruceharris
bruceharris / LoadingIndicator.js
Last active March 2, 2018 16:29
React Unit Testing Example 12
componentDidMount () {
this._delayTimer = setTimeout(
() => this.setState({ isPastDelay: true }), 100
);
}
@bruceharris
bruceharris / LoadingIndicator.test.js
Created March 2, 2018 16:27
React Unit Testing Example 13
describe('when isLoading is true', () => {
describe('given 200ms have elapsed', () => {
it('should render loading indicator', () => {
jest.useFakeTimers();
const wrapper = mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>
);