Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
Browse files Browse the repository at this point in the history
…o v8

* 'master' of https://github.com/wheresrhys/fetch-mock:
  lint
  documented the new behaviour
  implemented delay behaviour
  test for delay option
  • Loading branch information
wheresrhys committed Oct 27, 2019
2 parents c044450 + c8e797c commit 8270ade
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/_api-mocking/mock_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ parameters:
- Integer
content: |-
Limits the number of times the route can be used. If the route has already been called `repeat` times, the call to `fetch()` will fall through to be handled by any other routes defined (which may eventually result in an error if nothing matches it)
- name: delay
types:
- Integer
content: |-
Delays responding for the number of milliseconds specified.
- name: overwriteRoutes
types:
- Boolean
Expand Down
9 changes: 9 additions & 0 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ const limitMatcher = route => {
route.reset = () => (timesLeft = route.repeat);
};

const delayResponse = route => {
const { delay } = route;
if (delay) {
const response = route.response;
route.response = new Promise(res => setTimeout(() => res(response), delay));
}
};

module.exports = route => {
validateRoute(route);
route = sanitizeRoute(route);
route.matcher = generateMatcher(route);
limitMatcher(route);
delayResponse(route);
return route;
};

Expand Down
41 changes: 37 additions & 4 deletions test/specs/responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,50 @@ module.exports = fetchMock => {
});

it('function that returns a Promise', async () => {
fm.mock('http://it.at.there/', url => Promise.resolve(url));
fm.mock('http://it.at.there/', url => Promise.resolve('test: ' + url));
const res = await fm.fetchHandler('http://it.at.there/');
expect(res.status).to.equal(200);
expect(await res.text()).to.equal('http://it.at.there/');
expect(await res.text()).to.equal('test: http://it.at.there/');
});

it('Promise for a function that returns a response', async () => {
fm.mock('http://it.at.there/', Promise.resolve(url => url));
fm.mock('http://it.at.there/', Promise.resolve(url => 'test: ' + url));
const res = await fm.fetchHandler('http://it.at.there/');
expect(res.status).to.equal(200);
expect(await res.text()).to.equal('http://it.at.there/');
expect(await res.text()).to.equal('test: http://it.at.there/');
});

it('delay', async () => {
fm.mock('http://it.at.there/', 200, { delay: 20 });
const req = fm.fetchHandler('http://it.at.there/');
let resolved = false;
req.then(() => (resolved = true));
await new Promise(res => setTimeout(res, 10));
expect(resolved).to.be.false;
await new Promise(res => setTimeout(res, 11));
expect(resolved).to.be.true;
const res = await req;
expect(res.status).to.equal(200);
});

it("delay a function response's execution", async () => {
const startTimestamp = new Date().getTime();
fm.mock(
'http://it.at.there/',
() => ({ timestamp: new Date().getTime() }),
{ delay: 20 }
);
const req = fm.fetchHandler('http://it.at.there/');
let resolved = false;
req.then(() => (resolved = true));
await new Promise(res => setTimeout(res, 10));
expect(resolved).to.be.false;
await new Promise(res => setTimeout(res, 11));
expect(resolved).to.be.true;
const res = await req;
expect(res.status).to.equal(200);
const responseTimestamp = (await res.json()).timestamp;
expect(responseTimestamp - startTimestamp).to.be.within(20, 25);
});

it('Response', async () => {
Expand Down

0 comments on commit 8270ade

Please sign in to comment.