Skip to content

Commit

Permalink
Merge pull request #14 from wheresrhys/called
Browse files Browse the repository at this point in the history
refined behaviour of called() method
  • Loading branch information
wheresrhys committed May 17, 2015
2 parents 8efd33c + 763e394 commit 1352d9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/fetch-mock.js
Expand Up @@ -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);
};

Expand Down
27 changes: 27 additions & 0 deletions test/spec.js
Expand Up @@ -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'}]);
Expand All @@ -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) {
Expand All @@ -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();
});
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down

0 comments on commit 1352d9f

Please sign in to comment.