Skip to content

Instantly share code, notes, and snippets.

@imdongchen
Created April 29, 2021 15:41
Show Gist options
  • Save imdongchen/b4fa8319b6e7516f9bc0da533a7466a8 to your computer and use it in GitHub Desktop.
Save imdongchen/b4fa8319b6e7516f9bc0da533a7466a8 to your computer and use it in GitHub Desktop.
import xhrMock from 'xhr-mock'
export class ApiMock extends Component<TProps> {
constructor(props: TProps) {
super(props)
this.mock()
}
componentWillUnmount() {
this.unmock()
}
render() {
return this.props.children
}
mock() {
this.unmock()
xhrMock.setup()
this.props.mocks.forEach(({ method, url, response })=> {
xhrMock.use(method, url, response)
})
}
unmock() {
xhrMock.teardown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment