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

feat: Improved adapter and persister registration #62

Merged
merged 4 commits into from
Jul 12, 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
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ Check out the [Quick Start](https://netflix.github.io/pollyjs/#/quick-start) doc

## Usage

Let's take a look at what an example test case would look like using Polly.
Lets take a look at what an example test case would look like using Polly.

```js
import { Polly } from '@pollyjs/core';
import { Polly, FetchAdapter, XHRAdapter, RESTPersister } from '@pollyjs/core';

/*
Register the adapters and persisters we want to use. This way all future
polly instances can access them by name.
*/
Polly.register(XHRAdapter)
Polly.register(FetchAdapter)
Polly.register(RESTPersister)

describe('Netflix Homepage', function() {
it('should be able to sign in', async function() {
Expand All @@ -52,7 +60,10 @@ describe('Netflix Homepage', function() {
will record any requests that it hasn't yet seen while replaying ones it
has already recorded.
*/
const polly = new Polly('Sign In');
const polly = new Polly('Sign In', {
adapters: ['xhr', 'fetch'],
persister: 'rest'
});
const { server } = polly;

/* Intercept all Google Analytic requests and respond with a 200 */
Expand Down
24 changes: 2 additions & 22 deletions docs/adapters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,13 @@ adapter class.
```js
// Register and connect to a custom adapter:
new Polly('Custom Adapter', {
adapters: [
['my-custom-adapter', MyCustomAdapterClass]
]
});

// Register and connect to the default adapters + a custom adapter:
new Polly('Defaults + Custom Adapter', {
adapters: [
'fetch',
'xhr',
['my-custom-adapter', MyCustomAdapterClass]
]
});

// Register and connect to a custom fetch adapter:
new Polly('Custom Fetch Adapter', {
adapters: [
['fetch', MyCustomFetchAdapterClass]
]
adapters: [MyCustomAdapterClass]
});

// Register and connect to a custom adapter via .configure():
const polly = new Polly('Custom Adapter');

polly.configure({
adapters: [
['my-custom-adapter', MyCustomAdapterClass]
]
adapters: [MyCustomAdapterClass]
});
```
5 changes: 5 additions & 0 deletions docs/adapters/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ The fetch adapter is connected to by default but you can use the
adapter.

```js
import { Polly, FetchAdapter } from '@pollyjs/core';

// Register the fetch adapter so its accessible by all future polly instances
Polly.register(FetchAdapter);

const polly = new Polly('<Recording Name>', {
adapters: ['fetch']
});
Expand Down
5 changes: 5 additions & 0 deletions docs/adapters/xhr.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ The XHR adapter is connected to by default but you can use the
adapter.

```js
import { Polly, XHRAdapter } from '@pollyjs/core';

// Register the xhr adapter so its accessible by all future polly instances
Polly.register(XHRAdapter);

const polly = new Polly('<Recording Name>', {
adapters: ['xhr']
});
Expand Down
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ Connect to an adapter.

| Param | Type | Description |
| --- | --- | --- |
| name | `String` | The name of the adapter to connect to |
| name | `String|Function` | The adapter name of class to connect to |

__Example__

```js
polly.connectTo('xhr');
polly.connectTo(XHRAdapter);
```

### disconnectFrom
Expand All @@ -232,12 +233,13 @@ Disconnect from an adapter.

| Param | Type | Description |
| --- | --- | --- |
| name | `String` | The name of the adapter to disconnect from |
| name | `String|Function` | The adapter name of class to disconnect from |

__Example__

```js
polly.disconnectFrom('xhr');
polly.disconnectFrom(XHRAdapter);
```

### disconnect
Expand Down
24 changes: 19 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,21 @@ polly.configure({

## adapters

_Type_: `Array`
_Default_: `['fetch', 'xhr']`
_Type_: `Array[String|Function]`
_Default_: `[]`

The adapter(s) polly will hook into.

__Example__

```js
import { XHRAdapter, FetchAdapter } from '@pollyjs/core';

// Register the xhr adapter so its accessible by all future polly instances
Polly.register(XHRAdapter);

polly.configure({
adapters: ['xhr']
adapters: ['xhr', FetchAdapter]
});
```

Expand Down Expand Up @@ -165,17 +170,26 @@ polly.configure({

## persister

_Type_: `String`
_Default_: `'rest'`
_Type_: `String|Function`
_Default_: `null`

The persister to use for recording and replaying requests.

__Example__

```js
import { LocalStoragePersister, RESTPersister } from '@pollyjs/core';

// Register the local-storage persister so its accessible by all future polly instances
Polly.register(LocalStoragePersister);

polly.configure({
persister: 'local-storage'
});

polly.configure({
persister: RESTPersister
});
```

## persisterOptions
Expand Down
8 changes: 6 additions & 2 deletions docs/persisters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ yarn add @pollyjs/persister -D
import Persister from '@pollyjs/persister';

class CustomPersister extends Persister {
static get name() {
return 'custom';
}

findRecording() {}

saveRecording() {}
Expand Down Expand Up @@ -61,13 +65,13 @@ persister class.
```js
// Register and connect to a custom persister:
new Polly('Custom Persister', {
persister: ['my-custom-persister', MyCustomPersisterClass]
persister: MyCustomPersisterClass
});

// Register and connect to a custom persister via .configure():
const polly = new Polly('Custom Persister');

polly.configure({
persister: ['my-custom-persister', MyCustomPersisterClass]
persister: MyCustomPersisterClass
});
```
5 changes: 4 additions & 1 deletion docs/persisters/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ yarn add @pollyjs/persister-fs -D
import { Polly } from '@pollyjs/core';
import FSPersister from '@pollyjs/persister-fs';

// Register the fs persister so its accessible by all future polly instances
Polly.register(FSPersister);

new Polly('<Recording Name>', {
persister: ['fs', FSPersister]
persister: 'fs'
});
```

Expand Down
7 changes: 6 additions & 1 deletion docs/persisters/local-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ Read and write recordings to and from the browser's Local Storage.
## Usage

```js
const polly = new Polly('<Recording Name>', {
import { Polly, LocalStoragePersister } from '@pollyjs/core';

// Register the local-storage persister so its accessible by all future polly instances
Polly.register(LocalStoragePersister);

new Polly('<Recording Name>', {
persister: 'local-storage'
});
```
Expand Down
13 changes: 6 additions & 7 deletions docs/persisters/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ as well as a [CLI](cli/overview) to get you up and running.
## Usage

```js
import { Polly, RESTPersister } from '@pollyjs/core';

// Register the REST persister so its accessible by all future polly instances
Polly.register(RESTPersister);

new Polly('<Recording Name>', {
persister: 'rest',
persisterOptions: {
rest: {
host: '',
apiNamespace: '/polly'
}
}
persister: 'rest'
});
```

Expand Down
29 changes: 26 additions & 3 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ Now that you've installed Polly and have setup your server, you're ready to
fly. Lets take a look at what an example test case would look like using Polly.

```js
import { Polly } from '@pollyjs/core';
import { Polly, FetchAdapter, XHRAdapter, RESTPersister } from '@pollyjs/core';

/*
Register the adapters and persisters we want to use. This way all future
polly instances can access them by name.
*/
Polly.register(XHRAdapter)
Polly.register(FetchAdapter)
Polly.register(RESTPersister)

describe('Netflix Homepage', function() {
it('should be able to sign in', async function() {
Expand All @@ -61,7 +69,19 @@ describe('Netflix Homepage', function() {
will record any requests that it hasn't yet seen while replaying ones it
has already recorded.
*/
const polly = new Polly('Sign In');
const polly = new Polly('Sign In', {
adapters: ['xhr', 'fetch'],
persister: 'rest'
});
const { server } = polly;

/* Intercept all Google Analytic requests and respond with a 200 */
server
.get('/google-analytics/*path')
.intercept((req, res) => res.sendStatus(200));

/* Pass-through all GET requests to /coverage */
server.get('/coverage').passthrough();

/* start: pseudo test code */
await visit('/login');
Expand Down Expand Up @@ -169,7 +189,10 @@ import { Polly } from '@pollyjs/core';

describe('Netflix Homepage', function() {
it('should handle a failed sign in attempt', async function() {
const polly = new Polly('Failed Sign In');
const polly = new Polly('Failed Sign In', {
adapters: ['xhr', 'fetch'],
persister: 'rest'
});
const { server } = polly;

/*
Expand Down
15 changes: 13 additions & 2 deletions packages/@pollyjs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ Check out the [Quick Start](https://netflix.github.io/pollyjs/#/quick-start) doc
Lets take a look at what an example test case would look like using Polly.

```js
import { Polly } from '@pollyjs/core';
import { Polly, FetchAdapter, XHRAdapter, RESTPersister } from '@pollyjs/core';

/*
Register the adapters and persisters we want to use. This way all future
polly instances can access them by name.
*/
Polly.register(XHRAdapter)
Polly.register(FetchAdapter)
Polly.register(RESTPersister)

describe('Netflix Homepage', function() {
it('should be able to sign in', async function() {
Expand All @@ -65,7 +73,10 @@ describe('Netflix Homepage', function() {
will record any requests that it hasn't yet seen while replaying ones it
has already recorded.
*/
const polly = new Polly('Sign In');
const polly = new Polly('Sign In', {
adapters: ['xhr', 'fetch'],
persister: 'rest'
});
const { server } = polly;

/* Intercept all Google Analytic requests and respond with a 200 */
Expand Down
Loading