Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Loosen up global XHR native check #69

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});