Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Oct 27, 2019
1 parent 3bfc7eb commit 862eac0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ const limitMatcher = route => {
route.reset = () => (timesLeft = route.repeat);
};

const delayResponse = (route) => {
const {delay} = route;
const delayResponse = route => {
const { delay } = route;
if (delay) {
const response = route.response;
route.response = new Promise(res => setTimeout(() => res(response), delay))
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)
delayResponse(route);
return route;
};

Expand Down
25 changes: 14 additions & 11 deletions test/specs/responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ module.exports = fetchMock => {
});

describe('response negotiation', () => {

it('function', async () => {
fm.mock('http://it.at.there/', url => url);
const res = await fm.fetchHandler('http://it.at.there/');
Expand Down Expand Up @@ -250,31 +249,35 @@ module.exports = fetchMock => {
});

it('delay', async () => {
fm.mock('http://it.at.there/', 200, {delay: 20});
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))
req.then(() => (resolved = true));
await new Promise(res => setTimeout(res, 10));
expect(resolved).to.be.false;
await new Promise(res => setTimeout(res, 11))
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 () => {
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});
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))
req.then(() => (resolved = true));
await new Promise(res => setTimeout(res, 10));
expect(resolved).to.be.false;
await new Promise(res => setTimeout(res, 11))
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
const responseTimestamp = (await res.json()).timestamp;
expect(responseTimestamp - startTimestamp).to.be.within(20, 25);
});

Expand Down

0 comments on commit 862eac0

Please sign in to comment.