-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from wheresrhys/sticky-routes
Sticky routes
- Loading branch information
Showing
8 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
title: '.sticky()' | ||
position: 3 | ||
versionAdded: 9.7.0 | ||
description: |- | ||
Shorthand for `mock()` which creates a route that persists even when `restore()`, `reset()` or `resetbehavior()` are called; | ||
parentMethodGroup: mocking | ||
content_markdown: |- | ||
This method is particularly useful for setting up fixtures that must remain in place for all tests, e.g. | ||
```js | ||
fetchMock.sticky(/config-hub.com/, require('./fixtures/start-up-config.json')) | ||
``` | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
const chai = require('chai'); | ||
chai.use(require('sinon-chai')); | ||
const expect = chai.expect; | ||
const sinon = require('sinon'); | ||
|
||
module.exports = (fetchMock, theGlobal) => { | ||
describe('sticky routes', () => { | ||
describe('effect on routes', () => { | ||
let fm; | ||
before(() => { | ||
fm = fetchMock.createInstance(); | ||
fm.config.warnOnUnmatched = false; | ||
}); | ||
|
||
afterEach(() => fm.restore({ sticky: true })); | ||
|
||
describe('resetting behaviour', () => { | ||
it('behaviour resists resetBehavior calls', async () => { | ||
fm.mock('*', 200, { sticky: true }).resetBehavior(); | ||
expect(fm.routes.length).to.equal(1); | ||
}); | ||
|
||
it('behaviour resists restore calls', async () => { | ||
fm.mock('*', 200, { sticky: true }).restore(); | ||
expect(fm.routes.length).to.equal(1); | ||
}); | ||
|
||
it('behaviour resists reset calls', async () => { | ||
fm.mock('*', 200, { sticky: true }).reset(); | ||
expect(fm.routes.length).to.equal(1); | ||
}); | ||
|
||
it('behaviour does not resist resetBehavior calls when sent `sticky: true`', async () => { | ||
fm.mock('*', 200, { sticky: true }).resetBehavior({ sticky: true }); | ||
expect(fm.routes.length).to.equal(0); | ||
}); | ||
|
||
it('behaviour does not resist restore calls when sent `sticky: true`', async () => { | ||
fm.mock('*', 200, { sticky: true }).restore({ sticky: true }); | ||
expect(fm.routes.length).to.equal(0); | ||
}); | ||
|
||
it('behaviour does not resist reset calls when sent `sticky: true`', async () => { | ||
fm.mock('*', 200, { sticky: true }).reset({ sticky: true }); | ||
expect(fm.routes.length).to.equal(0); | ||
}); | ||
}); | ||
|
||
describe('resetting history', () => { | ||
it('history does not resist resetHistory calls', async () => { | ||
fm.mock('*', 200, { sticky: true }); | ||
fm.fetchHandler('http://la.com'); | ||
fm.resetHistory(); | ||
expect(fm.called()).to.be.false; | ||
}); | ||
|
||
it('history does not resist restore calls', async () => { | ||
fm.mock('*', 200, { sticky: true }); | ||
fm.fetchHandler('http://la.com'); | ||
fm.restore(); | ||
expect(fm.called()).to.be.false; | ||
}); | ||
|
||
it('history does not resist reset calls', async () => { | ||
fm.mock('*', 200, { sticky: true }); | ||
fm.fetchHandler('http://la.com'); | ||
fm.reset(); | ||
expect(fm.called()).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('multiple routes', () => { | ||
it('can have multiple sticky routes', async () => { | ||
fm.mock('*', 200, { sticky: true }) | ||
.mock('http://la.com', 200, { sticky: true }) | ||
.resetBehavior(); | ||
expect(fm.routes.length).to.equal(2); | ||
}); | ||
|
||
it('can have a sticky route before non-sticky routes', async () => { | ||
fm.mock('*', 200, { sticky: true }) | ||
.mock('http://la.com', 200) | ||
.resetBehavior(); | ||
expect(fm.routes.length).to.equal(1); | ||
expect(fm.routes[0].url).to.equal('*'); | ||
}); | ||
|
||
it('can have a sticky route after non-sticky routes', async () => { | ||
fm.mock('*', 200) | ||
.mock('http://la.com', 200, { sticky: true }) | ||
.resetBehavior(); | ||
expect(fm.routes.length).to.equal(1); | ||
expect(fm.routes[0].url).to.equal('http://la.com'); | ||
}); | ||
}); | ||
}); | ||
describe('global mocking', () => { | ||
let originalFetch; | ||
before(() => { | ||
originalFetch = theGlobal.fetch = sinon | ||
.stub() | ||
.returns(Promise.resolve()); | ||
}); | ||
afterEach(() => fetchMock.restore({ sticky: true })); | ||
|
||
it('global mocking resists resetBehavior calls', async () => { | ||
fetchMock.mock(/a/, 200, { sticky: true }).resetBehavior(); | ||
expect(theGlobal.fetch).not.to.equal(originalFetch); | ||
}); | ||
|
||
it('global mocking does not resist resetBehavior calls when sent `sticky: true`', async () => { | ||
fetchMock | ||
.mock(/a/, 200, { sticky: true }) | ||
.resetBehavior({ sticky: true }); | ||
expect(theGlobal.fetch).to.equal(originalFetch); | ||
}); | ||
}); | ||
|
||
describe('sandboxes', () => { | ||
it('sandboxed instances should inherit stickiness', async () => { | ||
const sbx1 = fetchMock | ||
.sandbox() | ||
.mock(/a/, 200, { sticky: true }) | ||
.catch(300); | ||
|
||
const sbx2 = sbx1.sandbox().resetBehavior(); | ||
|
||
expect(sbx1.routes.length).to.equal(1); | ||
expect(sbx2.routes.length).to.equal(1); | ||
|
||
sbx2.resetBehavior({ sticky: true }); | ||
|
||
expect(sbx1.routes.length).to.equal(1); | ||
expect(sbx2.routes.length).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
}; |