From 763e394b607c84c143016fb612c17575880d3d12 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 17 May 2015 20:04:20 +0100 Subject: [PATCH] refined behaviour of called() method --- README.md | 4 ++-- src/fetch-mock.js | 3 +++ test/spec.js | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1cc982ad..24f112b5 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,10 @@ Restores `fetch()` to its unstubbed state and clears all data recorded for its c Clears all data recorded for `fetch()`'s calls ### `calls(routeName)` -Returns an array of arrays of the arguments passed to `fetch()` that matched the given route +Returns an array of arrays of the arguments passed to `fetch()` that matched the given route. '__unmatched' can be passed in to return results for calls not matching any route. ### `called(routeName)` -Returns a Boolean denoting whether any calls matched the given route +Returns a Boolean denoting whether any calls matched the given route. '__unmatched' can be passed in to return results for calls not matching any route. If no routeName is passed it returns `true` if any fetch calls were made ### `reMock()` Normally calling `mock()` twice without restoring inbetween will throw an error. `reMock()` calls `restore()` internally before calling `mock()` again. This allows you to put a generic call to `mock()` in a `beforeEach()` while retaining the flexibility to vary the responses for some tests diff --git a/src/fetch-mock.js b/src/fetch-mock.js index 42e55202..5e8cded4 100644 --- a/src/fetch-mock.js +++ b/src/fetch-mock.js @@ -309,6 +309,9 @@ FetchMock.prototype.calls = function (name) { }; FetchMock.prototype.called = function (name) { + if (!name) { + return !!Object.keys(this._calls).length; + } return !!(this._calls[name] && this._calls[name].length); }; diff --git a/test/spec.js b/test/spec.js index e9771692..104bbbdc 100644 --- a/test/spec.js +++ b/test/spec.js @@ -129,6 +129,8 @@ module.exports = function (fetchMock, theGlobal) { fetchMock.mock(); Promise.all([fetch('http://1', {method: 'GET'}), fetch('http://2', {method: 'POST'})]) .then(function () { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('__unmatched')).to.be.true; var unmatchedCalls = fetchMock.calls('__unmatched'); expect(unmatchedCalls.length).to.equal(2); expect(unmatchedCalls[0]).to.eql(['http://1', {method: 'GET'}]); @@ -141,6 +143,8 @@ module.exports = function (fetchMock, theGlobal) { fetchMock.mock({greed: 'good'}); fetch('http://1') .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('__unmatched')).to.be.true; expect(fetchMock.calls('__unmatched').length).to.equal(1); expect(res.status).to.equal(200); res.text().then(function (text) { @@ -154,6 +158,8 @@ module.exports = function (fetchMock, theGlobal) { fetchMock.mock({greed: 'bad'}); fetch('http://1') .catch(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('__unmatched')).to.be.true; expect(res).to.equal('unmocked url: http://1'); done(); }); @@ -163,6 +169,8 @@ module.exports = function (fetchMock, theGlobal) { fetchMock.mock({greed: 'none'}); fetch('http://1') .then(function () { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('__unmatched')).to.be.true; expect(fetchCalls.length).to.equal(1); expect(fetchCalls[0].length).to.equal(2); done(); @@ -182,6 +190,8 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there'), fetch('http://it.at.thereabouts')]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route')).to.be.true; expect(fetchMock.calls('route').length).to.equal(1); expect(fetchMock.calls('__unmatched').length).to.equal(1); done(); @@ -198,6 +208,8 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there'), fetch('http://it.at.thereabouts'), fetch('http://it.at.hereabouts')]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route')).to.be.true; expect(fetchMock.calls('route').length).to.equal(2); expect(fetchMock.calls('__unmatched').length).to.equal(1); done(); @@ -214,6 +226,8 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.there/12345'), fetch('http://it.at.there/abcde')]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route')).to.be.true; expect(fetchMock.calls('route').length).to.equal(1); expect(fetchMock.calls('__unmatched').length).to.equal(2); done(); @@ -236,6 +250,8 @@ module.exports = function (fetchMock, theGlobal) { fetch('http://it.at.there/logged-in') ]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route')).to.be.true; expect(fetchMock.calls('route').length).to.equal(1); expect(fetchMock.calls('__unmatched').length).to.equal(2); done(); @@ -256,6 +272,9 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there'), fetch('http://it.at.here'), fetch('http://it.at.nowhere')]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route1')).to.be.true; + expect(fetchMock.called('route2')).to.be.true; expect(fetchMock.calls('route1').length).to.equal(1); expect(fetchMock.calls('route2').length).to.equal(1); expect(fetchMock.calls('__unmatched').length).to.equal(1); @@ -277,6 +296,8 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there')]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route1')).to.be.true; expect(fetchMock.calls('route1').length).to.equal(1); expect(fetchMock.calls('route2').length).to.equal(0); done(); @@ -293,6 +314,8 @@ module.exports = function (fetchMock, theGlobal) { }); Promise.all([fetch('http://it.at.there'), fetch('http://it.at.thereabouts', {headers: {head: 'val'}})]) .then(function (res) { + expect(fetchMock.called()).to.be.true; + expect(fetchMock.called('route')).to.be.true; expect(fetchMock.calls('route')[0]).to.eql(['http://it.at.there', undefined]); expect(fetchMock.calls('route')[1]).to.eql(['http://it.at.thereabouts', {headers: {head: 'val'}}]); done(); @@ -310,6 +333,8 @@ module.exports = function (fetchMock, theGlobal) { fetch('http://it.at.there') .then(function (res) { fetchMock.reset(); + expect(fetchMock.called()).to.be.false; + expect(fetchMock.called('route')).to.be.false; expect(fetchMock.calls('route').length).to.equal(0); done(); }); @@ -326,6 +351,8 @@ module.exports = function (fetchMock, theGlobal) { fetch('http://it.at.there') .then(function (res) { fetchMock.restore(); + expect(fetchMock.called()).to.be.false; + expect(fetchMock.called('route')).to.be.false; expect(fetchMock.calls('route').length).to.equal(0); done(); });