This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
componentDidMount () { | |
this._delayTimer = setTimeout( | |
() => this.setState({ isPastDelay: true }), 200 | |
) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
state = { | |
isPastDelay: false | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render() { | |
if (this.props.isLoading) { | |
return null; | |
} | |
return this.props.children; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
export default class LoadingIndicator extends Component { | |
render() { | |
return this.props.children; | |
} | |
} | |
LoadingIndicator.propTypes = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { mount } from 'enzyme'; | |
import LoadingIndicator from './LoadingIndicator' | |
describe('LoadingIndicator', () => { | |
describe('when isLoading is false', () => { | |
it('should render children', () => { | |
const wrapper = mount( | |
<LoadingIndicator isLoading={false}> | |
<div>ahoy!</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
export default class LoadingIndicator extends Component { | |
render() { | |
return null; | |
} | |
} | |
LoadingIndicator.propTypes = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Promise.all is good for executing many promises at once | |
Promise.all([ | |
promise1, | |
promise2 | |
]); | |
// Promise.resolve is good for wrapping synchronous code | |
Promise.resolve().then(function () { | |
if (somethingIsNotRight()) { | |
throw new Error("I will be rejected asynchronously!"); |