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(core): Disconnect from all adapters when pause is called #291

Merged
merged 5 commits into from Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions docs/api.md
Expand Up @@ -181,30 +181,36 @@ polly.replay();

### pause

Puts polly in a paused mode. All requests going forward will pass through
and will not be recorded or replayed. The previous mode will be saved and can
be restored by calling [play](api#play)
Disconnects the polly instance from all connected adapters. This ensures that
no requests will be handled by the polly instance until calling [play](api#play)
or manually connecting to a new adapter via [connectTo](api#connectTo). The
previously connected adapters will be saved and can be restored by
calling [play](api#play).

**Example**

```js
// polly.mode === 'replay'
await fetch('/api/not-a-secret');
polly.pause();
// polly.mode === 'passthrough'
// This and all subsequent requests will no longer be handled by polly
await fetch('/api/secret');
```

### play

Restores the mode to the one before [pause](api#pause) was called.
Reconnects to the adapters that were disconnected when [pause](api#pause)
was called.

**Example**

```js
// polly.mode === 'replay'
await fetch('/api/not-a-secret');
polly.pause();
// polly.mode === 'passthrough'
// This and all subsequent requests will no longer be handled by polly
await fetch('/api/secret');
polly.play();
// polly.mode === 'replay'
// This and all subsequent requests will again be handled by polly
await fetch('/api/not-a-secret');
```

### stop
Expand Down
12 changes: 6 additions & 6 deletions packages/@pollyjs/core/src/polly.js
Expand Up @@ -14,7 +14,7 @@ import { validateRecordingName } from './utils/validators';

const RECORDING_NAME = Symbol();
const RECORDING_ID = Symbol();
const PAUSED_MODE = Symbol();
const PAUSED_ADAPTERS = Symbol();

const FACTORY_REGISTRATION = new WeakMap();
const EVENT_EMITTER = new EventEmitter({
Expand Down Expand Up @@ -203,18 +203,18 @@ export default class Polly {
* @memberof Polly
*/
pause() {
this[PAUSED_MODE] = this.mode;
this.mode = MODES.PASSTHROUGH;
this[PAUSED_ADAPTERS] = [...this.adapters.keys()];
this.disconnect();
}

/**
* @public
* @memberof Polly
*/
play() {
if (this[PAUSED_MODE]) {
this.mode = this[PAUSED_MODE];
delete this[PAUSED_MODE];
if (this[PAUSED_ADAPTERS]) {
this[PAUSED_ADAPTERS].forEach(adapterName => this.connectTo(adapterName));
delete this[PAUSED_ADAPTERS];
}
}

Expand Down
43 changes: 35 additions & 8 deletions packages/@pollyjs/core/tests/unit/polly-test.js
Expand Up @@ -239,6 +239,21 @@ describe('Unit | Polly', function() {
describe('API', function() {
setupPolly();

class MockAdapterA extends Adapter {
static get name() {
return 'adapter-a';
}

onConnect() {}
onDisconnect() {}
}

class MockAdapterB extends MockAdapterA {
static get name() {
return 'adapter-b';
}
}

it('.record()', async function() {
this.polly.mode = MODES.REPLAY;

Expand All @@ -256,23 +271,35 @@ describe('Unit | Polly', function() {
});

it('.pause()', async function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add a test asserting on the behavior of _order when a pause and play occurs with requests in between. Will prevent a regression but also solidify expected behavior.

this.polly.mode = MODES.RECORD;
this.polly.configure({ adapters: [MockAdapterA, MockAdapterB] });

expect(this.polly.mode).to.equal(MODES.RECORD);
expect([...this.polly.adapters.keys()]).to.deep.equal([
'adapter-a',
'adapter-b'
]);
this.polly.pause();
expect(this.polly.mode).to.equal(MODES.PASSTHROUGH);
expect([...this.polly.adapters.keys()]).to.deep.equal([]);
});

it('.play()', async function() {
this.polly.mode = MODES.RECORD;
this.polly.configure({ adapters: [MockAdapterA, MockAdapterB] });

expect(this.polly.mode).to.equal(MODES.RECORD);
expect([...this.polly.adapters.keys()]).to.deep.equal([
'adapter-a',
'adapter-b'
]);
this.polly.play();
expect(this.polly.mode).to.equal(MODES.RECORD);
expect([...this.polly.adapters.keys()]).to.deep.equal([
'adapter-a',
'adapter-b'
]);
this.polly.pause();
expect(this.polly.mode).to.equal(MODES.PASSTHROUGH);
expect([...this.polly.adapters.keys()]).to.deep.equal([]);
this.polly.play();
expect(this.polly.mode).to.equal(MODES.RECORD);
expect([...this.polly.adapters.keys()]).to.deep.equal([
'adapter-a',
'adapter-b'
]);
});

it('.stop()', async function() {
Expand Down