Skip to content

Commit

Permalink
fix: Loosen up global XHR native check (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored and jasonmit committed Jul 19, 2018
1 parent 7ef9aae commit 79cdd96
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Adapter from '@pollyjs/adapter';
import { Fetch as FetchUtils } from '@pollyjs/utils';

const { defineProperty } = Object;
const STUB_META = Symbol();
const IS_STUBBED = Symbol();

export default class FetchAdapter extends Adapter {
static get name() {
Expand All @@ -22,7 +22,7 @@ export default class FetchAdapter extends Adapter {
this.assert('Response global not found.', !!(context && context.Response));
this.assert(
'Running concurrent fetch adapters is unsupported, stop any running Polly instances.',
!(STUB_META in context.fetch)
!context.fetch[IS_STUBBED]
);

this.native = context.fetch;
Expand All @@ -36,7 +36,7 @@ export default class FetchAdapter extends Adapter {
requestArguments: [url, options]
});

defineProperty(context.fetch, STUB_META, { value: true });
defineProperty(context.fetch, IS_STUBBED, { value: true });
}

onDisconnect() {
Expand Down
10 changes: 7 additions & 3 deletions packages/@pollyjs/adapter-xhr/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import FakeXHR from 'nise/lib/fake-xhr';
import Adapter from '@pollyjs/adapter';
import { XHR as XHRUtils } from '@pollyjs/utils';
import resolveXhr from './utils/resolve-xhr';
import { XHR as XHRUtils } from '@pollyjs/utils';

const SEND = Symbol();
const IS_STUBBED = Symbol();

export default class XHRAdapter extends Adapter {
static get name() {
Expand All @@ -14,10 +15,10 @@ export default class XHRAdapter extends Adapter {
this.assert('XHR global not found.', FakeXHR.xhr.supportsXHR);
this.assert(
'Running concurrent XHR adapters is unsupported, stop any running Polly instances.',
global.XMLHttpRequest === FakeXHR.xhr.GlobalXMLHttpRequest
!global.XMLHttpRequest[IS_STUBBED]
);

this.native = FakeXHR.xhr.GlobalXMLHttpRequest;
this.native = global.XMLHttpRequest;
this.xhr = FakeXHR.useFakeXMLHttpRequest();

this.xhr.onCreate = xhr => {
Expand All @@ -31,9 +32,12 @@ export default class XHRAdapter extends Adapter {
body
});
};

global.XMLHttpRequest[IS_STUBBED] = true;
}

onDisconnect() {
delete global.XMLHttpRequest[IS_STUBBED];
this.xhr.restore();
}

Expand Down
33 changes: 32 additions & 1 deletion packages/@pollyjs/adapter-xhr/tests/integration/adapter-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupMocha as setupPolly } from '@pollyjs/core';
import { Polly, setupMocha as setupPolly } from '@pollyjs/core';
import setupFetchRecord from '@pollyjs-tests/helpers/setup-fetch-record';
import adapterTests from '@pollyjs-tests/integration/adapter-tests';
import adapterBrowserTests from '@pollyjs-tests/integration/adapter-browser-tests';
Expand Down Expand Up @@ -26,3 +26,34 @@ describe('Integration | XHR Adapter', function() {
adapterTests();
adapterBrowserTests();
});

describe('Integration | XHR Adapter | Concurrency', function() {
it('should prevent concurrent XHR adapter instances', async function() {
const one = new Polly('one');
const two = new Polly('two');

one.connectTo(XHRAdapter);

expect(function() {
two.connectTo(XHRAdapter);
}).to.throw(/Running concurrent XHR adapters is unsupported/);

await one.stop();
await two.stop();
});

it('should allow you to register new instances once stopped', async function() {
const one = new Polly('one');
const two = new Polly('two');

one.connectTo(XHRAdapter);
await one.stop();

expect(function() {
two.connectTo(XHRAdapter);
}).to.not.throw();

await one.stop();
await two.stop();
});
});

0 comments on commit 79cdd96

Please sign in to comment.