This file contains hidden or 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 logo from './logo.svg'; | |
| import './App.css'; | |
| import LoadingIndicator from './components/LoadingIndicator'; | |
| class App extends Component { | |
| state = { | |
| isLoading: true, | |
| }; |
This file contains hidden or 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('on unmount', () => { | |
| it('should clear timeout', () => { | |
| jest.useFakeTimers(); | |
| const mockTimerValue = 12345; | |
| setTimeout.mockReturnValue(mockTimerValue); | |
| const wrapper = mount( | |
| <LoadingIndicator isLoading={true}> | |
| <div>ahoy!</div> |
This file contains hidden or 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
| componentWillUnmount() { | |
| clearTimeout(); | |
| } |
This file contains hidden or 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
| componentWillUnmount() { | |
| clearTimeout(this._delayTimer); | |
| } | |
This file contains hidden or 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('on unmount', () => { | |
| it('should clear timeout', () => { | |
| jest.useFakeTimers(); | |
| const wrapper = mount( | |
| <LoadingIndicator isLoading={true}> | |
| <div>ahoy!</div> | |
| </LoadingIndicator> | |
| ); | |
| wrapper.unmount(); |
This file contains hidden or 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 hidden or 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> | |
| ); |
This file contains hidden or 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 }), 100 | |
| ); | |
| } | |
This file contains hidden or 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
| export default class LoadingIndicator extends Component { | |
| static propTypes = { | |
| isLoading: PropTypes.bool.isRequired, | |
| }; | |
| state = { | |
| isPastDelay: false | |
| }; | |
| componentDidMount () { |
This file contains hidden or 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) { | |
| if (!this.state.isPastDelay) { | |
| return null; | |
| } | |
| return <div>loading...</div>; | |
| } | |
| return this.props.children; | |
| } |
NewerOlder