Created
December 7, 2017 23:08
-
-
Save Gpx/c5ce5395574201fdbdc8fe7164e42796 to your computer and use it in GitHub Desktop.
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
// @flow | |
import React from 'react' | |
import { fetchUsers, type User } from '../api/users' | |
type Props = { fetchUsers: typeof fetchUsers } | |
type State = { users: ?Array<User>} | |
class SmartComponent extends React.Component<Props, State> { | |
static defaultProps = { fetchUsers } | |
constructor(props: Props) { | |
super(props) | |
this.state = { users: null } | |
} | |
componentDidMount() { | |
this.fetchUsers() | |
} | |
async fetchUsers() { | |
const users = await this.props.fetchUsers() | |
this.setState({ users }) | |
} | |
render() { | |
const { users } = this.props | |
return users ? <div>{users.length}</div> : null | |
} | |
} | |
export default SmartComponent |
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('<SmartComponent />', () => { | |
it('should be possible to easily mock API calls', () => { | |
const fetchUsers = jest.fn(() => Promise.resolve([])) | |
const component = shallow(<SmartComponent fetchUsers={fetchUsers} />) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
💯