Skip to content

Instantly share code, notes, and snippets.

@Gpx
Created December 7, 2017 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gpx/c5ce5395574201fdbdc8fe7164e42796 to your computer and use it in GitHub Desktop.
Save Gpx/c5ce5395574201fdbdc8fe7164e42796 to your computer and use it in GitHub Desktop.
// @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
describe('<SmartComponent />', () => {
it('should be possible to easily mock API calls', () => {
const fetchUsers = jest.fn(() => Promise.resolve([]))
const component = shallow(<SmartComponent fetchUsers={fetchUsers} />)
})
})
Copy link

ghost commented Jan 11, 2018

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment