Skip to content

Commit

Permalink
fix: Legacy persisters and adapters should register (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit authored Apr 20, 2020
1 parent 0003c0e commit 8fd4d19
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 29 deletions.
9 changes: 6 additions & 3 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
EXPIRY_STRATEGIES,
PollyError,
Serializers,
assert
assert,
getFactoryId
} from '@pollyjs/utils';

import isExpired from './utils/is-expired';
Expand Down Expand Up @@ -35,7 +36,9 @@ export default class Adapter {
get options() {
return {
...(this.defaultOptions || {}),
...((this.polly.config.adapterOptions || {})[this.constructor.id] || {})
...((this.polly.config.adapterOptions || {})[
getFactoryId(this.constructor)
] || {})
};
}

Expand Down Expand Up @@ -222,7 +225,7 @@ export default class Adapter {

assert(message, ...args) {
assert(
`[${this.constructor.type}:${this.constructor.id}] ${message}`,
`[${this.constructor.type}:${getFactoryId(this.constructor)}] ${message}`,
...args
);
}
Expand Down
18 changes: 3 additions & 15 deletions packages/@pollyjs/core/src/-private/container.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { assert } from '@pollyjs/utils';

export function idOfFactory(Factory) {
if (Factory.hasOwnProperty('id')) {
return Factory.id;
}

console.warn(
`[Polly] ${Factory.id}:${Factory.name} "name" is deprecated and has been renamed to "id".`
);

return Factory.name;
}
import { assert, getFactoryId } from '@pollyjs/utils';

function keyFor(Factory) {
return `${Factory.type}:${idOfFactory(Factory)}`;
return `${Factory.type}:${getFactoryId(Factory)}`;
}

export class Container {
Expand All @@ -33,7 +21,7 @@ export class Container {
);

const { type } = Factory;
const name = idOfFactory(Factory);
const name = getFactoryId(Factory);

assert(
`Invalid registration id provided. Expected string, received: "${typeof name}"`,
Expand Down
10 changes: 5 additions & 5 deletions packages/@pollyjs/core/src/polly.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MODES, assert } from '@pollyjs/utils';
import { MODES, getFactoryId, assert } from '@pollyjs/utils';

import { version } from '../package.json';

import Logger from './-private/logger';
import { Container, idOfFactory } from './-private/container';
import { Container } from './-private/container';
import DefaultConfig from './defaults/config';
import PollyRequest from './-private/request';
import guidForRecording from './utils/guid-for-recording';
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class Polly {
if (persister) {
if (typeof persister === 'function') {
container.register(persister);
persister = idOfFactory(persister);
persister = getFactoryId(persister);
}

assert(
Expand Down Expand Up @@ -262,7 +262,7 @@ export default class Polly {

if (typeof idOrFactoryIdGetter === 'function') {
container.register(idOrFactoryIdGetter);
adapterId = idOfFactory(idOrFactoryIdGetter);
adapterId = getFactoryId(idOrFactoryIdGetter);
}

assert(
Expand All @@ -288,7 +288,7 @@ export default class Polly {
let adapterId = idOrFactoryIdGetter;

if (typeof idOrFactoryIdGetter === 'function') {
adapterId = idOfFactory(idOrFactoryIdGetter);
adapterId = getFactoryId(idOrFactoryIdGetter);
}

if (adapters.has(adapterId)) {
Expand Down
68 changes: 68 additions & 0 deletions packages/@pollyjs/core/tests/unit/polly-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ describe('Unit | Polly', function() {
expect(persistCalled).to.be.true;
});

it('it supports legacy custom persisters', async function() {
const fakeContext = {};
let instantiated, persistCalled, recognizedOptions;

class MockPersister extends Persister {
static get name() {
return 'mock';
}

constructor() {
super(...arguments);
instantiated = true;
}

persist() {
persistCalled = true;
recognizedOptions = this.options;
super.persist(...arguments);
}
}

const polly = new Polly('recording name', {
persister: MockPersister,
persisterOptions: {
mock: {
context: fakeContext
}
}
});

await polly.stop();

expect(instantiated).to.be.true;
expect(persistCalled).to.be.true;
expect(recognizedOptions.context).to.equal(fakeContext);
});

describe('configure', function() {
setupPolly();

Expand Down Expand Up @@ -213,6 +250,37 @@ describe('Unit | Polly', function() {
expect(connectCalled).to.be.true;
});

it('should connect legacy custom adapters', async function() {
const fakeContext = {};
let connectCalled = false;
let options;

class MockAdapter extends Adapter {
static get name() {
return 'mock';
}

onConnect() {
connectCalled = true;
options = this.options;
}

onDisconnect() {}
}

expect(connectCalled).to.be.false;
this.polly.configure({
adapters: [MockAdapter],
adapterOptions: {
mock: {
context: fakeContext
}
}
});
expect(connectCalled).to.be.true;
expect(options.context).to.equal(fakeContext);
});

it('should disconnect from adapters before connecting', async function() {
let connectCount = 0;
let disconnectCount = 0;
Expand Down
12 changes: 6 additions & 6 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import stringify from 'fast-json-stable-stringify';
import { ACTIONS, assert } from '@pollyjs/utils';
import { ACTIONS, assert, getFactoryId } from '@pollyjs/utils';

import HAR from './har';
import Entry from './har/entry';
Expand Down Expand Up @@ -27,11 +27,11 @@ export default class Persister {
}

get options() {
const { id } = this.constructor;

return {
...(this.defaultOptions || {}),
...((this.polly.config.persisterOptions || {})[id] || {})
...((this.polly.config.persisterOptions || {})[
getFactoryId(this.constructor)
] || {})
};
}

Expand All @@ -53,7 +53,7 @@ export default class Persister {
const creator = {
name: CREATOR_NAME,
version: this.polly.constructor.VERSION,
comment: `${this.constructor.type}:${this.constructor.id}`
comment: `${this.constructor.type}:${getFactoryId(this.constructor)}`
};

for (const [recordingId, { name, requests }] of this.pending) {
Expand Down Expand Up @@ -177,7 +177,7 @@ export default class Persister {

assert(message, ...args) {
assert(
`[${this.constructor.type}:${this.constructor.id}] ${message}`,
`[${this.constructor.type}:${getFactoryId(this.constructor)}] ${message}`,
...args
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@pollyjs/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export { default as PollyError } from './utils/polly-error';
export { default as Serializers } from './utils/serializers';

export { default as URL } from './utils/url';

export { default as getFactoryId } from './utils/get-factory-id';
11 changes: 11 additions & 0 deletions packages/@pollyjs/utils/src/utils/get-factory-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function getFactoryId(Factory) {
if (Factory.hasOwnProperty('id')) {
return Factory.id;
}

console.warn(
`[Polly] ${Factory.name} "name" is deprecated and has been renamed to "id".`
);

return Factory.name;
}

0 comments on commit 8fd4d19

Please sign in to comment.