From 2ba5cb6a1f8dcfd26b7319de5a5c2655165927c8 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 27 Oct 2019 17:45:51 +0000 Subject: [PATCH 1/4] test for delay option --- test/specs/responses.test.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/specs/responses.test.js b/test/specs/responses.test.js index d3bb87a1..1f93dfa6 100644 --- a/test/specs/responses.test.js +++ b/test/specs/responses.test.js @@ -221,6 +221,7 @@ 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/'); @@ -235,17 +236,29 @@ 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')); 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'); }); 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')); 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'); + }); + + 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; + expect(res.status).to.equal(200); }); it('Response', async () => { From 45fc743cd49f3d1229c8364a329ba260546c14d4 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 27 Oct 2019 17:59:02 +0000 Subject: [PATCH 2/4] implemented delay behaviour --- src/lib/compile-route.js | 9 +++++++++ test/specs/responses.test.js | 25 +++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/lib/compile-route.js b/src/lib/compile-route.js index 4adc54ba..c19a4d51 100644 --- a/src/lib/compile-route.js +++ b/src/lib/compile-route.js @@ -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; }; diff --git a/test/specs/responses.test.js b/test/specs/responses.test.js index 1f93dfa6..1ea76ea9 100644 --- a/test/specs/responses.test.js +++ b/test/specs/responses.test.js @@ -236,17 +236,17 @@ module.exports = fetchMock => { }); it('function that returns a Promise', async () => { - fm.mock('http://it.at.there/', url => Promise.resolve('test')); + 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('test'); + 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 => 'test')); + 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('test'); + expect(await res.text()).to.equal('test: http://it.at.there/'); }); it('delay', async () => { @@ -258,9 +258,26 @@ module.exports = fetchMock => { 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 () => { fm.mock( 'http://it.at.there/', From 3bfc7ebe754b0e9612d53ce1c0fb866193423753 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 27 Oct 2019 18:00:43 +0000 Subject: [PATCH 3/4] documented the new behaviour --- docs/_api-mocking/mock_options.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/_api-mocking/mock_options.md b/docs/_api-mocking/mock_options.md index 46a242fc..18c34423 100644 --- a/docs/_api-mocking/mock_options.md +++ b/docs/_api-mocking/mock_options.md @@ -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 From 862eac05ba683f8f0a1c2ee49a8272a8edea2129 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 27 Oct 2019 18:02:17 +0000 Subject: [PATCH 4/4] lint --- src/lib/compile-route.js | 10 +++++----- test/specs/responses.test.js | 25 ++++++++++++++----------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/lib/compile-route.js b/src/lib/compile-route.js index c19a4d51..dc95cabb 100644 --- a/src/lib/compile-route.js +++ b/src/lib/compile-route.js @@ -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; }; diff --git a/test/specs/responses.test.js b/test/specs/responses.test.js index 1ea76ea9..a9b3fd39 100644 --- a/test/specs/responses.test.js +++ b/test/specs/responses.test.js @@ -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/'); @@ -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); });