Skip to content

Commit

Permalink
refined tests, and fixed scoping of repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Mar 17, 2018
1 parent f5d6343 commit 011196f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ const generateMatcher = (route, config) => {
}

const limitMatcher = (route) => {

if (!route.repeat) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ FetchMock.config = {

FetchMock.createInstance = function () {
const instance = Object.create(FetchMock);
instance.routes = (this.routes || []).slice();
instance._uncompiledRoutes = (this._uncompiledRoutes || []).slice();
instance.routes = instance._uncompiledRoutes.map(config => instance.compileRoute(config));
instance.fallbackResponse = this.fallbackResponse || undefined;
instance.config = Object.assign({}, this.config || FetchMock.config);
instance._calls = {};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/inspecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ FetchMock.done = function (name) {
.filter(bool => !bool).length === 0
};

module.exports = FetchMock;
module.exports = FetchMock;
4 changes: 3 additions & 1 deletion src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ FetchMock.mock = function (matcher, response, options = {}) {
const getMatcher = (route, propName) => (route2) => route[propName] === route2[propName];

FetchMock.addRoute = function (route) {
this._uncompiledRoutes.push(route);
route = this.compileRoute(route);

const clashes = this.routes.filter(getMatcher(route, 'name'));
Expand Down Expand Up @@ -91,6 +92,7 @@ FetchMock.restore = function () {
}
this.fallbackResponse = undefined;
this.routes = [];
this._uncompiledRoutes = [];
this.reset();
return this;
}
Expand All @@ -103,4 +105,4 @@ FetchMock.reset = function () {
return this;
}

module.exports = FetchMock;
module.exports = FetchMock;
33 changes: 21 additions & 12 deletions test/specs/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,53 +129,62 @@ module.exports = (fetchMock) => {
});

describe('sandbox isolation', () => {
it('done-ness doesn\'t propagate to children of global', () => {
it('doesn\'t propagate to children of global', () => {
fm
.mock('http://it.at.there1/', 200, {repeat: 1});
.mock('http://it.at.there/', 200, {repeat: 1});

const sb1 = fm.sandbox();

fm.fetchHandler('http://it.at.there1/');
fm.fetchHandler('http://it.at.there/');

expect(fm.done()).to.be.true;
expect(sb1.done()).to.be.false;

expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw();
});

it('done-ness doesn\'t propagate to global from children', () => {
it('doesn\'t propagate to global from children', () => {
fm
.mock('http://it.at.there1/', 200, {repeat: 1});
.mock('http://it.at.there/', 200, {repeat: 1});

const sb1 = fm.sandbox();

sb1.fetchHandler('http://it.at.there1/');
sb1.fetchHandler('http://it.at.there/');

expect(fm.done()).to.be.false;
expect(sb1.done()).to.be.true;

expect(() => fm.fetchHandler('http://it.at.there/')).not.to.throw();
});

it('done-ness doesn\'t propagate to children of sandbox', () => {
it('doesn\'t propagate to children of sandbox', () => {
const sb1 = fm
.sandbox()
.mock('http://it.at.there1/', 200, {repeat: 1});
.mock('http://it.at.there/', 200, {repeat: 1});

const sb2 = sb1.sandbox();

sb1.fetchHandler('http://it.at.there1/');
sb1.fetchHandler('http://it.at.there/');

expect(sb1.done()).to.be.true;
expect(sb2.done()).to.be.false;

expect(() => sb2.fetchHandler('http://it.at.there/')).not.to.throw();
});

it('done-ness doesn\'t propagate to sandbox from children', () => {
it('doesn\'t propagate to sandbox from children', () => {
const sb1 = fm
.sandbox()
.mock('http://it.at.there1/', 200, {repeat: 1});
.mock('http://it.at.there/', 200, {repeat: 1});

const sb2 = sb1.sandbox();

sb2.fetchHandler('http://it.at.there1/');
sb2.fetchHandler('http://it.at.there/');

expect(sb1.done()).to.be.false;
expect(sb2.done()).to.be.true;

expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw();
});
});

Expand Down

0 comments on commit 011196f

Please sign in to comment.