Mock XHR requests declaratively
npm install @react-mock/xhrA declarative wrapper for the great xhr-mock.
> Note: XhrMock mocks the global XMLHttpRequest API, so only one XhrMock instance should be rendered at once.
``js
import { XhrMock } from '@react-mock/xhr';
// GET
render(
response={(req, res) => res.body(JSON.stringify(users))}
>
);
// POST
render(
);
`
`js
const res = body => (req, res) => res.body(JSON.stringify(body));
render(
{ url: '/users', response: res([{ id: 123 }]) },
{ url: '/user/123', response: res({ name: 'Jessica' }) }
]}
>
);
``