2021-05-11 10:25:22 -04:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import fetchMock from 'fetch-mock'
|
2022-04-04 08:31:47 -04:00
|
|
|
fetchMock.config.fallbackToNetwork = true
|
2021-05-11 10:25:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run callback to mock fetch routes, call restore() when unmounted
|
|
|
|
*/
|
|
|
|
export default function useFetchMock(callback) {
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
fetchMock.restore()
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
// Running fetchMock.restore() here as well,
|
|
|
|
// in case there was an error before the component was unmounted.
|
|
|
|
fetchMock.restore()
|
|
|
|
|
|
|
|
// The callback has to be run here, rather than in useEffect,
|
|
|
|
// so it's run before the component is rendered.
|
|
|
|
callback(fetchMock)
|
|
|
|
}
|