Skip to content

Commit

Permalink
tidy sandbox tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed May 18, 2020
1 parent 9535102 commit 924759e
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions test/specs/sandbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,89 @@ describe('sandbox', () => {
expect(typeof sbx).to.equal('function');
});

it('port settings from parent instance', () => {
it('inherit settings from parent instance', () => {
const sbx = fetchMock.sandbox();
expect(sbx.config).to.eql(fetchMock.config);
});

it('implement full fetch-mock api', () => {
const sbx = fetchMock.sandbox();
for (const key in fetchMock) {
if (!['MATCHED', 'UNMATCHED', 'fetchMock'].includes(key)) {
expect(typeof sbx[key]).to.equal(typeof fetchMock[key]);
}
expect(typeof sbx[key]).to.equal(typeof fetchMock[key]);
}
});

it('delegate to its own fetch handler', async () => {
const sbx = fetchMock.sandbox().mock('http://domain.url/', 200);
const sbx = fetchMock.sandbox().mock('http://a.com', 200);

sinon.stub(sbx, 'fetchHandler');

sbx('http://domain.url');
expect(sbx.fetchHandler).calledWith('http://domain.url');
sbx('http://a.com');
expect(sbx.fetchHandler).calledWith('http://a.com');
});

it("don't interfere with global fetch", () => {
const sbx = fetchMock.sandbox().mock('http://domain.url/', 200);
const sbx = fetchMock.sandbox().mock('http://a.com', 200);

expect(theGlobal.fetch).to.equal(originalFetch);
expect(theGlobal.fetch).not.to.equal(sbx);
});

it("don't interfere with global fetch-mock", async () => {
const sbx = fetchMock.sandbox().mock('http://domain.url/', 200).catch(302);
const sbx = fetchMock.sandbox().mock('http://a.com', 200).catch(302);

fetchMock.mock('http://domain2.url/', 200).catch(301);
fetchMock.mock('http://b.com', 200).catch(301);

expect(theGlobal.fetch).to.equal(fetchMock.fetchHandler);
expect(fetchMock.fetchHandler).not.to.equal(sbx);
expect(fetchMock.fallbackResponse).not.to.equal(sbx.fallbackResponse);
expect(fetchMock.routes).not.to.equal(sbx.routes);

const [sandboxed, globally] = await Promise.all([
sbx('http://domain.url/'),
fetch('http://domain2.url/'),
sbx('http://a.com'),
fetch('http://b.com'),
]);

expect(sandboxed.status).to.equal(200);
expect(globally.status).to.equal(200);
expect(sbx.called('http://domain.url/')).to.be.true;
expect(sbx.called('http://domain2.url/')).to.be.false;
expect(fetchMock.called('http://domain2.url/')).to.be.true;
expect(fetchMock.called('http://domain.url/')).to.be.false;
expect(sbx.called('http://domain.url/')).to.be.true;
expect(sbx.called('http://a.com')).to.be.true;
expect(sbx.called('http://b.com')).to.be.false;
expect(fetchMock.called('http://b.com')).to.be.true;
expect(fetchMock.called('http://a.com')).to.be.false;
expect(sbx.called('http://a.com')).to.be.true;
fetchMock.restore();
});

it("don't interfere with other sandboxes", async () => {
const sbx = fetchMock.sandbox().mock('http://domain.url/', 200).catch(301);
const sbx = fetchMock.sandbox().mock('http://a.com', 200).catch(301);

const sbx2 = fetchMock
.sandbox()
.mock('http://domain2.url/', 200)
.catch(302);
const sbx2 = fetchMock.sandbox().mock('http://b.com', 200).catch(302);

expect(sbx2).not.to.equal(sbx);
expect(sbx2.fallbackResponse).not.to.equal(sbx.fallbackResponse);
expect(sbx2.routes).not.to.equal(sbx.routes);

const [res1, res2] = await Promise.all([
sbx('http://domain.url/'),
sbx2('http://domain2.url/'),
sbx('http://a.com'),
sbx2('http://b.com'),
]);
expect(res1.status).to.equal(200);
expect(res2.status).to.equal(200);
expect(sbx.called('http://domain.url/')).to.be.true;
expect(sbx.called('http://domain2.url/')).to.be.false;
expect(sbx2.called('http://domain2.url/')).to.be.true;
expect(sbx2.called('http://domain.url/')).to.be.false;
expect(sbx.called('http://a.com')).to.be.true;
expect(sbx.called('http://b.com')).to.be.false;
expect(sbx2.called('http://b.com')).to.be.true;
expect(sbx2.called('http://a.com')).to.be.false;
});

it('can be restored', async () => {
const sbx = fetchMock.sandbox().get('https://api.resin.io/foo', 200);
const sbx = fetchMock.sandbox().get('https://a.com', 200);

const res = await sbx('https://api.resin.io/foo');
const res = await sbx('https://a.com');
expect(res.status).to.equal(200);

sbx.restore().get('https://api.resin.io/foo', 500);
sbx.restore().get('https://a.com', 500);

const res2 = await sbx('https://api.resin.io/foo');
const res2 = await sbx('https://a.com');
expect(res2.status).to.equal(500);
});

Expand Down Expand Up @@ -141,7 +136,7 @@ describe('sandbox', () => {

expect(fetch.name).to.equal('fetchMockProxy');
expect(new Headers()).to.be.an.instanceOf(fetchMock.config.Headers);
expect(new Request('http://example.com')).to.be.an.instanceOf(
expect(new Request('http://a.com')).to.be.an.instanceOf(
fetchMock.config.Request
);
expect(new Response()).to.be.an.instanceOf(fetchMock.config.Response);
Expand Down

0 comments on commit 924759e

Please sign in to comment.